2011-01-05 11:13:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2011-01-15 13:29:43 +00:00
|
|
|
* This file is part of the Symfony package.
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-01-05 11:13:27 +00:00
|
|
|
*
|
2011-01-15 13:29:43 +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
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\DependencyInjection\Compiler;
|
|
|
|
|
2014-10-25 14:19:21 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
2011-01-15 13:29:43 +00:00
|
|
|
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)) {
|
2017-09-17 10:49:41 +01:00
|
|
|
$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
|
|
|
{
|
2014-10-25 14:19:21 +01:00
|
|
|
$seen = array();
|
2017-09-13 12:10:11 +01:00
|
|
|
while ($container->hasAlias($id)) {
|
2014-10-25 14:19:21 +01:00
|
|
|
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;
|
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|