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
12namespace Symfony\Component\DependencyInjection\Compiler;
13
14use Symfony\Component\DependencyInjection\Alias;
15use Symfony\Component\DependencyInjection\ContainerBuilder;
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 */
24class 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([$id, $definition], [$decorated[2], --$order]);
36        }
37        $decoratingDefinitions = [];
38
39        foreach ($definitions as list($id, $definition)) {
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                $private = $alias->isPrivate();
54                $container->setAlias($renamedId, new Alias($container->normalizeId($alias), false));
55            } else {
56                $decoratedDefinition = $container->getDefinition($inner);
57                $public = $decoratedDefinition->isPublic();
58                $private = $decoratedDefinition->isPrivate();
59                $decoratedDefinition->setPublic(false);
60                $container->setDefinition($renamedId, $decoratedDefinition);
61                $decoratingDefinitions[$inner] = $decoratedDefinition;
62            }
63
64            if (isset($decoratingDefinitions[$inner])) {
65                $decoratingDefinition = $decoratingDefinitions[$inner];
66                $definition->setTags(array_merge($decoratingDefinition->getTags(), $definition->getTags()));
67                $autowiringTypes = $decoratingDefinition->getAutowiringTypes(false);
68                if ($types = array_merge($autowiringTypes, $definition->getAutowiringTypes(false))) {
69                    $definition->setAutowiringTypes($types);
70                }
71                $decoratingDefinition->setTags([]);
72                if ($autowiringTypes) {
73                    $decoratingDefinition->setAutowiringTypes([]);
74                }
75                $decoratingDefinitions[$inner] = $definition;
76            }
77
78            $container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
79        }
80    }
81}
82