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

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. /**
  20.  * Adds all services with the tags "form.type", "form.type_extension" and
  21.  * "form.type_guesser" as arguments of the "form.extension" service.
  22.  *
  23.  * @author Bernhard Schussek <bschussek@gmail.com>
  24.  */
  25. class FormPass implements CompilerPassInterface
  26. {
  27.     use PriorityTaggedServiceTrait;
  28.     public function process(ContainerBuilder $container)
  29.     {
  30.         if (!$container->hasDefinition('form.extension')) {
  31.             return;
  32.         }
  33.         $definition $container->getDefinition('form.extension');
  34.         $definition->replaceArgument(0$this->processFormTypes($container));
  35.         $definition->replaceArgument(1$this->processFormTypeExtensions($container));
  36.         $definition->replaceArgument(2$this->processFormTypeGuessers($container));
  37.     }
  38.     private function processFormTypes(ContainerBuilder $container): Reference
  39.     {
  40.         // Get service locator argument
  41.         $servicesMap = [];
  42.         $namespaces = ['Symfony\Component\Form\Extension\Core\Type' => true];
  43.         // Builds an array with fully-qualified type class names as keys and service IDs as values
  44.         foreach ($container->findTaggedServiceIds('form.type'true) as $serviceId => $tag) {
  45.             // Add form type service to the service locator
  46.             $serviceDefinition $container->getDefinition($serviceId);
  47.             $servicesMap[$formType $serviceDefinition->getClass()] = new Reference($serviceId);
  48.             $namespaces[substr($formType0strrpos($formType'\\'))] = true;
  49.         }
  50.         if ($container->hasDefinition('console.command.form_debug')) {
  51.             $commandDefinition $container->getDefinition('console.command.form_debug');
  52.             $commandDefinition->setArgument(1array_keys($namespaces));
  53.             $commandDefinition->setArgument(2array_keys($servicesMap));
  54.         }
  55.         return ServiceLocatorTagPass::register($container$servicesMap);
  56.     }
  57.     private function processFormTypeExtensions(ContainerBuilder $container): array
  58.     {
  59.         $typeExtensions = [];
  60.         $typeExtensionsClasses = [];
  61.         foreach ($this->findAndSortTaggedServices('form.type_extension'$container) as $reference) {
  62.             $serviceId = (string) $reference;
  63.             $serviceDefinition $container->getDefinition($serviceId);
  64.             $tag $serviceDefinition->getTag('form.type_extension');
  65.             $typeExtensionClass $container->getParameterBag()->resolveValue($serviceDefinition->getClass());
  66.             if (isset($tag[0]['extended_type'])) {
  67.                 $typeExtensions[$tag[0]['extended_type']][] = new Reference($serviceId);
  68.                 $typeExtensionsClasses[] = $typeExtensionClass;
  69.             } else {
  70.                 $extendsTypes false;
  71.                 $typeExtensionsClasses[] = $typeExtensionClass;
  72.                 foreach ($typeExtensionClass::getExtendedTypes() as $extendedType) {
  73.                     $typeExtensions[$extendedType][] = new Reference($serviceId);
  74.                     $extendsTypes true;
  75.                 }
  76.                 if (!$extendsTypes) {
  77.                     throw new InvalidArgumentException(sprintf('The getExtendedTypes() method for service "%s" does not return any extended types.'$serviceId));
  78.                 }
  79.             }
  80.         }
  81.         foreach ($typeExtensions as $extendedType => $extensions) {
  82.             $typeExtensions[$extendedType] = new IteratorArgument($extensions);
  83.         }
  84.         if ($container->hasDefinition('console.command.form_debug')) {
  85.             $commandDefinition $container->getDefinition('console.command.form_debug');
  86.             $commandDefinition->setArgument(3$typeExtensionsClasses);
  87.         }
  88.         return $typeExtensions;
  89.     }
  90.     private function processFormTypeGuessers(ContainerBuilder $container): ArgumentInterface
  91.     {
  92.         $guessers = [];
  93.         $guessersClasses = [];
  94.         foreach ($container->findTaggedServiceIds('form.type_guesser'true) as $serviceId => $tags) {
  95.             $guessers[] = new Reference($serviceId);
  96.             $serviceDefinition $container->getDefinition($serviceId);
  97.             $guessersClasses[] = $serviceDefinition->getClass();
  98.         }
  99.         if ($container->hasDefinition('console.command.form_debug')) {
  100.             $commandDefinition $container->getDefinition('console.command.form_debug');
  101.             $commandDefinition->setArgument(4$guessersClasses);
  102.         }
  103.         return new IteratorArgument($guessers);
  104.     }
  105. }