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

70 lines
2.1 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
namespace Symfony\Component\DependencyInjection\Compiler;
2011-01-07 14:44:29 +00:00
use Symfony\Component\DependencyInjection\Alias;
2011-01-05 11:13:27 +00:00
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* Replaces all references to aliases with references to the actual service.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveReferencesToAliasesPass implements CompilerPassInterface
{
protected $container;
public function process(ContainerBuilder $container)
{
$this->container = $container;
foreach ($container->getDefinitions() as $id => $definition)
{
$definition->setArguments($this->processArguments($definition->getArguments()));
$definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
}
2011-01-07 14:44:29 +00:00
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
$container->setAlias($id, new Alias($defId, $alias->isPublic()));
}
}
2011-01-05 11:13:27 +00:00
}
protected function processArguments(array $arguments)
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this->processArguments($argument);
} else if ($argument instanceof Reference) {
$defId = $this->getDefinitionId($id = (string) $argument);
if ($defId !== $id) {
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
}
}
}
return $arguments;
}
protected function getDefinitionId($id)
{
if ($this->container->hasAlias($id)) {
2011-01-07 14:44:29 +00:00
return $this->getDefinitionId((string) $this->container->getAlias($id));
2011-01-05 11:13:27 +00:00
}
return $id;
}
}