831d9945369180b4dc058820a38e64a80f6cf0e9
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / ResolveReferencesToAliasesPass.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DependencyInjection\Compiler;
13
14 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
15 use Symfony\Component\DependencyInjection\Reference;
16 use Symfony\Component\DependencyInjection\ContainerBuilder;
17
18 /**
19  * Replaces all references to aliases with references to the actual service.
20  *
21  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
22  */
23 class ResolveReferencesToAliasesPass extends AbstractRecursivePass
24 {
25     /**
26      * {@inheritdoc}
27      */
28     public function process(ContainerBuilder $container)
29     {
30         parent::process($container);
31
32         foreach ($container->getAliases() as $id => $alias) {
33             $aliasId = $container->normalizeId($alias);
34             if ($aliasId !== $defId = $this->getDefinitionId($aliasId, $container)) {
35                 $container->setAlias($id, $defId)->setPublic($alias->isPublic())->setPrivate($alias->isPrivate());
36             }
37         }
38     }
39
40     /**
41      * {@inheritdoc}
42      */
43     protected function processValue($value, $isRoot = false)
44     {
45         if ($value instanceof Reference) {
46             $defId = $this->getDefinitionId($id = $this->container->normalizeId($value), $this->container);
47
48             if ($defId !== $id) {
49                 return new Reference($defId, $value->getInvalidBehavior());
50             }
51         }
52
53         return parent::processValue($value);
54     }
55
56     /**
57      * Resolves an alias into a definition id.
58      *
59      * @param string           $id        The definition or alias id to resolve
60      * @param ContainerBuilder $container
61      *
62      * @return string The definition id with aliases resolved
63      */
64     private function getDefinitionId($id, ContainerBuilder $container)
65     {
66         $seen = array();
67         while ($container->hasAlias($id)) {
68             if (isset($seen[$id])) {
69                 throw new ServiceCircularReferenceException($id, array_keys($seen));
70             }
71             $seen[$id] = true;
72             $id = $container->normalizeId($container->getAlias($id));
73         }
74
75         return $id;
76     }
77 }