ab50cec3531bf01f146b97e2879aeb9537e832aa
[yaffs-website] / vendor / symfony / http-kernel / DependencyInjection / RemoveEmptyControllerArgumentLocatorsPass.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\HttpKernel\DependencyInjection;
13
14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17 /**
18  * Removes empty service-locators registered for ServiceValueResolver.
19  *
20  * @author Nicolas Grekas <p@tchwork.com>
21  */
22 class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface
23 {
24     private $resolverServiceId;
25
26     public function __construct($resolverServiceId = 'argument_resolver.service')
27     {
28         $this->resolverServiceId = $resolverServiceId;
29     }
30
31     public function process(ContainerBuilder $container)
32     {
33         if (false === $container->hasDefinition($this->resolverServiceId)) {
34             return;
35         }
36
37         $serviceResolver = $container->getDefinition($this->resolverServiceId);
38         $controllerLocator = $container->getDefinition((string) $serviceResolver->getArgument(0));
39         $controllers = $controllerLocator->getArgument(0);
40
41         foreach ($controllers as $controller => $argumentRef) {
42             $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
43
44             if (!$argumentLocator->getArgument(0)) {
45                 // remove empty argument locators
46                 $reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller);
47             } else {
48                 // any methods listed for call-at-instantiation cannot be actions
49                 $reason = false;
50                 $action = substr(strrchr($controller, ':'), 1);
51                 $id = substr($controller, 0, -1 - strlen($action));
52                 $controllerDef = $container->getDefinition($id);
53                 foreach ($controllerDef->getMethodCalls() as list($method)) {
54                     if (0 === strcasecmp($action, $method)) {
55                         $reason = sprintf('Removing method "%s" of service "%s" from controller candidates: the method is called at instantiation, thus cannot be an action.', $action, $id);
56                         break;
57                     }
58                 }
59                 if (!$reason) {
60                     if ($controllerDef->getClass() === $id) {
61                         $controllers[$id.'::'.$action] = $argumentRef;
62                     }
63                     if ('__invoke' === $action) {
64                         $controllers[$id] = $argumentRef;
65                     }
66                     continue;
67                 }
68             }
69
70             unset($controllers[$controller]);
71             $container->log($this, $reason);
72         }
73
74         $controllerLocator->replaceArgument(0, $controllers);
75     }
76 }