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

78 lines
2.2 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
/*
* This file is part of the Symfony package.
2011-01-05 11:13:27 +00:00
*
* (c) Fabien Potencier <fabien@symfony.com>
2011-01-05 11:13:27 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2011-01-05 11:13:27 +00:00
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2011-01-05 11:13:27 +00:00
/**
* Replaces all references to aliases with references to the actual service.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
2017-09-13 12:10:11 +01:00
class ResolveReferencesToAliasesPass extends AbstractRecursivePass
2011-01-05 11:13:27 +00:00
{
2011-02-13 18:06:41 +00:00
/**
2017-09-13 12:10:11 +01:00
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
2011-01-05 11:13:27 +00:00
public function process(ContainerBuilder $container)
{
2017-09-13 12:10:11 +01:00
parent::process($container);
2011-01-07 14:44:29 +00:00
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
2017-09-13 12:10:11 +01:00
if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
$container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
2011-01-07 14:44:29 +00:00
}
}
2011-01-05 11:13:27 +00:00
}
2011-02-13 18:06:41 +00:00
/**
2017-09-13 12:10:11 +01:00
* {@inheritdoc}
2011-02-13 18:06:41 +00:00
*/
2017-09-13 12:10:11 +01:00
protected function processValue($value, $isRoot = false)
2011-01-05 11:13:27 +00:00
{
2017-09-13 12:10:11 +01:00
if ($value instanceof Reference) {
$defId = $this->getDefinitionId($id = (string) $value, $this->container);
2011-01-05 11:13:27 +00:00
2017-09-13 12:10:11 +01:00
if ($defId !== $id) {
return new Reference($defId, $value->getInvalidBehavior());
2011-01-05 11:13:27 +00:00
}
}
2017-09-13 12:10:11 +01:00
return parent::processValue($value);
2016-01-27 07:44:59 +00:00
}
2011-02-13 18:06:41 +00:00
/**
2011-12-13 07:50:54 +00:00
* Resolves an alias into a definition id.
2011-02-13 18:06:41 +00:00
*
2017-09-13 12:10:11 +01:00
* @param string $id The definition or alias id to resolve
* @param ContainerBuilder $container
2011-12-13 07:50:54 +00:00
*
2011-02-13 18:06:41 +00:00
* @return string The definition id with aliases resolved
*/
2017-09-13 12:33:28 +01:00
private function getDefinitionId($id, ContainerBuilder $container)
2011-01-05 11:13:27 +00:00
{
$seen = array();
2017-09-13 12:10:11 +01:00
while ($container->hasAlias($id)) {
if (isset($seen[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($seen));
}
$seen[$id] = true;
2017-09-13 12:10:11 +01:00
$id = (string) $container->getAlias($id);
2011-01-05 11:13:27 +00:00
}
return $id;
}
}