|
| 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\Argument\ServiceClosureArgument; |
| 15 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 16 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 17 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
| 18 | +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
| 19 | +use Symfony\Component\DependencyInjection\LazyProxy\InheritanceProxyHelper; |
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
| 21 | +use Symfony\Component\DependencyInjection\ServiceLocator; |
| 22 | +use Symfony\Component\DependencyInjection\TypedReference; |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates the service-locators required by ServiceArgumentValueResolver. |
| 26 | + * |
| 27 | + * @author Nicolas Grekas <p@tchwork.com> |
| 28 | + */ |
| 29 | +class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface |
| 30 | +{ |
| 31 | + private $resolverServiceId; |
| 32 | + private $controllerTag; |
| 33 | + |
| 34 | + public function __construct($resolverServiceId = 'argument_resolver.service', $controllerTag = 'controller.service_arguments') |
| 35 | + { |
| 36 | + $this->resolverServiceId = $resolverServiceId; |
| 37 | + $this->controllerTag = $controllerTag; |
| 38 | + } |
| 39 | + |
| 40 | + public function process(ContainerBuilder $container) |
| 41 | + { |
| 42 | + if (false === $container->hasDefinition($this->resolverServiceId)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $parameterBag = $container->getParameterBag(); |
| 47 | + $controllers = array(); |
| 48 | + |
| 49 | + foreach ($container->findTaggedServiceIds($this->controllerTag) as $id => $tags) { |
| 50 | + $def = $container->getDefinition($id); |
| 51 | + |
| 52 | + if ($def->isAbstract()) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + $class = $def->getClass(); |
| 56 | + $isAutowired = $def->isAutowired(); |
| 57 | + |
| 58 | + // resolve service class, taking parent definitions into account |
| 59 | + while (!$class && $def instanceof ChildDefinition) { |
| 60 | + $def = $container->findDefinition($def->getParent()); |
| 61 | + $class = $def->getClass(); |
| 62 | + } |
| 63 | + $class = $parameterBag->resolveValue($class); |
| 64 | + |
| 65 | + if (!$r = $container->getReflectionClass($class)) { |
| 66 | + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id)); |
| 67 | + } |
| 68 | + |
| 69 | + // get regular public methods |
| 70 | + $methods = array(); |
| 71 | + $arguments = array(); |
| 72 | + foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) { |
| 73 | + if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) { |
| 74 | + $methods[strtolower($r->name)] = array($r, $r->getParameters()); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // validate and collect explicit per-actions and per-arguments service references |
| 79 | + foreach ($tags as $attributes) { |
| 80 | + if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['service'])) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + foreach (array('action', 'argument', 'service') as $k) { |
| 84 | + if (!isset($attributes[$k][0])) { |
| 85 | + throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, JSON_UNESCAPED_UNICODE), $id)); |
| 86 | + } |
| 87 | + } |
| 88 | + if (!isset($methods[$action = strtolower($attributes['action'])])) { |
| 89 | + throw new InvalidArgumentException(sprintf('Invalid "action" attribute on tag "%s" for service "%s": no public "%s()" method found on class "%s".', $this->controllerTag, $id, $attributes['action'], $class)); |
| 90 | + } |
| 91 | + list($r, $parameters) = $methods[$action]; |
| 92 | + $found = false; |
| 93 | + |
| 94 | + foreach ($parameters as $p) { |
| 95 | + if ($attributes['argument'] === $p->name) { |
| 96 | + if (!isset($arguments[$r->name][$p->name])) { |
| 97 | + $arguments[$r->name][$p->name] = $attributes['service']; |
| 98 | + } |
| 99 | + $found = true; |
| 100 | + break; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if (!$found) { |
| 105 | + throw new InvalidArgumentException(sprintf('Invalid "%s" tag for service "%s": method "%s()" has no "%s" argument on class "%s".', $this->controllerTag, $id, $r->name, $attributes['argument'], $class)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + foreach ($methods as list($r, $parameters)) { |
| 110 | + // create a per-method map of argument-names to service/type-references |
| 111 | + $args = array(); |
| 112 | + foreach ($parameters as $p) { |
| 113 | + $type = $target = InheritanceProxyHelper::getTypeHint($r, $p, true); |
| 114 | + $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE; |
| 115 | + |
| 116 | + if (isset($arguments[$r->name][$p->name])) { |
| 117 | + $target = $arguments[$r->name][$p->name]; |
| 118 | + if ('?' !== $target[0]) { |
| 119 | + $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE; |
| 120 | + } elseif ('' === $target = (string) substr($target, 1)) { |
| 121 | + throw new InvalidArgumentException(sprintf('A "%s" tag must have non-empty "service" attributes for service "%s".', $this->controllerTag, $id)); |
| 122 | + } elseif ($p->allowsNull() && !$p->isOptional()) { |
| 123 | + $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE; |
| 124 | + } |
| 125 | + } elseif (!$type) { |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + $args[$p->name] = new ServiceClosureArgument($type ? new TypedReference($target, $type, $invalidBehavior, false) : new Reference($target, $invalidBehavior)); |
| 130 | + } |
| 131 | + // register the maps as a per-method service-locators |
| 132 | + if ($args) { |
| 133 | + $argsId = sprintf('arguments.%s:%s', $id, $r->name); |
| 134 | + $container->register($argsId, ServiceLocator::class) |
| 135 | + ->addArgument($args) |
| 136 | + ->setPublic(false) |
| 137 | + ->setAutowired($isAutowired) |
| 138 | + ->addTag('controller.arguments_locator', array($class, $id, $r->name)); |
| 139 | + $controllers[$id.':'.$r->name] = new Reference($argsId); |
| 140 | + if ($id === $class) { |
| 141 | + $controllers[$id.'::'.$r->name] = new Reference($argsId); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + $container->getDefinition($this->resolverServiceId) |
| 148 | + ->getArgument(0) |
| 149 | + ->setValues($controllers); |
| 150 | + } |
| 151 | +} |
0 commit comments