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($valuebool $isRoot false)
  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 || !$value->hasTag('container.service_locator')) {
  37.             return parent::processValue($value$isRoot);
  38.         }
  39.         if (!$value->getClass()) {
  40.             $value->setClass(ServiceLocator::class);
  41.         }
  42.         $services $value->getArguments()[0] ?? null;
  43.         if ($services instanceof TaggedIteratorArgument) {
  44.             $services $this->findAndSortTaggedServices($services$this->container);
  45.         }
  46.         if (!\is_array($services)) {
  47.             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));
  48.         }
  49.         $i 0;
  50.         foreach ($services as $k => $v) {
  51.             if ($v instanceof ServiceClosureArgument) {
  52.                 continue;
  53.             }
  54.             if (!$v instanceof Reference) {
  55.                 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, "%s" found for key "%s".'$this->currentIdget_debug_type($v), $k));
  56.             }
  57.             if ($i === $k) {
  58.                 unset($services[$k]);
  59.                 $k = (string) $v;
  60.                 ++$i;
  61.             } elseif (\is_int($k)) {
  62.                 $i null;
  63.             }
  64.             $services[$k] = new ServiceClosureArgument($v);
  65.         }
  66.         ksort($services);
  67.         $value->setArgument(0$services);
  68.         $id '.service_locator.'.ContainerBuilder::hash($value);
  69.         if ($isRoot) {
  70.             if ($id !== $this->currentId) {
  71.                 $this->container->setAlias($id, new Alias($this->currentIdfalse));
  72.             }
  73.             return $value;
  74.         }
  75.         $this->container->setDefinition($id$value->setPublic(false));
  76.         return new Reference($id);
  77.     }
  78.     /**
  79.      * @param Reference[] $refMap
  80.      */
  81.     public static function register(ContainerBuilder $container, array $refMapstring $callerId null): Reference
  82.     {
  83.         foreach ($refMap as $id => $ref) {
  84.             if (!$ref instanceof Reference) {
  85.                 throw new InvalidArgumentException(sprintf('Invalid service locator definition: only services can be referenced, "%s" found for key "%s". Inject parameter values using constructors instead.'get_debug_type($ref), $id));
  86.             }
  87.             $refMap[$id] = new ServiceClosureArgument($ref);
  88.         }
  89.         $locator = (new Definition(ServiceLocator::class))
  90.             ->addArgument($refMap)
  91.             ->addTag('container.service_locator');
  92.         if (null !== $callerId && $container->hasDefinition($callerId)) {
  93.             $locator->setBindings($container->getDefinition($callerId)->getBindings());
  94.         }
  95.         if (!$container->hasDefinition($id '.service_locator.'.ContainerBuilder::hash($locator))) {
  96.             $container->setDefinition($id$locator);
  97.         }
  98.         if (null !== $callerId) {
  99.             $locatorId $id;
  100.             // Locators are shared when they hold the exact same list of factories;
  101.             // to have them specialized per consumer service, we use a cloning factory
  102.             // to derivate customized instances from the prototype one.
  103.             $container->register($id .= '.'.$callerIdServiceLocator::class)
  104.                 ->setFactory([new Reference($locatorId), 'withContext'])
  105.                 ->addTag('container.service_locator_context', ['id' => $callerId])
  106.                 ->addArgument($callerId)
  107.                 ->addArgument(new Reference('service_container'));
  108.         }
  109.         return new Reference($id);
  110.     }
  111. }