4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Compiler;
14 use Symfony\Component\DependencyInjection\Alias;
15 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
16 use Symfony\Component\DependencyInjection\Reference;
17 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 * Replaces all references to aliases with references to the actual service.
22 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24 class ResolveReferencesToAliasesPass implements CompilerPassInterface
29 * Processes the ContainerBuilder to replace references to aliases with actual service references.
31 * @param ContainerBuilder $container
33 public function process(ContainerBuilder $container)
35 $this->container = $container;
37 foreach ($container->getDefinitions() as $definition) {
38 if ($definition->isSynthetic() || $definition->isAbstract()) {
42 $definition->setArguments($this->processArguments($definition->getArguments()));
43 $definition->setMethodCalls($this->processArguments($definition->getMethodCalls()));
44 $definition->setProperties($this->processArguments($definition->getProperties()));
45 $definition->setFactory($this->processFactory($definition->getFactory()));
48 foreach ($container->getAliases() as $id => $alias) {
49 $aliasId = (string) $alias;
50 if ($aliasId !== $defId = $this->getDefinitionId($aliasId)) {
51 $container->setAlias($id, new Alias($defId, $alias->isPublic()));
57 * Processes the arguments to replace aliases.
59 * @param array $arguments An array of References
61 * @return array An array of References
63 private function processArguments(array $arguments)
65 foreach ($arguments as $k => $argument) {
66 if (is_array($argument)) {
67 $arguments[$k] = $this->processArguments($argument);
68 } elseif ($argument instanceof Reference) {
69 $defId = $this->getDefinitionId($id = (string) $argument);
72 $arguments[$k] = new Reference($defId, $argument->getInvalidBehavior());
80 private function processFactory($factory)
82 if (null === $factory || !is_array($factory) || !$factory[0] instanceof Reference) {
86 $defId = $this->getDefinitionId($id = (string) $factory[0]);
89 $factory[0] = new Reference($defId, $factory[0]->getInvalidBehavior());
96 * Resolves an alias into a definition id.
98 * @param string $id The definition or alias id to resolve
100 * @return string The definition id with aliases resolved
102 private function getDefinitionId($id)
105 while ($this->container->hasAlias($id)) {
106 if (isset($seen[$id])) {
107 throw new ServiceCircularReferenceException($id, array_keys($seen));
110 $id = (string) $this->container->getAlias($id);