ControllerResolver.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * ControllerResolver.
  15. *
  16. * This implementation uses the '_controller' request attribute to determine
  17. * the controller to execute and uses the request attributes to determine
  18. * the controller method arguments.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. *
  22. * @api
  23. */
  24. class ControllerResolver implements ControllerResolverInterface
  25. {
  26. private $logger;
  27. /**
  28. * Constructor.
  29. *
  30. * @param LoggerInterface $logger A LoggerInterface instance
  31. */
  32. public function __construct(LoggerInterface $logger = null)
  33. {
  34. $this->logger = $logger;
  35. }
  36. /**
  37. * Returns the Controller instance associated with a Request.
  38. *
  39. * This method looks for a '_controller' request attribute that represents
  40. * the controller name (a string like ClassName::MethodName).
  41. *
  42. * @param Request $request A Request instance
  43. *
  44. * @return mixed|Boolean A PHP callable representing the Controller,
  45. * or false if this resolver is not able to determine the controller
  46. *
  47. * @throws \InvalidArgumentException|\LogicException If the controller can't be found
  48. *
  49. * @api
  50. */
  51. public function getController(Request $request)
  52. {
  53. if (!$controller = $request->attributes->get('_controller')) {
  54. if (null !== $this->logger) {
  55. $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing');
  56. }
  57. return false;
  58. }
  59. if (is_array($controller) || (is_object($controller) && method_exists($controller, '__invoke'))) {
  60. return $controller;
  61. }
  62. if (false === strpos($controller, ':')) {
  63. if (method_exists($controller, '__invoke')) {
  64. return new $controller;
  65. } elseif (function_exists($controller)) {
  66. return $controller;
  67. }
  68. }
  69. $callable = $this->createController($controller);
  70. if (!is_callable($callable)) {
  71. throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable.', $request->getPathInfo()));
  72. }
  73. return $callable;
  74. }
  75. /**
  76. * Returns the arguments to pass to the controller.
  77. *
  78. * @param Request $request A Request instance
  79. * @param mixed $controller A PHP callable
  80. *
  81. * @return array
  82. *
  83. * @throws \RuntimeException When value for argument given is not provided
  84. *
  85. * @api
  86. */
  87. public function getArguments(Request $request, $controller)
  88. {
  89. if (is_array($controller)) {
  90. $r = new \ReflectionMethod($controller[0], $controller[1]);
  91. } elseif (is_object($controller) && !$controller instanceof \Closure) {
  92. $r = new \ReflectionObject($controller);
  93. $r = $r->getMethod('__invoke');
  94. } else {
  95. $r = new \ReflectionFunction($controller);
  96. }
  97. return $this->doGetArguments($request, $controller, $r->getParameters());
  98. }
  99. protected function doGetArguments(Request $request, $controller, array $parameters)
  100. {
  101. $attributes = $request->attributes->all();
  102. $arguments = array();
  103. foreach ($parameters as $param) {
  104. if (array_key_exists($param->name, $attributes)) {
  105. $arguments[] = $attributes[$param->name];
  106. } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
  107. $arguments[] = $request;
  108. } elseif ($param->isDefaultValueAvailable()) {
  109. $arguments[] = $param->getDefaultValue();
  110. } else {
  111. if (is_array($controller)) {
  112. $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
  113. } elseif (is_object($controller)) {
  114. $repr = get_class($controller);
  115. } else {
  116. $repr = $controller;
  117. }
  118. 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));
  119. }
  120. }
  121. return $arguments;
  122. }
  123. /**
  124. * Returns a callable for the given controller.
  125. *
  126. * @param string $controller A Controller string
  127. *
  128. * @return mixed A PHP callable
  129. *
  130. * @throws \InvalidArgumentException
  131. */
  132. protected function createController($controller)
  133. {
  134. if (false === strpos($controller, '::')) {
  135. throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
  136. }
  137. list($class, $method) = explode('::', $controller, 2);
  138. if (!class_exists($class)) {
  139. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  140. }
  141. return array(new $class(), $method);
  142. }
  143. }