ReplaceAliasByActualDefinitionPass.php in Service Container 7
File
modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Compiler/ReplaceAliasByActualDefinitionPass.php
View source
<?php
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Reference;
class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface {
private $compiler;
private $formatter;
private $sourceId;
public function process(ContainerBuilder $container) {
$this->compiler = $container
->getCompiler();
$this->formatter = $this->compiler
->getLoggingFormatter();
foreach ($container
->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
try {
$definition = $container
->getDefinition($aliasId);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with "%s".', $alias, $id), null, $e);
}
if ($definition
->isPublic()) {
continue;
}
$definition
->setPublic(true);
$container
->setDefinition($id, $definition);
$container
->removeDefinition($aliasId);
$this
->updateReferences($container, $aliasId, $id);
$this
->process($container);
break;
}
}
private function updateReferences($container, $currentId, $newId) {
foreach ($container
->getAliases() as $id => $alias) {
if ($currentId === (string) $alias) {
$container
->setAlias($id, $newId);
}
}
foreach ($container
->getDefinitions() as $id => $definition) {
$this->sourceId = $id;
$definition
->setArguments($this
->updateArgumentReferences($definition
->getArguments(), $currentId, $newId));
$definition
->setMethodCalls($this
->updateArgumentReferences($definition
->getMethodCalls(), $currentId, $newId));
$definition
->setProperties($this
->updateArgumentReferences($definition
->getProperties(), $currentId, $newId));
}
}
private function updateArgumentReferences(array $arguments, $currentId, $newId) {
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this
->updateArgumentReferences($argument, $currentId, $newId);
}
elseif ($argument instanceof Reference) {
if ($currentId === (string) $argument) {
$arguments[$k] = new Reference($newId, $argument
->getInvalidBehavior());
$this->compiler
->addLogMessage($this->formatter
->formatUpdateReference($this, $this->sourceId, $currentId, $newId));
}
}
}
return $arguments;
}
}