vendor/symfony/http-kernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php line 172

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <[email protected]>
  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\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\DependencyInjection\Attribute\Target;
  13. use Symfony\Component\DependencyInjection\ChildDefinition;
  14. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  15. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  16. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  17. use Symfony\Component\DependencyInjection\ContainerBuilder;
  18. use Symfony\Component\DependencyInjection\ContainerInterface;
  19. use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
  20. use Symfony\Component\DependencyInjection\LazyProxy\ProxyHelper;
  21. use Symfony\Component\DependencyInjection\Reference;
  22. use Symfony\Component\DependencyInjection\TypedReference;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  26. /**
  27. * Creates the service-locators required by ServiceValueResolver.
  28. *
  29. * @author Nicolas Grekas <[email protected]>
  30. */
  31. class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface
  32. {
  33. private $resolverServiceId;
  34. private $controllerTag;
  35. private $controllerLocator;
  36. private $notTaggedControllerResolverServiceId;
  37. public function __construct(string $resolverServiceId = 'argument_resolver.service', string $controllerTag = 'controller.service_arguments', string $controllerLocator = 'argument_resolver.controller_locator', string $notTaggedControllerResolverServiceId = 'argument_resolver.not_tagged_controller')
  38. {
  39. if (0 < \func_num_args()) {
  40. trigger_deprecation('symfony/http-kernel', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
  41. }
  42. $this->resolverServiceId = $resolverServiceId;
  43. $this->controllerTag = $controllerTag;
  44. $this->controllerLocator = $controllerLocator;
  45. $this->notTaggedControllerResolverServiceId = $notTaggedControllerResolverServiceId;
  46. }
  47. public function process(ContainerBuilder $container)
  48. {
  49. if (false === $container->hasDefinition($this->resolverServiceId) && false === $container->hasDefinition($this->notTaggedControllerResolverServiceId)) {
  50. return;
  51. }
  52. $parameterBag = $container->getParameterBag();
  53. $controllers = [];
  54. $publicAliases = [];
  55. foreach ($container->getAliases() as $id => $alias) {
  56. if ($alias->isPublic() && !$alias->isPrivate()) {
  57. $publicAliases[(string) $alias][] = $id;
  58. }
  59. }
  60. foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) {
  61. $def = $container->getDefinition($id);
  62. $def->setPublic(true);
  63. $def->setLazy(false);
  64. $class = $def->getClass();
  65. $autowire = $def->isAutowired();
  66. $bindings = $def->getBindings();
  67. // resolve service class, taking parent definitions into account
  68. while ($def instanceof ChildDefinition) {
  69. $def = $container->findDefinition($def->getParent());
  70. $class = $class ?: $def->getClass();
  71. $bindings += $def->getBindings();
  72. }
  73. $class = $parameterBag->resolveValue($class);
  74. if (!$r = $container->getReflectionClass($class)) {
  75. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  76. }
  77. $isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);
  78. // get regular public methods
  79. $methods = [];
  80. $arguments = [];
  81. foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
  82. if ('setContainer' === $r->name && $isContainerAware) {
  83. continue;
  84. }
  85. if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
  86. $methods[strtolower($r->name)] = [$r, $r->getParameters()];
  87. }
  88. }
  89. // validate and collect explicit per-actions and per-arguments service references
  90. foreach ($tags as $attributes) {
  91. if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) {
  92. $autowire = true;
  93. continue;
  94. }
  95. foreach (['action', 'argument', 'id'] as $k) {
  96. if (!isset($attributes[$k][0])) {
  97. throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, \JSON_UNESCAPED_UNICODE), $id));
  98. }
  99. }
  100. if (!isset($methods[$action = strtolower($attributes['action'])])) {
  101. 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));
  102. }
  103. [$r, $parameters] = $methods[$action];
  104. $found = false;
  105. foreach ($parameters as $p) {
  106. if ($attributes['argument'] === $p->name) {
  107. if (!isset($arguments[$r->name][$p->name])) {
  108. $arguments[$r->name][$p->name] = $attributes['id'];
  109. }
  110. $found = true;
  111. break;
  112. }
  113. }
  114. if (!$found) {
  115. 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. }
  117. }
  118. foreach ($methods as [$r, $parameters]) {
  119. /** @var \ReflectionMethod $r */
  120. // create a per-method map of argument-names to service/type-references
  121. $args = [];
  122. foreach ($parameters as $p) {
  123. /** @var \ReflectionParameter $p */
  124. $type = ltrim($target = (string) ProxyHelper::getTypeHint($r, $p), '\\');
  125. $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::RUNTIME_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;
  134. }
  135. } elseif (isset($bindings[$bindingName = $type.' $'.$name = Target::parseName($p)]) || isset($bindings[$bindingName = '$'.$name]) || isset($bindings[$bindingName = $type])) {
  136. $binding = $bindings[$bindingName];
  137. [$bindingValue, $bindingId, , $bindingType, $bindingFile] = $binding->getValues();
  138. $binding->setValues([$bindingValue, $bindingId, true, $bindingType, $bindingFile]);
  139. if (!$bindingValue instanceof Reference) {
  140. $args[$p->name] = new Reference('.value.'.$container->hash($bindingValue));
  141. $container->register((string) $args[$p->name], 'mixed')
  142. ->setFactory('current')
  143. ->addArgument([$bindingValue]);
  144. } else {
  145. $args[$p->name] = $bindingValue;
  146. }
  147. continue;
  148. } elseif (!$type || !$autowire || '\\' !== $target[0]) {
  149. continue;
  150. } elseif (is_subclass_of($type, \UnitEnum::class)) {
  151. // do not attempt to register enum typed arguments if not already present in bindings
  152. continue;
  153. } elseif (!$p->allowsNull()) {
  154. $invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE;
  155. }
  156. if (Request::class === $type || SessionInterface::class === $type || Response::class === $type) {
  157. continue;
  158. }
  159. if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) {
  160. $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);
  161. // see if the type-hint lives in the same namespace as the controller
  162. if (0 === strncmp($type, $class, strrpos($class, '\\'))) {
  163. $message .= ' Did you forget to add a use statement?';
  164. }
  165. $container->register($erroredId = '.errored.'.$container->hash($message), $type)
  166. ->addError($message);
  167. $args[$p->name] = new Reference($erroredId, ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE);
  168. } else {
  169. $target = ltrim($target, '\\');
  170. $args[$p->name] = $type ? new TypedReference($target, $type, $invalidBehavior, Target::parseName($p)) : new Reference($target, $invalidBehavior);
  171. }
  172. }
  173. // register the maps as a per-method service-locators
  174. if ($args) {
  175. $controllers[$id.'::'.$r->name] = ServiceLocatorTagPass::register($container, $args);
  176. foreach ($publicAliases[$id] ?? [] as $alias) {
  177. $controllers[$alias.'::'.$r->name] = clone $controllers[$id.'::'.$r->name];
  178. }
  179. }
  180. }
  181. }
  182. $controllerLocatorRef = ServiceLocatorTagPass::register($container, $controllers);
  183. if ($container->hasDefinition($this->resolverServiceId)) {
  184. $container->getDefinition($this->resolverServiceId)
  185. ->replaceArgument(0, $controllerLocatorRef);
  186. }
  187. if ($container->hasDefinition($this->notTaggedControllerResolverServiceId)) {
  188. $container->getDefinition($this->notTaggedControllerResolverServiceId)
  189. ->replaceArgument(0, $controllerLocatorRef);
  190. }
  191. $container->setAlias($this->controllerLocator, (string) $controllerLocatorRef);
  192. }
  193. }