Drupal investigation

ControllerResolver.php 6.1KB

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