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\ContainerBuilder;
18 * Overwrites a service but keeps the overridden one.
20 * @author Christophe Coevoet <stof@notk.org>
21 * @author Fabien Potencier <fabien@symfony.com>
22 * @author Diego Saint Esteben <diego@saintesteben.me>
24 class DecoratorServicePass implements CompilerPassInterface
26 public function process(ContainerBuilder $container)
28 $definitions = new \SplPriorityQueue();
31 foreach ($container->getDefinitions() as $id => $definition) {
32 if (!$decorated = $definition->getDecoratedService()) {
35 $definitions->insert(array($id, $definition), array($decorated[2], --$order));
37 $decoratingDefinitions = array();
39 foreach ($definitions as list($id, $definition)) {
40 list($inner, $renamedId) = $definition->getDecoratedService();
42 $definition->setDecoratedService(null);
45 $renamedId = $id.'.inner';
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));
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;
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);
71 $decoratingDefinition->setTags(array());
72 if ($autowiringTypes) {
73 $decoratingDefinition->setAutowiringTypes(array());
75 $decoratingDefinitions[$inner] = $definition;
78 $container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);