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

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