annotate vendor/symfony/http-kernel/DependencyInjection/RemoveEmptyControllerArgumentLocatorsPass.php @ 0:c75dbcec494b

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