RegisterControllerArgumentLocatorsPass.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Attribute\Autowire;
  12. use Symfony\Component\DependencyInjection\Attribute\AutowireCallable;
  13. use Symfony\Component\DependencyInjection\Attribute\Target;
  14. use Symfony\Component\DependencyInjection\ChildDefinition;
  15. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  16. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. use Symfony\Component\DependencyInjection\TypedReference;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  25. use Symfony\Component\VarExporter\ProxyHelper;
  26. /**
  27. * Creates the service-locators required by ServiceValueResolver.
  28. *
  29. * @author Nicolas Grekas <p@tchwork.com>
  30. */
  31. class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface
  32. {
  33. public function process(ContainerBuilder $container): void
  34. {
  35. if (!$container->hasDefinition('argument_resolver.service') && !$container->hasDefinition('argument_resolver.not_tagged_controller')) {
  36. return;
  37. }
  38. $parameterBag = $container->getParameterBag();
  39. $controllers = [];
  40. $controllerClasses = [];
  41. $publicAliases = [];
  42. foreach ($container->getAliases() as $id => $alias) {
  43. if ($alias->isPublic() && !$alias->isPrivate()) {
  44. $publicAliases[(string) $alias][] = $id;
  45. }
  46. }
  47. $emptyAutowireAttributes = class_exists(Autowire::class) ? null : [];
  48. foreach ($container->findTaggedServiceIds('controller.service_arguments', true) as $id => $tags) {
  49. $def = $container->getDefinition($id);
  50. $def->setPublic(true);
  51. $def->setLazy(false);
  52. $class = $def->getClass();
  53. $autowire = $def->isAutowired();
  54. $bindings = $def->getBindings();
  55. // resolve service class, taking parent definitions into account
  56. while ($def instanceof ChildDefinition) {
  57. $def = $container->findDefinition($def->getParent());
  58. $class = $class ?: $def->getClass();
  59. $bindings += $def->getBindings();
  60. }
  61. $class = $parameterBag->resolveValue($class);
  62. if (!$r = $container->getReflectionClass($class)) {
  63. throw new InvalidArgumentException(\sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  64. }
  65. $controllerClasses[] = $class;
  66. // get regular public methods
  67. $methods = [];
  68. $arguments = [];
  69. foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
  70. if ('setContainer' === $r->name) {
  71. continue;
  72. }
  73. if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
  74. $methods[strtolower($r->name)] = [$r, $r->getParameters()];
  75. }
  76. }
  77. // validate and collect explicit per-actions and per-arguments service references
  78. foreach ($tags as $attributes) {
  79. if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) {
  80. $autowire = true;
  81. continue;
  82. }
  83. foreach (['action', 'argument', 'id'] as $k) {
  84. if (!isset($attributes[$k][0])) {
  85. throw new InvalidArgumentException(\sprintf('Missing "%s" attribute on tag "controller.service_arguments" %s for service "%s".', $k, 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 "controller.service_arguments" for service "%s": no public "%s()" method found on class "%s".', $id, $attributes['action'], $class));
  90. }
  91. [$r, $parameters] = $methods[$action];
  92. $found = false;
  93. foreach ($parameters as $p) {
  94. if ($attributes['argument'] === $p->name) {
  95. if (!isset($arguments[$r->name][$p->name])) {
  96. $arguments[$r->name][$p->name] = $attributes['id'];
  97. }
  98. $found = true;
  99. break;
  100. }
  101. }
  102. if (!$found) {
  103. throw new InvalidArgumentException(\sprintf('Invalid "controller.service_arguments" tag for service "%s": method "%s()" has no "%s" argument on class "%s".', $id, $r->name, $attributes['argument'], $class));
  104. }
  105. }
  106. foreach ($methods as [$r, $parameters]) {
  107. /** @var \ReflectionMethod $r */
  108. // create a per-method map of argument-names to service/type-references
  109. $args = [];
  110. $erroredIds = 0;
  111. foreach ($parameters as $p) {
  112. /** @var \ReflectionParameter $p */
  113. $type = preg_replace('/(^|[(|&])\\\\/', '\1', $target = ltrim(ProxyHelper::exportType($p) ?? '', '?'));
  114. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  115. $autowireAttributes = $autowire ? $emptyAutowireAttributes : [];
  116. $parsedName = $p->name;
  117. $k = null;
  118. if (isset($arguments[$r->name][$p->name])) {
  119. $target = $arguments[$r->name][$p->name];
  120. if ('?' !== $target[0]) {
  121. $invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE;
  122. } elseif ('' === $target = substr($target, 1)) {
  123. throw new InvalidArgumentException(\sprintf('A "controller.service_arguments" tag must have non-empty "id" attributes for service "%s".', $id));
  124. } elseif ($p->allowsNull() && !$p->isOptional()) {
  125. $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
  126. }
  127. } elseif (isset($bindings[$bindingName = $type.' $'.$name = Target::parseName($p, $k, $parsedName)])
  128. || isset($bindings[$bindingName = $type.' $'.$parsedName])
  129. || isset($bindings[$bindingName = '$'.$name])
  130. || isset($bindings[$bindingName = $type])
  131. ) {
  132. $binding = $bindings[$bindingName];
  133. [$bindingValue, $bindingId, , $bindingType, $bindingFile] = $binding->getValues();
  134. $binding->setValues([$bindingValue, $bindingId, true, $bindingType, $bindingFile]);
  135. $args[$p->name] = $bindingValue;
  136. continue;
  137. } elseif (!$autowire || (!($autowireAttributes ??= $p->getAttributes(Autowire::class, \ReflectionAttribute::IS_INSTANCEOF)) && (!$type || '\\' !== $target[0]))) {
  138. continue;
  139. } elseif (!$autowireAttributes && is_subclass_of($type, \UnitEnum::class)) {
  140. // do not attempt to register enum typed arguments if not already present in bindings
  141. continue;
  142. } elseif (!$p->allowsNull()) {
  143. $invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE;
  144. }
  145. if (Request::class === $type || SessionInterface::class === $type || Response::class === $type) {
  146. continue;
  147. }
  148. if ($autowireAttributes) {
  149. $attribute = $autowireAttributes[0]->newInstance();
  150. $value = $parameterBag->resolveValue($attribute->value);
  151. if ($attribute instanceof AutowireCallable) {
  152. $args[$p->name] = $attribute->buildDefinition($value, $type, $p);
  153. } elseif ($value instanceof Reference) {
  154. $args[$p->name] = $type ? new TypedReference($value, $type, $invalidBehavior, $p->name) : new Reference($value, $invalidBehavior);
  155. } else {
  156. $args[$p->name] = new Reference('.value.'.$container->hash($value));
  157. $container->register((string) $args[$p->name], 'mixed')
  158. ->setFactory('current')
  159. ->addArgument([$value]);
  160. }
  161. continue;
  162. }
  163. if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) {
  164. $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);
  165. // see if the type-hint lives in the same namespace as the controller
  166. if (0 === strncmp($type, $class, strrpos($class, '\\'))) {
  167. $message .= ' Did you forget to add a use statement?';
  168. }
  169. $container->register($erroredId = '.errored.'.$container->hash($message), $type)
  170. ->addError($message);
  171. $args[$p->name] = new Reference($erroredId, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE);
  172. ++$erroredIds;
  173. } else {
  174. $target = preg_replace('/(^|[(|&])\\\\/', '\1', $target);
  175. $args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior, Target::parseName($p)) : new Reference($target, $invalidBehavior);
  176. }
  177. }
  178. // register the maps as a per-method service-locators
  179. if ($args) {
  180. $controllers[$id.'::'.$r->name] = ServiceLocatorTagPass::register($container, $args, \count($args) !== $erroredIds ? $id.'::'.$r->name.'()' : null);
  181. foreach ($publicAliases[$id] ?? [] as $alias) {
  182. $controllers[$alias.'::'.$r->name] = clone $controllers[$id.'::'.$r->name];
  183. }
  184. }
  185. }
  186. }
  187. $controllerLocatorRef = ServiceLocatorTagPass::register($container, $controllers);
  188. if ($container->hasDefinition('argument_resolver.service')) {
  189. $container->getDefinition('argument_resolver.service')
  190. ->replaceArgument(0, $controllerLocatorRef);
  191. }
  192. if ($container->hasDefinition('argument_resolver.not_tagged_controller')) {
  193. $container->getDefinition('argument_resolver.not_tagged_controller')
  194. ->replaceArgument(0, $controllerLocatorRef);
  195. }
  196. $container->setAlias('argument_resolver.controller_locator', (string) $controllerLocatorRef);
  197. if ($container->hasDefinition('controller_resolver')) {
  198. $container->getDefinition('controller_resolver')
  199. ->addMethodCall('allowControllers', [array_unique($controllerClasses)]);
  200. }
  201. }
  202. }