Version 1
[yaffs-website] / vendor / symfony / dependency-injection / Compiler / DecoratorServicePass.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\ContainerBuilder;
15 use Symfony\Component\DependencyInjection\Alias;
16
17 /**
18  * Overwrites a service but keeps the overridden one.
19  *
20  * @author Christophe Coevoet <stof@notk.org>
21  * @author Fabien Potencier <fabien@symfony.com>
22  * @author Diego Saint Esteben <diego@saintesteben.me>
23  */
24 class DecoratorServicePass implements CompilerPassInterface
25 {
26     public function process(ContainerBuilder $container)
27     {
28         $definitions = new \SplPriorityQueue();
29         $order = PHP_INT_MAX;
30
31         foreach ($container->getDefinitions() as $id => $definition) {
32             if (!$decorated = $definition->getDecoratedService()) {
33                 continue;
34             }
35             $definitions->insert(array($id, $definition), array($decorated[2], --$order));
36         }
37
38         foreach ($definitions as $arr) {
39             list($id, $definition) = $arr;
40             list($inner, $renamedId) = $definition->getDecoratedService();
41
42             $definition->setDecoratedService(null);
43
44             if (!$renamedId) {
45                 $renamedId = $id.'.inner';
46             }
47
48             // we create a new alias/service for the service we are replacing
49             // to be able to reference it in the new one
50             if ($container->hasAlias($inner)) {
51                 $alias = $container->getAlias($inner);
52                 $public = $alias->isPublic();
53                 $container->setAlias($renamedId, new Alias((string) $alias, false));
54             } else {
55                 $decoratedDefinition = $container->getDefinition($inner);
56                 $definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
57                 $definition->setAutowiringTypes(array_merge($decoratedDefinition->getAutowiringTypes(), $definition->getAutowiringTypes()));
58                 $public = $decoratedDefinition->isPublic();
59                 $decoratedDefinition->setPublic(false);
60                 $decoratedDefinition->setTags(array());
61                 $decoratedDefinition->setAutowiringTypes(array());
62                 $container->setDefinition($renamedId, $decoratedDefinition);
63             }
64
65             $container->setAlias($inner, new Alias($id, $public));
66         }
67     }
68 }