PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Controller/ControllerResolver.php

https://gitlab.com/wormen/client.mastodont-engine
PHP | 238 lines | 143 code | 43 blank | 52 comment | 36 complexity | 0dd4a86a576367b5bd173c36472382f7 MD5 | raw file
  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 ArgumentResolverInterface, ControllerResolverInterface
  23. {
  24. private $logger;
  25. /**
  26. * Constructor.
  27. *
  28. * @param LoggerInterface $logger A LoggerInterface instance
  29. */
  30. public function __construct(LoggerInterface $logger = null)
  31. {
  32. $this->logger = $logger;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. *
  37. * This method looks for a '_controller' request attribute that represents
  38. * the controller name (a string like ClassName::MethodName).
  39. */
  40. public function getController(Request $request)
  41. {
  42. if (!$controller = $request->attributes->get('_controller')) {
  43. if (null !== $this->logger) {
  44. $this->logger->warning('Unable to look for the controller as the "_controller" parameter is missing.');
  45. }
  46. return false;
  47. }
  48. if (is_array($controller)) {
  49. return $controller;
  50. }
  51. if (is_object($controller)) {
  52. if (method_exists($controller, '__invoke')) {
  53. return $controller;
  54. }
  55. throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
  56. }
  57. if (false === strpos($controller, ':')) {
  58. if (method_exists($controller, '__invoke')) {
  59. return $this->instantiateController($controller);
  60. } elseif (function_exists($controller)) {
  61. return $controller;
  62. }
  63. }
  64. $callable = $this->createController($controller);
  65. if (!is_callable($callable)) {
  66. throw new \InvalidArgumentException(sprintf('The controller for URI "%s" is not callable. %s', $request->getPathInfo(), $this->getControllerError($callable)));
  67. }
  68. return $callable;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. *
  73. * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
  74. */
  75. public function getArguments(Request $request, $controller)
  76. {
  77. @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
  78. if (is_array($controller)) {
  79. $r = new \ReflectionMethod($controller[0], $controller[1]);
  80. } elseif (is_object($controller) && !$controller instanceof \Closure) {
  81. $r = new \ReflectionObject($controller);
  82. $r = $r->getMethod('__invoke');
  83. } else {
  84. $r = new \ReflectionFunction($controller);
  85. }
  86. return $this->doGetArguments($request, $controller, $r->getParameters());
  87. }
  88. /**
  89. * @deprecated This method is deprecated as of 3.1 and will be removed in 4.0. Implement the ArgumentResolverInterface and inject it in the HttpKernel instead.
  90. */
  91. protected function doGetArguments(Request $request, $controller, array $parameters)
  92. {
  93. @trigger_error(sprintf('%s is deprecated as of 3.1 and will be removed in 4.0. Implement the %s and inject it in the HttpKernel instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
  94. $attributes = $request->attributes->all();
  95. $arguments = array();
  96. foreach ($parameters as $param) {
  97. if (array_key_exists($param->name, $attributes)) {
  98. if (PHP_VERSION_ID >= 50600 && $param->isVariadic() && is_array($attributes[$param->name])) {
  99. $arguments = array_merge($arguments, array_values($attributes[$param->name]));
  100. } else {
  101. $arguments[] = $attributes[$param->name];
  102. }
  103. } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
  104. $arguments[] = $request;
  105. } elseif ($param->isDefaultValueAvailable()) {
  106. $arguments[] = $param->getDefaultValue();
  107. } else {
  108. if (is_array($controller)) {
  109. $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
  110. } elseif (is_object($controller)) {
  111. $repr = get_class($controller);
  112. } else {
  113. $repr = $controller;
  114. }
  115. 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));
  116. }
  117. }
  118. return $arguments;
  119. }
  120. /**
  121. * Returns a callable for the given controller.
  122. *
  123. * @param string $controller A Controller string
  124. *
  125. * @return callable A PHP callable
  126. *
  127. * @throws \InvalidArgumentException
  128. */
  129. protected function createController($controller)
  130. {
  131. if (false === strpos($controller, '::')) {
  132. throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
  133. }
  134. list($class, $method) = explode('::', $controller, 2);
  135. if (!class_exists($class)) {
  136. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  137. }
  138. return array($this->instantiateController($class), $method);
  139. }
  140. /**
  141. * Returns an instantiated controller.
  142. *
  143. * @param string $class A class name
  144. *
  145. * @return object
  146. */
  147. protected function instantiateController($class)
  148. {
  149. return new $class();
  150. }
  151. private function getControllerError($callable)
  152. {
  153. if (is_string($callable)) {
  154. if (false !== strpos($callable, '::')) {
  155. $callable = explode('::', $callable);
  156. }
  157. if (class_exists($callable) && !method_exists($callable, '__invoke')) {
  158. return sprintf('Class "%s" does not have a method "__invoke".', $callable);
  159. }
  160. if (!function_exists($callable)) {
  161. return sprintf('Function "%s" does not exist.', $callable);
  162. }
  163. }
  164. if (!is_array($callable)) {
  165. return sprintf('Invalid type for controller given, expected string or array, got "%s".', gettype($callable));
  166. }
  167. if (2 !== count($callable)) {
  168. return sprintf('Invalid format for controller, expected array(controller, method) or controller::method.');
  169. }
  170. list($controller, $method) = $callable;
  171. if (is_string($controller) && !class_exists($controller)) {
  172. return sprintf('Class "%s" does not exist.', $controller);
  173. }
  174. $className = is_object($controller) ? get_class($controller) : $controller;
  175. if (method_exists($controller, $method)) {
  176. return sprintf('Method "%s" on class "%s" should be public and non-abstract.', $method, $className);
  177. }
  178. $collection = get_class_methods($controller);
  179. $alternatives = array();
  180. foreach ($collection as $item) {
  181. $lev = levenshtein($method, $item);
  182. if ($lev <= strlen($method) / 3 || false !== strpos($item, $method)) {
  183. $alternatives[] = $item;
  184. }
  185. }
  186. asort($alternatives);
  187. $message = sprintf('Expected method "%s" on class "%s"', $method, $className);
  188. if (count($alternatives) > 0) {
  189. $message .= sprintf(', did you mean "%s"?', implode('", "', $alternatives));
  190. } else {
  191. $message .= sprintf('. Available methods: "%s".', implode('", "', $collection));
  192. }
  193. return $message;
  194. }
  195. }