vendor/symfony/form/DependencyInjection/FormPass.php line 60

Open in your IDE?
  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\Component\Form\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Argument\ArgumentInterface;
  12. use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
  15. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\Form\FormTypeExtensionInterface;
  20. /**
  21.  * Adds all services with the tags "form.type", "form.type_extension" and
  22.  * "form.type_guesser" as arguments of the "form.extension" service.
  23.  *
  24.  * @author Bernhard Schussek <bschussek@gmail.com>
  25.  */
  26. class FormPass implements CompilerPassInterface
  27. {
  28.     use PriorityTaggedServiceTrait;
  29.     private $formExtensionService;
  30.     private $formTypeTag;
  31.     private $formTypeExtensionTag;
  32.     private $formTypeGuesserTag;
  33.     private $formDebugCommandService;
  34.     public function __construct(string $formExtensionService 'form.extension'string $formTypeTag 'form.type'string $formTypeExtensionTag 'form.type_extension'string $formTypeGuesserTag 'form.type_guesser'string $formDebugCommandService 'console.command.form_debug')
  35.     {
  36.         $this->formExtensionService $formExtensionService;
  37.         $this->formTypeTag $formTypeTag;
  38.         $this->formTypeExtensionTag $formTypeExtensionTag;
  39.         $this->formTypeGuesserTag $formTypeGuesserTag;
  40.         $this->formDebugCommandService $formDebugCommandService;
  41.     }
  42.     public function process(ContainerBuilder $container)
  43.     {
  44.         if (!$container->hasDefinition($this->formExtensionService)) {
  45.             return;
  46.         }
  47.         $definition $container->getDefinition($this->formExtensionService);
  48.         if (new IteratorArgument([]) != $definition->getArgument(2)) {
  49.             return;
  50.         }
  51.         $definition->replaceArgument(0$this->processFormTypes($container));
  52.         $definition->replaceArgument(1$this->processFormTypeExtensions($container));
  53.         $definition->replaceArgument(2$this->processFormTypeGuessers($container));
  54.     }
  55.     private function processFormTypes(ContainerBuilder $container): Reference
  56.     {
  57.         // Get service locator argument
  58.         $servicesMap = [];
  59.         $namespaces = ['Symfony\Component\Form\Extension\Core\Type' => true];
  60.         // Builds an array with fully-qualified type class names as keys and service IDs as values
  61.         foreach ($container->findTaggedServiceIds($this->formTypeTagtrue) as $serviceId => $tag) {
  62.             // Add form type service to the service locator
  63.             $serviceDefinition $container->getDefinition($serviceId);
  64.             $servicesMap[$formType $serviceDefinition->getClass()] = new Reference($serviceId);
  65.             $namespaces[substr($formType0strrpos($formType'\\'))] = true;
  66.         }
  67.         if ($container->hasDefinition($this->formDebugCommandService)) {
  68.             $commandDefinition $container->getDefinition($this->formDebugCommandService);
  69.             $commandDefinition->setArgument(1array_keys($namespaces));
  70.             $commandDefinition->setArgument(2array_keys($servicesMap));
  71.         }
  72.         return ServiceLocatorTagPass::register($container$servicesMap);
  73.     }
  74.     private function processFormTypeExtensions(ContainerBuilder $container): array
  75.     {
  76.         $typeExtensions = [];
  77.         $typeExtensionsClasses = [];
  78.         foreach ($this->findAndSortTaggedServices($this->formTypeExtensionTag$container) as $reference) {
  79.             $serviceId = (string) $reference;
  80.             $serviceDefinition $container->getDefinition($serviceId);
  81.             $tag $serviceDefinition->getTag($this->formTypeExtensionTag);
  82.             $typeExtensionClass $container->getParameterBag()->resolveValue($serviceDefinition->getClass());
  83.             if (isset($tag[0]['extended_type'])) {
  84.                 if (!method_exists($typeExtensionClass'getExtendedTypes')) {
  85.                     @trigger_error(sprintf('Not implementing the "%s::getExtendedTypes()" method in "%s" is deprecated since Symfony 4.2.'FormTypeExtensionInterface::class, $typeExtensionClass), \E_USER_DEPRECATED);
  86.                 }
  87.                 $typeExtensions[$tag[0]['extended_type']][] = new Reference($serviceId);
  88.                 $typeExtensionsClasses[] = $typeExtensionClass;
  89.             } elseif (method_exists($typeExtensionClass'getExtendedTypes')) {
  90.                 $extendsTypes false;
  91.                 $typeExtensionsClasses[] = $typeExtensionClass;
  92.                 foreach ($typeExtensionClass::getExtendedTypes() as $extendedType) {
  93.                     $typeExtensions[$extendedType][] = new Reference($serviceId);
  94.                     $extendsTypes true;
  95.                 }
  96.                 if (!$extendsTypes) {
  97.                     throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.'$serviceId));
  98.                 }
  99.             } else {
  100.                 throw new InvalidArgumentException(sprintf('"%s" tagged services have to implement the static getExtendedTypes() method. Class "%s" for service "%s" does not implement it.'$this->formTypeExtensionTag$typeExtensionClass$serviceId));
  101.             }
  102.         }
  103.         foreach ($typeExtensions as $extendedType => $extensions) {
  104.             $typeExtensions[$extendedType] = new IteratorArgument($extensions);
  105.         }
  106.         if ($container->hasDefinition($this->formDebugCommandService)) {
  107.             $commandDefinition $container->getDefinition($this->formDebugCommandService);
  108.             $commandDefinition->setArgument(3$typeExtensionsClasses);
  109.         }
  110.         return $typeExtensions;
  111.     }
  112.     private function processFormTypeGuessers(ContainerBuilder $container): ArgumentInterface
  113.     {
  114.         $guessers = [];
  115.         $guessersClasses = [];
  116.         foreach ($container->findTaggedServiceIds($this->formTypeGuesserTagtrue) as $serviceId => $tags) {
  117.             $guessers[] = new Reference($serviceId);
  118.             $serviceDefinition $container->getDefinition($serviceId);
  119.             $guessersClasses[] = $serviceDefinition->getClass();
  120.         }
  121.         if ($container->hasDefinition($this->formDebugCommandService)) {
  122.             $commandDefinition $container->getDefinition($this->formDebugCommandService);
  123.             $commandDefinition->setArgument(4$guessersClasses);
  124.         }
  125.         return new IteratorArgument($guessers);
  126.     }
  127. }