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

91 lines
2.7 KiB
PHP
Raw Normal View History

2011-01-05 11:13:27 +00:00
<?php
/*
* This file is part of the Symfony package.
2011-01-05 11:13:27 +00:00
*
* (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-05 11:13:27 +00:00
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2011-01-05 11:13:27 +00:00
/**
* Replaces all references to aliases with references to the actual service.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ResolveReferencesToAliasesPass implements CompilerPassInterface
{
protected $container;
2011-02-13 18:06:41 +00:00
/**
* Processes the ContainerBuilder to replace references to aliases with actual service references.
*
* @param ContainerBuilder $container
*/
2011-01-05 11:13:27 +00:00
public function process(ContainerBuilder $container)
{
$this->container = $container;
2011-02-27 19:56:29 +00:00
foreach ($container->getDefinitions() as $definition) {
2011-01-26 23:14:31 +00:00
if ($definition->isSynthetic() || $definition->isAbstract()) {
continue;
}
2011-01-05 11:13:27 +00:00
$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
}
2011-02-13 18:06:41 +00:00
/**
* Processes the arguments to replace aliases
*
* @param array $arguments An array of References
* @return array An array of References
*/
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);
2011-02-27 19:56:29 +00:00
} elseif ($argument instanceof Reference) {
2011-01-05 11:13:27 +00:00
$defId = $this->getDefinitionId($id = (string) $argument);
if ($defId !== $id) {
$arguments[$k] = new Reference($defId, $argument->getInvalidBehavior(), $argument->isStrict());
2011-01-05 11:13:27 +00:00
}
}
}
return $arguments;
}
2011-02-13 18:06:41 +00:00
/**
* Resolve an alias into a definition id
*
* @param string $id The definition or alias id to resolve
* @return string The definition id with aliases resolved
*/
2011-01-05 11:13:27 +00:00
protected function getDefinitionId($id)
{
while ($this->container->hasAlias($id)) {
$id = (string) $this->container->getAlias($id);
2011-01-05 11:13:27 +00:00
}
return $id;
}
}