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/ReplaceAliasByActualDefinitionPass.php

117 lines
3.6 KiB
PHP
Raw Normal View History

2011-01-07 14:44:29 +00:00
<?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.
*/
2011-01-07 14:44:29 +00:00
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Replaces aliases with actual service definitions, effectively removing these
* aliases.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ReplaceAliasByActualDefinitionPass implements CompilerPassInterface
{
private $compiler;
private $formatter;
private $sourceId;
2011-02-13 18:06:41 +00:00
/**
* Process the Container to replace aliases with service definitions.
*
* @param ContainerBuilder $container
2011-02-13 18:06:41 +00:00
*/
2011-01-07 14:44:29 +00:00
public function process(ContainerBuilder $container)
{
$this->compiler = $container->getCompiler();
$this->formatter = $this->compiler->getLoggingFormatter();
2011-01-07 14:44:29 +00:00
foreach ($container->getAliases() as $id => $alias) {
$aliasId = (string) $alias;
$definition = $container->getDefinition($aliasId);
2011-01-07 14:44:29 +00:00
if ($definition->isPublic()) {
continue;
}
$definition->setPublic(true);
$container->setDefinition($id, $definition);
$container->remove($aliasId);
$this->updateReferences($container, $aliasId, $id);
// we have to restart the process due to concurrent modification of
// the container
$this->process($container);
break;
}
}
2011-02-13 18:06:41 +00:00
/**
2011-02-26 17:43:28 +00:00
* Updates references to remove aliases.
2011-02-13 18:06:41 +00:00
*
* @param ContainerBuilder $container The container
* @param string $currentId The alias identifier being replaced
* @param string $newId The id of the service the alias points to
*/
private function updateReferences($container, $currentId, $newId)
2011-01-07 14:44:29 +00:00
{
foreach ($container->getAliases() as $id => $alias) {
if ($currentId === (string) $alias) {
$container->setAlias($id, $newId);
}
}
foreach ($container->getDefinitions() as $id => $definition) {
$this->sourceId = $id;
2011-01-07 14:44:29 +00:00
$definition->setArguments(
$this->updateArgumentReferences($definition->getArguments(), $currentId, $newId)
);
$definition->setMethodCalls(
$this->updateArgumentReferences($definition->getMethodCalls(), $currentId, $newId)
);
$definition->setProperties(
$this->updateArgumentReferences($definition->getProperties(), $currentId, $newId)
);
2011-01-07 14:44:29 +00:00
}
}
2011-02-13 18:06:41 +00:00
/**
* Updates argument references.
*
* @param array $arguments An array of Arguments
* @param string $currentId The alias identifier
* @param string $newId The identifier the alias points to
*/
private function updateArgumentReferences(array $arguments, $currentId, $newId)
2011-01-07 14:44:29 +00:00
{
foreach ($arguments as $k => $argument) {
if (is_array($argument)) {
$arguments[$k] = $this->updateArgumentReferences($argument, $currentId, $newId);
} else if ($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));
2011-01-07 14:44:29 +00:00
}
}
}
return $arguments;
}
}