Updated to Drupal 8.5. Core Media not yet in use.
[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 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                 $private = $alias->isPrivate();
53                 $container->setAlias($renamedId, new Alias($container->normalizeId($alias), false));
54             } else {
55                 $decoratedDefinition = $container->getDefinition($inner);
56                 $definition->setTags(array_merge($decoratedDefinition->getTags(), $definition->getTags()));
57                 if ($types = array_merge($decoratedDefinition->getAutowiringTypes(false), $definition->getAutowiringTypes(false))) {
58                     $definition->setAutowiringTypes($types);
59                 }
60                 $public = $decoratedDefinition->isPublic();
61                 $private = $decoratedDefinition->isPrivate();
62                 $decoratedDefinition->setPublic(false);
63                 $decoratedDefinition->setTags(array());
64                 if ($decoratedDefinition->getAutowiringTypes(false)) {
65                     $decoratedDefinition->setAutowiringTypes(array());
66                 }
67                 $container->setDefinition($renamedId, $decoratedDefinition);
68             }
69
70             $container->setAlias($inner, $id)->setPublic($public)->setPrivate($private);
71         }
72     }
73 }