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\HttpKernel\DependencyInjection;
14 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15 use Symfony\Component\DependencyInjection\ContainerBuilder;
18 * Removes empty service-locators registered for ServiceValueResolver.
20 * @author Nicolas Grekas <p@tchwork.com>
22 class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface
24 private $resolverServiceId;
26 public function __construct($resolverServiceId = 'argument_resolver.service')
28 $this->resolverServiceId = $resolverServiceId;
31 public function process(ContainerBuilder $container)
33 if (false === $container->hasDefinition($this->resolverServiceId)) {
37 $serviceResolver = $container->getDefinition($this->resolverServiceId);
38 $controllerLocator = $container->getDefinition((string) $serviceResolver->getArgument(0));
39 $controllers = $controllerLocator->getArgument(0);
41 foreach ($controllers as $controller => $argumentRef) {
42 $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]);
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);
48 // any methods listed for call-at-instantiation cannot be actions
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);
60 if ($controllerDef->getClass() === $id) {
61 $controllers[$id.'::'.$action] = $argumentRef;
63 if ('__invoke' === $action) {
64 $controllers[$id] = $argumentRef;
70 unset($controllers[$controller]);
71 $container->log($this, $reason);
74 $controllerLocator->replaceArgument(0, $controllers);