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\Bundle\FrameworkBundle\Controller\AbstractController;
15 use Symfony\Component\DependencyInjection\ChildDefinition;
16 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17 use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
18 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22 use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
23 use Symfony\Component\DependencyInjection\Reference;
24 use Symfony\Component\DependencyInjection\TypedReference;
25 use Symfony\Component\HttpFoundation\Request;
28 * Creates the service-locators required by ServiceValueResolver.
30 * @author Nicolas Grekas <p@tchwork.com>
32 class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface
34 private $resolverServiceId;
35 private $controllerTag;
37 public function __construct($resolverServiceId = 'argument_resolver.service', $controllerTag = 'controller.service_arguments')
39 $this->resolverServiceId = $resolverServiceId;
40 $this->controllerTag = $controllerTag;
43 public function process(ContainerBuilder $container)
45 if (false === $container->hasDefinition($this->resolverServiceId)) {
49 $parameterBag = $container->getParameterBag();
50 $controllers = array();
52 foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) {
53 $def = $container->getDefinition($id);
54 $def->setPublic(true);
55 $class = $def->getClass();
56 $autowire = $def->isAutowired();
57 $bindings = $def->getBindings();
59 // resolve service class, taking parent definitions into account
60 while ($def instanceof ChildDefinition) {
61 $def = $container->findDefinition($def->getParent());
62 $class = $class ?: $def->getClass();
63 $bindings = $def->getBindings();
65 $class = $parameterBag->resolveValue($class);
67 if (!$r = $container->getReflectionClass($class)) {
68 throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
70 $isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);
72 // get regular public methods
75 foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
76 if ('setContainer' === $r->name && $isContainerAware) {
79 if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
80 $methods[strtolower($r->name)] = array($r, $r->getParameters());
84 // validate and collect explicit per-actions and per-arguments service references
85 foreach ($tags as $attributes) {
86 if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) {
90 foreach (array('action', 'argument', 'id') as $k) {
91 if (!isset($attributes[$k][0])) {
92 throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, JSON_UNESCAPED_UNICODE), $id));
95 if (!isset($methods[$action = strtolower($attributes['action'])])) {
96 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));
98 list($r, $parameters) = $methods[$action];
101 foreach ($parameters as $p) {
102 if ($attributes['argument'] === $p->name) {
103 if (!isset($arguments[$r->name][$p->name])) {
104 $arguments[$r->name][$p->name] = $attributes['id'];
112 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));
116 foreach ($methods as list($r, $parameters)) {
117 /** @var \ReflectionMethod $r */
119 // create a per-method map of argument-names to service/type-references
121 foreach ($parameters as $p) {
122 /** @var \ReflectionParameter $p */
123 $type = $target = ProxyHelper::getTypeHint($r, $p, true);
124 $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
126 if (isset($arguments[$r->name][$p->name])) {
127 $target = $arguments[$r->name][$p->name];
128 if ('?' !== $target[0]) {
129 $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
130 } elseif ('' === $target = (string) substr($target, 1)) {
131 throw new InvalidArgumentException(sprintf('A "%s" tag must have non-empty "id" attributes for service "%s".', $this->controllerTag, $id));
132 } elseif ($p->allowsNull() && !$p->isOptional()) {
133 $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
135 } elseif (isset($bindings[$bindingName = '$'.$p->name]) || isset($bindings[$bindingName = $type])) {
136 $binding = $bindings[$bindingName];
138 list($bindingValue, $bindingId) = $binding->getValues();
140 if (!$bindingValue instanceof Reference) {
144 $binding->setValues(array($bindingValue, $bindingId, true));
145 $args[$p->name] = $bindingValue;
148 } elseif (!$type || !$autowire) {
152 if (Request::class === $type) {
156 if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) {
157 $message = sprintf('Cannot determine controller argument for "%s::%s()": the $%s argument is type-hinted with the non-existent class or interface: "%s".', $class, $r->name, $p->name, $type);
159 // see if the type-hint lives in the same namespace as the controller
160 if (0 === strncmp($type, $class, strrpos($class, '\\'))) {
161 $message .= ' Did you forget to add a use statement?';
164 throw new InvalidArgumentException($message);
167 $args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior);
169 // register the maps as a per-method service-locators
171 $controllers[$id.':'.$r->name] = ServiceLocatorTagPass::register($container, $args);
176 $container->getDefinition($this->resolverServiceId)
177 ->replaceArgument(0, ServiceLocatorTagPass::register($container, $controllers));