菜谱项目

RegisterControllerArgumentLocatorsPass.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\DependencyInjection\ChildDefinition;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  15. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  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\ProxyHelper;
  20. use Symfony\Component\DependencyInjection\Reference;
  21. use Symfony\Component\DependencyInjection\TypedReference;
  22. /**
  23. * Creates the service-locators required by ServiceValueResolver.
  24. *
  25. * @author Nicolas Grekas <p@tchwork.com>
  26. */
  27. class RegisterControllerArgumentLocatorsPass implements CompilerPassInterface
  28. {
  29. private $resolverServiceId;
  30. private $controllerTag;
  31. public function __construct($resolverServiceId = 'argument_resolver.service', $controllerTag = 'controller.service_arguments')
  32. {
  33. $this->resolverServiceId = $resolverServiceId;
  34. $this->controllerTag = $controllerTag;
  35. }
  36. public function process(ContainerBuilder $container)
  37. {
  38. if (false === $container->hasDefinition($this->resolverServiceId)) {
  39. return;
  40. }
  41. $parameterBag = $container->getParameterBag();
  42. $controllers = array();
  43. foreach ($container->findTaggedServiceIds($this->controllerTag, true) as $id => $tags) {
  44. $def = $container->getDefinition($id);
  45. $def->setPublic(true);
  46. $class = $def->getClass();
  47. $autowire = $def->isAutowired();
  48. // resolve service class, taking parent definitions into account
  49. while (!$class && $def instanceof ChildDefinition) {
  50. $def = $container->findDefinition($def->getParent());
  51. $class = $def->getClass();
  52. }
  53. $class = $parameterBag->resolveValue($class);
  54. if (!$r = $container->getReflectionClass($class)) {
  55. throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
  56. }
  57. $isContainerAware = $r->implementsInterface(ContainerAwareInterface::class) || is_subclass_of($class, AbstractController::class);
  58. // get regular public methods
  59. $methods = array();
  60. $arguments = array();
  61. foreach ($r->getMethods(\ReflectionMethod::IS_PUBLIC) as $r) {
  62. if ('setContainer' === $r->name && $isContainerAware) {
  63. continue;
  64. }
  65. if (!$r->isConstructor() && !$r->isDestructor() && !$r->isAbstract()) {
  66. $methods[strtolower($r->name)] = array($r, $r->getParameters());
  67. }
  68. }
  69. // validate and collect explicit per-actions and per-arguments service references
  70. foreach ($tags as $attributes) {
  71. if (!isset($attributes['action']) && !isset($attributes['argument']) && !isset($attributes['id'])) {
  72. $autowire = true;
  73. continue;
  74. }
  75. foreach (array('action', 'argument', 'id') as $k) {
  76. if (!isset($attributes[$k][0])) {
  77. throw new InvalidArgumentException(sprintf('Missing "%s" attribute on tag "%s" %s for service "%s".', $k, $this->controllerTag, json_encode($attributes, JSON_UNESCAPED_UNICODE), $id));
  78. }
  79. }
  80. if (!isset($methods[$action = strtolower($attributes['action'])])) {
  81. 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));
  82. }
  83. list($r, $parameters) = $methods[$action];
  84. $found = false;
  85. foreach ($parameters as $p) {
  86. if ($attributes['argument'] === $p->name) {
  87. if (!isset($arguments[$r->name][$p->name])) {
  88. $arguments[$r->name][$p->name] = $attributes['id'];
  89. }
  90. $found = true;
  91. break;
  92. }
  93. }
  94. if (!$found) {
  95. 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));
  96. }
  97. }
  98. foreach ($methods as list($r, $parameters)) {
  99. /** @var \ReflectionMethod $r */
  100. // create a per-method map of argument-names to service/type-references
  101. $args = array();
  102. foreach ($parameters as $p) {
  103. /** @var \ReflectionParameter $p */
  104. $type = $target = ProxyHelper::getTypeHint($r, $p, true);
  105. $invalidBehavior = ContainerInterface::IGNORE_ON_INVALID_REFERENCE;
  106. if (isset($arguments[$r->name][$p->name])) {
  107. $target = $arguments[$r->name][$p->name];
  108. if ('?' !== $target[0]) {
  109. $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
  110. } elseif ('' === $target = (string) substr($target, 1)) {
  111. throw new InvalidArgumentException(sprintf('A "%s" tag must have non-empty "id" attributes for service "%s".', $this->controllerTag, $id));
  112. } elseif ($p->allowsNull() && !$p->isOptional()) {
  113. $invalidBehavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
  114. }
  115. } elseif (!$type || !$autowire) {
  116. continue;
  117. }
  118. if ($type && !$p->isOptional() && !$p->allowsNull() && !class_exists($type) && !interface_exists($type, false)) {
  119. $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);
  120. // see if the type-hint lives in the same namespace as the controller
  121. if (0 === strncmp($type, $class, strrpos($class, '\\'))) {
  122. $message .= ' Did you forget to add a use statement?';
  123. }
  124. throw new InvalidArgumentException($message);
  125. }
  126. $args[$p->name] = $type ? new TypedReference($target, $type, $r->class, $invalidBehavior) : new Reference($target, $invalidBehavior);
  127. }
  128. // register the maps as a per-method service-locators
  129. if ($args) {
  130. $controllers[$id.':'.$r->name] = ServiceLocatorTagPass::register($container, $args);
  131. }
  132. }
  133. }
  134. $container->getDefinition($this->resolverServiceId)
  135. ->replaceArgument(0, ServiceLocatorTagPass::register($container, $controllers));
  136. }
  137. }