2011-01-07 14:44:29 +00:00
|
|
|
<?php
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.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
|
|
|
|
{
|
2011-02-13 18:06:41 +00:00
|
|
|
/**
|
|
|
|
* Process the Container to replace aliases with service definitions.
|
|
|
|
*
|
|
|
|
* @param ContainerBuilder $container
|
|
|
|
*/
|
2011-01-07 14:44:29 +00:00
|
|
|
public function process(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
foreach ($container->getAliases() as $id => $alias) {
|
|
|
|
$aliasId = (string) $alias;
|
|
|
|
|
2011-02-04 18:10:13 +00:00
|
|
|
$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
|
|
|
|
*/
|
2011-01-07 14:44:29 +00:00
|
|
|
protected function updateReferences($container, $currentId, $newId)
|
|
|
|
{
|
|
|
|
foreach ($container->getAliases() as $id => $alias) {
|
|
|
|
if ($currentId === (string) $alias) {
|
|
|
|
$container->setAlias($id, $newId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($container->getDefinitions() as $definition) {
|
|
|
|
$definition->setArguments(
|
|
|
|
$this->updateArgumentReferences($definition->getArguments(), $currentId, $newId)
|
|
|
|
);
|
|
|
|
|
|
|
|
$definition->setMethodCalls(
|
|
|
|
$this->updateArgumentReferences($definition->getMethodCalls(), $currentId, $newId)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2011-01-07 14:44:29 +00:00
|
|
|
protected function updateArgumentReferences(array $arguments, $currentId, $newId)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $arguments;
|
|
|
|
}
|
2011-02-04 18:10:13 +00:00
|
|
|
}
|