Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

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

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) {
  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 (!$v instanceof Reference) {
  58.                 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));
  59.             }
  60.             if ($i === $k) {
  61.                 unset($services[$k]);
  62.                 $k = (string) $v;
  63.                 ++$i;
  64.             } elseif (\is_int($k)) {
  65.                 $i null;
  66.             }
  67.             $services[$k] = new ServiceClosureArgument($v);
  68.         }
  69.         ksort($services);
  70.         $value->setArgument(0$services);
  71.         $id '.service_locator.'.ContainerBuilder::hash($value);
  72.         if ($isRoot) {
  73.             if ($id !== $this->currentId) {
  74.                 $this->container->setAlias($id, new Alias($this->currentIdfalse));
  75.             }
  76.             return $value;
  77.         }
  78.         $this->container->setDefinition($id$value->setPublic(false));
  79.         return new Reference($id);
  80.     }
  81.     /**
  82.      * @param Reference[] $refMap
  83.      */
  84.     public static function register(ContainerBuilder $container, array $refMapstring $callerId null): Reference
  85.     {
  86.         foreach ($refMap as $id => $ref) {
  87.             if (!$ref instanceof Reference) {
  88.                 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));
  89.             }
  90.             $refMap[$id] = new ServiceClosureArgument($ref);
  91.         }
  92.         $locator = (new Definition(ServiceLocator::class))
  93.             ->addArgument($refMap)
  94.             ->addTag('container.service_locator');
  95.         if (null !== $callerId && $container->hasDefinition($callerId)) {
  96.             $locator->setBindings($container->getDefinition($callerId)->getBindings());
  97.         }
  98.         if (!$container->hasDefinition($id '.service_locator.'.ContainerBuilder::hash($locator))) {
  99.             $container->setDefinition($id$locator);
  100.         }
  101.         if (null !== $callerId) {
  102.             $locatorId $id;
  103.             // Locators are shared when they hold the exact same list of factories;
  104.             // to have them specialized per consumer service, we use a cloning factory
  105.             // to derivate customized instances from the prototype one.
  106.             $container->register($id .= '.'.$callerIdServiceLocator::class)
  107.                 ->setFactory([new Reference($locatorId), 'withContext'])
  108.                 ->addTag('container.service_locator_context', ['id' => $callerId])
  109.                 ->addArgument($callerId)
  110.                 ->addArgument(new Reference('service_container'));
  111.         }
  112.         return new Reference($id);
  113.     }
  114. }