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

151 lines
4.9 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\Exception\InvalidArgumentException;
2011-01-07 14:44:29 +00:00
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
*
* @throws InvalidArgumentException if the service definition does not exist
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;
try {
$definition = $container->getDefinition($aliasId);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(sprintf('Unable to replace alias "%s" with actual definition "%s".', $id, $alias), null, $e);
}
2011-01-07 14:44:29 +00:00
if ($definition->isPublic()) {
continue;
}
$definition->setPublic(true);
$container->setDefinition($id, $definition);
$container->removeDefinition($aliasId);
2011-01-07 14:44:29 +00:00
$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
2012-05-15 17:56:32 +01:00
* @param string $currentId The alias identifier being replaced
* @param string $newId The id of the service the alias points to
2011-02-13 18:06:41 +00:00
*/
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)
);
2016-02-20 19:14:11 +00:00
$definition->setFactoryService($this->updateFactoryServiceReference($definition->getFactoryService(), $currentId, $newId));
$definition->setFactory($this->updateFactoryReference($definition->getFactory(), $currentId, $newId));
2011-01-07 14:44:29 +00:00
}
}
2011-02-13 18:06:41 +00:00
/**
* Updates argument references.
*
2012-05-15 17:56:32 +01:00
* @param array $arguments An array of Arguments
2011-02-13 18:06:41 +00:00
* @param string $currentId The alias identifier
2012-05-15 17:56:32 +01:00
* @param string $newId The identifier the alias points to
*
* @return array
2011-02-13 18:06:41 +00:00
*/
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);
2011-12-18 13:42:59 +00:00
} elseif ($argument instanceof Reference) {
2011-01-07 14:44:29 +00:00
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;
}
2016-02-20 19:14:11 +00:00
private function updateFactoryServiceReference($factoryService, $currentId, $newId)
{
if (null === $factoryService) {
return;
}
return $currentId === $factoryService ? $newId : $currentId;
}
private function updateFactoryReference($factory, $currentId, $newId)
{
if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
return $factory;
}
if ($currentId === (string) $factory[0]) {
$factory[0] = new Reference($newId, $factory[0]->getInvalidBehavior());
}
return $factory;
}
}