This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/DependencyInjection/Compiler/ResolveReferencesToAliasesPass.php
Nicolas Grekas 7fb9f614ee Merge branch '2.8' into 3.3
* 2.8:
  [DI] minor docblock fixes
2017-10-24 16:05:06 +02:00

78 lines
2.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 Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Replaces all references to aliases with references to the actual service.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveReferencesToAliasesPass extends AbstractRecursivePass
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
parent::process($container);
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
}
}
}
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
if ($value instanceof Reference) {
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
if ($defId !== $id) {
return new Reference($defId, $value->getInvalidBehavior());
}
}
return parent::processValue($value);
}
/**
* Resolves an alias into a definition id.
*
* @param string $id The definition or alias id to resolve
* @param ContainerBuilder $container
*
* @return string The definition id with aliases resolved
*/
private function getDefinitionId($id, ContainerBuilder $container)
{
$seen = array();
while ($container->hasAlias($id)) {
if (isset($seen[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($seen));
}
$seen[$id] = true;
$id = (string) $container->getAlias($id);
}
return $id;
}
}