DoctrineOrmExtension.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\Form;
  11. use Doctrine\Common\Persistence\ManagerRegistry;
  12. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  13. use Symfony\Component\Form\AbstractExtension;
  14. use Symfony\Component\Form\ChoiceList\Factory\CachingFactoryDecorator;
  15. use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
  16. use Symfony\Component\Form\ChoiceList\Factory\DefaultChoiceListFactory;
  17. use Symfony\Component\Form\ChoiceList\Factory\PropertyAccessDecorator;
  18. use Symfony\Component\PropertyAccess\PropertyAccess;
  19. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  20. class DoctrineOrmExtension extends AbstractExtension
  21. {
  22. protected $registry;
  23. private $propertyAccessor;
  24. private $choiceListFactory;
  25. public function __construct(ManagerRegistry $registry, PropertyAccessorInterface $propertyAccessor = null, ChoiceListFactoryInterface $choiceListFactory = null)
  26. {
  27. $this->registry = $registry;
  28. $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
  29. $this->choiceListFactory = $choiceListFactory ?: new CachingFactoryDecorator(new PropertyAccessDecorator(new DefaultChoiceListFactory(), $this->propertyAccessor));
  30. }
  31. protected function loadTypes()
  32. {
  33. return array(
  34. new EntityType($this->registry, $this->propertyAccessor, $this->choiceListFactory),
  35. );
  36. }
  37. protected function loadTypeGuesser()
  38. {
  39. return new DoctrineOrmTypeGuesser($this->registry);
  40. }
  41. }