Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\HttpKernel\DependencyInjection; Chris@14: Chris@14: use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; Chris@14: use Symfony\Component\DependencyInjection\ContainerBuilder; Chris@14: Chris@14: /** Chris@14: * Removes empty service-locators registered for ServiceValueResolver. Chris@14: * Chris@14: * @author Nicolas Grekas
Chris@14: */ Chris@14: class RemoveEmptyControllerArgumentLocatorsPass implements CompilerPassInterface Chris@14: { Chris@14: private $resolverServiceId; Chris@14: Chris@14: public function __construct($resolverServiceId = 'argument_resolver.service') Chris@14: { Chris@14: $this->resolverServiceId = $resolverServiceId; Chris@14: } Chris@14: Chris@14: public function process(ContainerBuilder $container) Chris@14: { Chris@14: if (false === $container->hasDefinition($this->resolverServiceId)) { Chris@14: return; Chris@14: } Chris@14: Chris@14: $serviceResolver = $container->getDefinition($this->resolverServiceId); Chris@14: $controllerLocator = $container->getDefinition((string) $serviceResolver->getArgument(0)); Chris@14: $controllers = $controllerLocator->getArgument(0); Chris@14: Chris@14: foreach ($controllers as $controller => $argumentRef) { Chris@14: $argumentLocator = $container->getDefinition((string) $argumentRef->getValues()[0]); Chris@14: Chris@14: if (!$argumentLocator->getArgument(0)) { Chris@14: // remove empty argument locators Chris@14: $reason = sprintf('Removing service-argument resolver for controller "%s": no corresponding services exist for the referenced types.', $controller); Chris@14: } else { Chris@14: // any methods listed for call-at-instantiation cannot be actions Chris@14: $reason = false; Chris@14: $action = substr(strrchr($controller, ':'), 1); Chris@17: $id = substr($controller, 0, -1 - \strlen($action)); Chris@14: $controllerDef = $container->getDefinition($id); Chris@14: foreach ($controllerDef->getMethodCalls() as list($method)) { Chris@14: if (0 === strcasecmp($action, $method)) { Chris@14: $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: break; Chris@14: } Chris@14: } Chris@14: if (!$reason) { Chris@14: if ($controllerDef->getClass() === $id) { Chris@14: $controllers[$id.'::'.$action] = $argumentRef; Chris@14: } Chris@14: if ('__invoke' === $action) { Chris@14: $controllers[$id] = $argumentRef; Chris@14: } Chris@14: continue; Chris@14: } Chris@14: } Chris@14: Chris@14: unset($controllers[$controller]); Chris@14: $container->log($this, $reason); Chris@14: } Chris@14: Chris@14: $controllerLocator->replaceArgument(0, $controllers); Chris@14: } Chris@14: }