vendor/symfony/form/Extension/DependencyInjection/DependencyInjectionExtension.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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\Component\Form\Extension\DependencyInjection;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\Form\Exception\InvalidArgumentException;
  13. use Symfony\Component\Form\FormExtensionInterface;
  14. use Symfony\Component\Form\FormTypeGuesserChain;
  15. class DependencyInjectionExtension implements FormExtensionInterface
  16. {
  17.     private $guesser;
  18.     private $guesserLoaded false;
  19.     private $typeContainer;
  20.     private $typeExtensionServices;
  21.     private $guesserServices;
  22.     /**
  23.      * @param iterable[] $typeExtensionServices
  24.      */
  25.     public function __construct(ContainerInterface $typeContainer, array $typeExtensionServicesiterable $guesserServices)
  26.     {
  27.         $this->typeContainer $typeContainer;
  28.         $this->typeExtensionServices $typeExtensionServices;
  29.         $this->guesserServices $guesserServices;
  30.     }
  31.     public function getType($name)
  32.     {
  33.         if (!$this->typeContainer->has($name)) {
  34.             throw new InvalidArgumentException(sprintf('The field type "%s" is not registered in the service container.'$name));
  35.         }
  36.         return $this->typeContainer->get($name);
  37.     }
  38.     public function hasType($name)
  39.     {
  40.         return $this->typeContainer->has($name);
  41.     }
  42.     public function getTypeExtensions($name)
  43.     {
  44.         $extensions = [];
  45.         if (isset($this->typeExtensionServices[$name])) {
  46.             foreach ($this->typeExtensionServices[$name] as $extension) {
  47.                 $extensions[] = $extension;
  48.                 if (method_exists($extension'getExtendedTypes')) {
  49.                     $extendedTypes = [];
  50.                     foreach ($extension::getExtendedTypes() as $extendedType) {
  51.                         $extendedTypes[] = $extendedType;
  52.                     }
  53.                 } else {
  54.                     $extendedTypes = [$extension->getExtendedType()];
  55.                 }
  56.                 // validate the result of getExtendedTypes()/getExtendedType() to ensure it is consistent with the service definition
  57.                 if (!\in_array($name$extendedTypestrue)) {
  58.                     throw new InvalidArgumentException(sprintf('The extended type "%s" specified for the type extension class "%s" does not match any of the actual extended types (["%s"]).'$name, \get_class($extension), implode('", "'$extendedTypes)));
  59.                 }
  60.             }
  61.         }
  62.         return $extensions;
  63.     }
  64.     public function hasTypeExtensions($name)
  65.     {
  66.         return isset($this->typeExtensionServices[$name]);
  67.     }
  68.     public function getTypeGuesser()
  69.     {
  70.         if (!$this->guesserLoaded) {
  71.             $this->guesserLoaded true;
  72.             $guessers = [];
  73.             foreach ($this->guesserServices as $serviceId => $service) {
  74.                 $guessers[] = $service;
  75.             }
  76.             if ($guessers) {
  77.                 $this->guesser = new FormTypeGuesserChain($guessers);
  78.             }
  79.         }
  80.         return $this->guesser;
  81.     }
  82. }