vendor/sonata-project/core-bundle/src/Form/Extension/DependencyInjectionExtension.php line 146

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sonata Project package.
  4.  *
  5.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\CoreBundle\Form\Extension;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\Form\FormExtensionInterface;
  14. use Symfony\Component\Form\FormTypeGuesserChain;
  15. use Symfony\Component\Form\FormTypeGuesserInterface;
  16. use Symfony\Component\Form\FormTypeInterface;
  17. /**
  18.  * This proxy class help to keep BC code with < SF2.8 form behavior by restoring
  19.  * the type as a code and not as a class.
  20.  *
  21.  * @deprecated since 3.7, to be removed in 4.0, the form mapping feature should be disabled.
  22.  */
  23. class DependencyInjectionExtension implements FormExtensionInterface
  24. {
  25.     /**
  26.      * @var FormExtensionInterface
  27.      */
  28.     protected $extension;
  29.     /**
  30.      * @var array
  31.      */
  32.     protected $mappingTypes;
  33.     /**
  34.      * @var array
  35.      */
  36.     protected $extensionTypes;
  37.     /**
  38.      * @var ContainerInterface
  39.      */
  40.     private $container;
  41.     /**
  42.      * @var string[]
  43.      */
  44.     private $typeServiceIds;
  45.     /**
  46.      * @var string[]
  47.      */
  48.     private $typeExtensionServiceIds;
  49.     /**
  50.      * @var string[]
  51.      */
  52.     private $guesserServiceIds;
  53.     /**
  54.      * @var FormTypeGuesserInterface
  55.      */
  56.     private $guesser;
  57.     /**
  58.      * @var bool
  59.      */
  60.     private $guesserLoaded false;
  61.     public function __construct(
  62.         ContainerInterface $container,
  63.         array $typeServiceIds,
  64.         array $typeExtensionServiceIds,
  65.         array $guesserServiceIds,
  66.         array $mappingTypes = [],
  67.         array $extensionTypes = []
  68.     ) {
  69.         $this->container $container;
  70.         $this->typeServiceIds $typeServiceIds;
  71.         $this->typeExtensionServiceIds $typeExtensionServiceIds;
  72.         $this->guesserServiceIds $guesserServiceIds;
  73.         $this->mappingTypes $mappingTypes;
  74.         $this->mappingExtensionTypes $extensionTypes;
  75.         $this->reverseMappingTypes array_flip($mappingTypes);
  76.     }
  77.     public function getType($name)
  78.     {
  79.         // resolve code to FQCN
  80.         $name self::findClass($this->mappingTypes$name);
  81.         if (!isset($this->typeServiceIds[$name])) {
  82.             if (class_exists($name) && \in_array(FormTypeInterface::class, class_implements($name), true)) {
  83.                 return new $name();
  84.             }
  85.             throw new InvalidArgumentException(
  86.                 sprintf('The field type "%s" is not registered with the service container.'$name)
  87.             );
  88.         }
  89.         $type $this->container->get($this->typeServiceIds[$name]);
  90.         if ($name !== \get_class($type) && (method_exists($type'getName') && $type->getName() !== $name)) {
  91.             throw new InvalidArgumentException(sprintf(
  92.                 'The type name specified for the service "%s" does not match the actual name.'
  93.                 .' Expected "%s", given "%s"',
  94.                 $this->typeServiceIds[$name],
  95.                 $name,
  96.                 \get_class($type)
  97.             ));
  98.         }
  99.         return $type;
  100.     }
  101.     public function hasType($name)
  102.     {
  103.         return isset($this->mappingTypes[$name]) || isset($this->typeServiceIds[$name]);
  104.     }
  105.     public function getTypeExtensions($name)
  106.     {
  107.         // lookup inside the extension mapping
  108.         $serviceIdx = [];
  109.         if (isset($this->reverseMappingTypes[$name])) {
  110.             $code $this->reverseMappingTypes[$name];
  111.             if (isset($this->mappingExtensionTypes[$code])) {
  112.                 $serviceIdx array_merge($serviceIdx$this->mappingExtensionTypes[$code]);
  113.             }
  114.         }
  115.         $serviceIdx array_unique(array_merge(isset($this->typeExtensionServiceIds[$name]) ? $this->typeExtensionServiceIds[$name] : [], $serviceIdx));
  116.         $extensions = [];
  117.         foreach ($serviceIdx as $serviceId) {
  118.             if ($this->container->has($serviceId)) {
  119.                 $extensions[] = $this->container->get($serviceId);
  120.             }
  121.         }
  122.         return $extensions;
  123.     }
  124.     public function hasTypeExtensions($name)
  125.     {
  126.         return isset($this->reverseMappingTypes[$name]) || isset($this->typeExtensionServiceIds[$name]);
  127.     }
  128.     public function getTypeGuesser()
  129.     {
  130.         if (!$this->guesserLoaded) {
  131.             $this->guesserLoaded true;
  132.             $guessers = [];
  133.             foreach ($this->guesserServiceIds as $serviceId) {
  134.                 if ($this->container->has($serviceId)) {
  135.                     $guessers[] = $this->container->get($serviceId);
  136.                 }
  137.             }
  138.             if ($guessers) {
  139.                 $this->guesser = new FormTypeGuesserChain($guessers);
  140.             }
  141.         }
  142.         return $this->guesser;
  143.     }
  144.     /**
  145.      * @param string $type
  146.      *
  147.      * @return string
  148.      */
  149.     protected static function findClass($mapping$type)
  150.     {
  151.         if (strpos($type'\\')) {
  152.             return $type;
  153.         }
  154.         if (!isset($mapping[$type])) {
  155.             return $type;
  156.         }
  157.         return $mapping[$type];
  158.     }
  159. }