Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\HttpKernel\DependencyInjection;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
Chris@14
|
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
|
Chris@14
|
16
|
Chris@14
|
17 /**
|
Chris@14
|
18 * Removes empty service-locators registered for ServiceValueResolver.
|
Chris@14
|
19 *
|
Chris@14
|
20 * @author Nicolas Grekas <p@tchwork.com>
|
Chris@14
|
21 */
|
Chris@14
|
22 class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface
|
Chris@14
|
23 {
|
Chris@14
|
24 private $resolverServiceId;
|
Chris@14
|
25
|
Chris@14
|
26 public function __construct($resolverServiceId = 'argument_resolver.service')
|
Chris@14
|
27 {
|
Chris@14
|
28 $this->resolverServiceId = $resolverServiceId;
|
Chris@14
|
29 }
|
Chris@14
|
30
|
Chris@14
|
31 public function process(ContainerBuilder $container)
|
Chris@14
|
32 {
|
Chris@14
|
33 if (false === $container->hasDefinition($this->resolverServiceId)) {
|
Chris@14
|
34 return;
|
Chris@14
|
35 }
|
Chris@14
|
36
|
Chris@14
|
37 $serviceResolver = $container->getDefinition($this->resolverServiceId);
|
Chris@14
|
38 $controllerLocator = $container->getDefinition((string) $serviceResolver->getArgument(0));
|
Chris@14
|
39 $controllers = $controllerLocator->getArgument(0);
|
Chris@14
|
40
|
Chris@14
|
41 foreach ($controllers as $controller => $argumentRef) {
|
Chris@14
|
42 $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
|
Chris@14
|
43
|
Chris@14
|
44 if (!$argumentLocator->getArgument(0)) {
|
Chris@14
|
45 // remove empty argument locators
|
Chris@14
|
46 $reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller);
|
Chris@14
|
47 } else {
|
Chris@14
|
48 // any methods listed for call-at-instantiation cannot be actions
|
Chris@14
|
49 $reason = false;
|
Chris@14
|
50 $action = substr(strrchr($controller, ':'), 1);
|
Chris@17
|
51 $id = substr($controller, 0, -1 - \strlen($action));
|
Chris@14
|
52 $controllerDef = $container->getDefinition($id);
|
Chris@14
|
53 foreach ($controllerDef->getMethodCalls() as list($method)) {
|
Chris@14
|
54 if (0 === strcasecmp($action, $method)) {
|
Chris@14
|
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);
|
Chris@14
|
56 break;
|
Chris@14
|
57 }
|
Chris@14
|
58 }
|
Chris@14
|
59 if (!$reason) {
|
Chris@14
|
60 if ($controllerDef->getClass() === $id) {
|
Chris@14
|
61 $controllers[$id.'::'.$action] = $argumentRef;
|
Chris@14
|
62 }
|
Chris@14
|
63 if ('__invoke' === $action) {
|
Chris@14
|
64 $controllers[$id] = $argumentRef;
|
Chris@14
|
65 }
|
Chris@14
|
66 continue;
|
Chris@14
|
67 }
|
Chris@14
|
68 }
|
Chris@14
|
69
|
Chris@14
|
70 unset($controllers[$controller]);
|
Chris@14
|
71 $container->log($this, $reason);
|
Chris@14
|
72 }
|
Chris@14
|
73
|
Chris@14
|
74 $controllerLocator->replaceArgument(0, $controllers);
|
Chris@14
|
75 }
|
Chris@14
|
76 }
|