| 148 |
lars |
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 |
public function process(ContainerBuilder $container)
|
|
|
25 |
{
|
|
|
26 |
$controllerLocator = $container->findDefinition('argument_resolver.controller_locator');
|
|
|
27 |
$controllers = $controllerLocator->getArgument(0);
|
|
|
28 |
|
|
|
29 |
foreach ($controllers as $controller => $argumentRef) {
|
|
|
30 |
$argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
|
|
|
31 |
|
|
|
32 |
if (!$argumentLocator->getArgument(0)) {
|
|
|
33 |
// remove empty argument locators
|
|
|
34 |
$reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller);
|
|
|
35 |
} else {
|
|
|
36 |
// any methods listed for call-at-instantiation cannot be actions
|
|
|
37 |
$reason = false;
|
|
|
38 |
[$id, $action] = explode('::', $controller);
|
|
|
39 |
|
|
|
40 |
if ($container->hasAlias($id)) {
|
|
|
41 |
continue;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
$controllerDef = $container->getDefinition($id);
|
|
|
45 |
foreach ($controllerDef->getMethodCalls() as [$method]) {
|
|
|
46 |
if (0 === strcasecmp($action, $method)) {
|
|
|
47 |
$reason = sprintf('Removing method "%s" of service "%s" from controller candidates: the method is called at instantiation, thus cannot be an action.', $action, $id);
|
|
|
48 |
break;
|
|
|
49 |
}
|
|
|
50 |
}
|
|
|
51 |
if (!$reason) {
|
|
|
52 |
// see Symfony\Component\HttpKernel\Controller\ContainerControllerResolver
|
|
|
53 |
$controllers[$id.':'.$action] = $argumentRef;
|
|
|
54 |
|
|
|
55 |
if ('__invoke' === $action) {
|
|
|
56 |
$controllers[$id] = $argumentRef;
|
|
|
57 |
}
|
|
|
58 |
continue;
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
unset($controllers[$controller]);
|
|
|
63 |
$container->log($this, $reason);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
$controllerLocator->replaceArgument(0, $controllers);
|
|
|
67 |
}
|
|
|
68 |
}
|