ControllerResolver.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\Controller;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. /**
  14. * This implementation uses the '_controller' request attribute to determine
  15. * the controller to execute and uses the request attributes to determine
  16. * the controller method arguments.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class ControllerResolver implements ControllerResolverInterface
  21. {
  22. private $logger;
  23. /**
  24. * If the ...$arg functionality is available.
  25. *
  26. * Requires at least PHP 5.6.0 or HHVM 3.9.1
  27. *
  28. * @var bool
  29. */
  30. private $supportsVariadic;
  31. /**
  32. * If scalar types exists.
  33. *
  34. * @var bool
  35. */
  36. private $supportsScalarTypes;
  37. public function __construct(LoggerInterface $logger = null)
  38. {
  39. $this->logger = $logger;
  40. $this->supportsVariadic = method_exists('ReflectionParameter', 'isVariadic');
  41. $this->supportsScalarTypes = method_exists('ReflectionParameter', 'getType');
  42. }
  43. /**
  44. * {@inheritdoc}
  45. *
  46. * This method looks for a '_controller' request attribute that represents
  47. * the controller name (a string like ClassName::MethodName).
  48. */
  49. public function getController(Request $request)
  50. {
  51. if (!$controller = $request->attributes->get('_controller')) {
  52. if (null !== $this->logger) {
  53. $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
  54. }
  55. return false;
  56. }
  57. if (\is_array($controller)) {
  58. return $controller;
  59. }
  60. if (\is_object($controller)) {
  61. if (method_exists($controller, '__invoke')) {
  62. return $controller;
  63. }
  64. throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', \get_class($controller), $request->getPathInfo()));
  65. }
  66. if (false === strpos($controller, ':')) {
  67. if (method_exists($controller, '__invoke')) {
  68. return $this->instantiateController($controller);
  69. } elseif (\function_exists($controller)) {
  70. return $controller;
  71. }
  72. }
  73. $callable = $this->createController($controller);
  74. if (!\is_callable($callable)) {
  75. throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', $controller, $request->getPathInfo()));
  76. }
  77. return $callable;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getArguments(Request $request, $controller)
  83. {
  84. if (\is_array($controller)) {
  85. $r = new \ReflectionMethod($controller[0], $controller[1]);
  86. } elseif (\is_object($controller) && !$controller instanceof \Closure) {
  87. $r = new \ReflectionObject($controller);
  88. $r = $r->getMethod('__invoke');
  89. } else {
  90. $r = new \ReflectionFunction($controller);
  91. }
  92. return $this->doGetArguments($request, $controller, $r->getParameters());
  93. }
  94. /**
  95. * @param Request $request
  96. * @param callable $controller
  97. * @param \ReflectionParameter[] $parameters
  98. *
  99. * @return array The arguments to use when calling the action
  100. */
  101. protected function doGetArguments(Request $request, $controller, array $parameters)
  102. {
  103. $attributes = $request->attributes->all();
  104. $arguments = array();
  105. foreach ($parameters as $param) {
  106. if (array_key_exists($param->name, $attributes)) {
  107. if ($this->supportsVariadic && $param->isVariadic() && \is_array($attributes[$param->name])) {
  108. $arguments = array_merge($arguments, array_values($attributes[$param->name]));
  109. } else {
  110. $arguments[] = $attributes[$param->name];
  111. }
  112. } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
  113. $arguments[] = $request;
  114. } elseif ($param->isDefaultValueAvailable()) {
  115. $arguments[] = $param->getDefaultValue();
  116. } elseif ($this->supportsScalarTypes && $param->hasType() && $param->allowsNull()) {
  117. $arguments[] = null;
  118. } else {
  119. if (\is_array($controller)) {
  120. $repr = sprintf('%s::%s()', \get_class($controller[0]), $controller[1]);
  121. } elseif (\is_object($controller)) {
  122. $repr = \get_class($controller);
  123. } else {
  124. $repr = $controller;
  125. }
  126. throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name));
  127. }
  128. }
  129. return $arguments;
  130. }
  131. /**
  132. * Returns a callable for the given controller.
  133. *
  134. * @param string $controller A Controller string
  135. *
  136. * @return callable A PHP callable
  137. *
  138. * @throws \InvalidArgumentException
  139. */
  140. protected function createController($controller)
  141. {
  142. if (false === strpos($controller, '::')) {
  143. throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
  144. }
  145. list($class, $method) = explode('::', $controller, 2);
  146. if (!class_exists($class)) {
  147. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  148. }
  149. return array($this->instantiateController($class), $method);
  150. }
  151. /**
  152. * Returns an instantiated controller.
  153. *
  154. * @param string $class A class name
  155. *
  156. * @return object
  157. */
  158. protected function instantiateController($class)
  159. {
  160. return new $class();
  161. }
  162. }