vendor/symfony/dependency-injection/Compiler/ResolveReferencesToAliasesPass.php line 56

  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\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. /**
  15.  * Replaces all references to aliases with references to the actual service.
  16.  *
  17.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18.  */
  19. class ResolveReferencesToAliasesPass extends AbstractRecursivePass
  20. {
  21.     protected bool $skipScalars true;
  22.     /**
  23.      * @return void
  24.      */
  25.     public function process(ContainerBuilder $container)
  26.     {
  27.         parent::process($container);
  28.         foreach ($container->getAliases() as $id => $alias) {
  29.             $aliasId = (string) $alias;
  30.             $this->currentId $id;
  31.             if ($aliasId !== $defId $this->getDefinitionId($aliasId$container)) {
  32.                 $container->setAlias($id$defId)->setPublic($alias->isPublic());
  33.             }
  34.         }
  35.     }
  36.     protected function processValue(mixed $valuebool $isRoot false): mixed
  37.     {
  38.         if (!$value instanceof Reference) {
  39.             return parent::processValue($value$isRoot);
  40.         }
  41.         $defId $this->getDefinitionId($id = (string) $value$this->container);
  42.         return $defId !== $id ? new Reference($defId$value->getInvalidBehavior()) : $value;
  43.     }
  44.     private function getDefinitionId(string $idContainerBuilder $container): string
  45.     {
  46.         if (!$container->hasAlias($id)) {
  47.             return $id;
  48.         }
  49.         $alias $container->getAlias($id);
  50.         if ($alias->isDeprecated()) {
  51.             $referencingDefinition $container->hasDefinition($this->currentId) ? $container->getDefinition($this->currentId) : $container->getAlias($this->currentId);
  52.             if (!$referencingDefinition->isDeprecated()) {
  53.                 $deprecation $alias->getDeprecation($id);
  54.                 trigger_deprecation($deprecation['package'], $deprecation['version'], rtrim($deprecation['message'], '. ').'. It is being referenced by the "%s" '.($container->hasDefinition($this->currentId) ? 'service.' 'alias.'), $this->currentId);
  55.             }
  56.         }
  57.         $seen = [];
  58.         do {
  59.             if (isset($seen[$id])) {
  60.                 throw new ServiceCircularReferenceException($idarray_merge(array_keys($seen), [$id]));
  61.             }
  62.             $seen[$id] = true;
  63.             $id = (string) $container->getAlias($id);
  64.         } while ($container->hasAlias($id));
  65.         return $id;
  66.     }
  67. }