rector/vendor/symfony/dependency-injection/Compiler/ResolveReferencesToAliasesPass.php
Tomas Votruba a23b777a9a Updated Rector to commit 3a96b9097d6fbc29085210f2d85dd5d95755ef31
3a96b9097d [DeadCode] Skip fluent return  on RemoveEmptyMethodCallRector (#521)
2021-07-27 11:16:44 +00:00

74 lines
3.2 KiB
PHP

<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace RectorPrefix20210727\Symfony\Component\DependencyInjection\Compiler;
use RectorPrefix20210727\Symfony\Component\DependencyInjection\ContainerBuilder;
use RectorPrefix20210727\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use RectorPrefix20210727\Symfony\Component\DependencyInjection\Reference;
/**
* Replaces all references to aliases with references to the actual service.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveReferencesToAliasesPass extends \RectorPrefix20210727\Symfony\Component\DependencyInjection\Compiler\AbstractRecursivePass
{
/**
* {@inheritdoc}
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container
*/
public function process($container)
{
parent::process($container);
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
$this->currentId = $id;
if ($aliasId !== ($defId = $this->getDefinitionId($aliasId, $container))) {
$container->setAlias($id, $defId)->setPublic($alias->isPublic());
}
}
}
/**
* {@inheritdoc}
* @param bool $isRoot
*/
protected function processValue($value, $isRoot = \false)
{
if (!$value instanceof \RectorPrefix20210727\Symfony\Component\DependencyInjection\Reference) {
return parent::processValue($value, $isRoot);
}
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
return $defId !== $id ? new \RectorPrefix20210727\Symfony\Component\DependencyInjection\Reference($defId, $value->getInvalidBehavior()) : $value;
}
private function getDefinitionId(string $id, \RectorPrefix20210727\Symfony\Component\DependencyInjection\ContainerBuilder $container) : string
{
if (!$container->hasAlias($id)) {
return $id;
}
$alias = $container->getAlias($id);
if ($alias->isDeprecated()) {
$referencingDefinition = $container->hasDefinition($this->currentId) ? $container->getDefinition($this->currentId) : $container->getAlias($this->currentId);
if (!$referencingDefinition->isDeprecated()) {
$deprecation = $alias->getDeprecation($id);
trigger_deprecation($deprecation['package'], $deprecation['version'], \rtrim($deprecation['message'], '. ') . '. It is being referenced by the "%s" ' . ($container->hasDefinition($this->currentId) ? 'service.' : 'alias.'), $this->currentId);
}
}
$seen = [];
do {
if (isset($seen[$id])) {
throw new \RectorPrefix20210727\Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException($id, \array_merge(\array_keys($seen), [$id]));
}
$seen[$id] = \true;
$id = (string) $container->getAlias($id);
} while ($container->hasAlias($id));
return $id;
}
}