vendor/symfony/dependency-injection/Compiler/ServiceLocatorTagPass.php line 103

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\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Alias;
  12. use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
  13. use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
  14. use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Definition;
  17. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  18. use Symfony\Component\DependencyInjection\Reference;
  19. use Symfony\Component\DependencyInjection\ServiceLocator;
  20. /**
  21.  * Applies the "container.service_locator" tag by wrapping references into ServiceClosureArgument instances.
  22.  *
  23.  * @author Nicolas Grekas <p@tchwork.com>
  24.  */
  25. final class ServiceLocatorTagPass extends AbstractRecursivePass
  26. {
  27.     use PriorityTaggedServiceTrait;
  28.     protected function processValue(mixed $valuebool $isRoot false): mixed
  29.     {
  30.         if ($value instanceof ServiceLocatorArgument) {
  31.             if ($value->getTaggedIteratorArgument()) {
  32.                 $value->setValues($this->findAndSortTaggedServices($value->getTaggedIteratorArgument(), $this->container));
  33.             }
  34.             return self::register($this->container$value->getValues());
  35.         }
  36.         if ($value instanceof Definition) {
  37.             $value->setBindings(parent::processValue($value->getBindings()));
  38.         }
  39.         if (!$value instanceof Definition || !$value->hasTag('container.service_locator')) {
  40.             return parent::processValue($value$isRoot);
  41.         }
  42.         if (!$value->getClass()) {
  43.             $value->setClass(ServiceLocator::class);
  44.         }
  45.         $services $value->getArguments()[0] ?? null;
  46.         if ($services instanceof TaggedIteratorArgument) {
  47.             $services $this->findAndSortTaggedServices($services$this->container);
  48.         }
  49.         if (!\is_array($services)) {
  50.             throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set.'$this->currentId));
  51.         }
  52.         $i 0;
  53.         foreach ($services as $k => $v) {
  54.             if ($v instanceof ServiceClosureArgument) {
  55.                 continue;
  56.             }
  57.             if ($i === $k) {
  58.                 if ($v instanceof Reference) {
  59.                     unset($services[$k]);
  60.                     $k = (string) $v;
  61.                 }
  62.                 ++$i;
  63.             } elseif (\is_int($k)) {
  64.                 $i null;
  65.             }
  66.             $services[$k] = new ServiceClosureArgument($v);
  67.         }
  68.         ksort($services);
  69.         $value->setArgument(0$services);
  70.         $id '.service_locator.'.ContainerBuilder::hash($value);
  71.         if ($isRoot) {
  72.             if ($id !== $this->currentId) {
  73.                 $this->container->setAlias($id, new Alias($this->currentIdfalse));
  74.             }
  75.             return $value;
  76.         }
  77.         $this->container->setDefinition($id$value->setPublic(false));
  78.         return new Reference($id);
  79.     }
  80.     public static function register(ContainerBuilder $container, array $mapstring $callerId null): Reference
  81.     {
  82.         foreach ($map as $k => $v) {
  83.             $map[$k] = new ServiceClosureArgument($v);
  84.         }
  85.         $locator = (new Definition(ServiceLocator::class))
  86.             ->addArgument($map)
  87.             ->addTag('container.service_locator');
  88.         if (null !== $callerId && $container->hasDefinition($callerId)) {
  89.             $locator->setBindings($container->getDefinition($callerId)->getBindings());
  90.         }
  91.         if (!$container->hasDefinition($id '.service_locator.'.ContainerBuilder::hash($locator))) {
  92.             $container->setDefinition($id$locator);
  93.         }
  94.         if (null !== $callerId) {
  95.             $locatorId $id;
  96.             // Locators are shared when they hold the exact same list of factories;
  97.             // to have them specialized per consumer service, we use a cloning factory
  98.             // to derivate customized instances from the prototype one.
  99.             $container->register($id .= '.'.$callerIdServiceLocator::class)
  100.                 ->setFactory([new Reference($locatorId), 'withContext'])
  101.                 ->addTag('container.service_locator_context', ['id' => $callerId])
  102.                 ->addArgument($callerId)
  103.                 ->addArgument(new Reference('service_container'));
  104.         }
  105.         return new Reference($id);
  106.     }
  107. }