comparison vendor/symfony/dependency-injection/Compiler/DecoratorServicePass.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 list($id, $definition)) {
39 list($inner, $renamedId) = $definition->getDecoratedService();
40
41 $definition->setDecoratedService(null);
42
43 if (!$renamedId) {
44 $renamedId = $id.'.inner';
45 }
46
47 // we create a new alias/service for the service we are replacing
48 // to be able to reference it in the new one
49 if ($container->hasAlias($inner)) {
50 $alias = $container->getAlias($inner);
51 $public = $alias->isPublic();
52 $container->setAlias($renamedId, new Alias((string) $alias, false));
53 } else {
54 $decoratedDefinition = $container->getDefinition($inner);
55 $definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
56 $definition->setAutowiringTypes(array_merge($decoratedDefinition->getAutowiringTypes(), $definition->getAutowiringTypes()));
57 $public = $decoratedDefinition->isPublic();
58 $decoratedDefinition->setPublic(false);
59 $decoratedDefinition->setTags(array());
60 $decoratedDefinition->setAutowiringTypes(array());
61 $container->setDefinition($renamedId, $decoratedDefinition);
62 }
63
64 $container->setAlias($inner, new Alias($id, $public));
65 }
66 }
67 }