/vendor/symfony/src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

https://github.com/proclamo/txinbometro · PHP · 143 lines · 68 code · 19 blank · 56 comment · 14 complexity · 7082e43782f97149f83c08b1ea2e4157 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 Symfony\Component\HttpKernel\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->warn('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) || false === strpos($controller, ':')) && method_exists($controller, '__invoke'))) {
  60. return $controller;
  61. }
  62. list($controller, $method) = $this->createController($controller);
  63. if (!method_exists($controller, $method)) {
  64. throw new \InvalidArgumentException(sprintf('Method "%s::%s" does not exist.', get_class($controller), $method));
  65. }
  66. return array($controller, $method);
  67. }
  68. /**
  69. * Returns the arguments to pass to the controller.
  70. *
  71. * @param Request $request A Request instance
  72. * @param mixed $controller A PHP callable
  73. *
  74. * @throws \RuntimeException When value for argument given is not provided
  75. *
  76. * @api
  77. */
  78. public function getArguments(Request $request, $controller)
  79. {
  80. $attributes = $request->attributes->all();
  81. if (is_array($controller)) {
  82. $r = new \ReflectionMethod($controller[0], $controller[1]);
  83. $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
  84. } elseif (is_object($controller)) {
  85. $r = new \ReflectionObject($controller);
  86. $r = $r->getMethod('__invoke');
  87. $repr = get_class($controller);
  88. } else {
  89. $r = new \ReflectionFunction($controller);
  90. $repr = $controller;
  91. }
  92. $arguments = array();
  93. foreach ($r->getParameters() as $param) {
  94. if (array_key_exists($param->getName(), $attributes)) {
  95. $arguments[] = $attributes[$param->getName()];
  96. } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
  97. $arguments[] = $request;
  98. } elseif ($param->isDefaultValueAvailable()) {
  99. $arguments[] = $param->getDefaultValue();
  100. } else {
  101. 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->getName()));
  102. }
  103. }
  104. return $arguments;
  105. }
  106. /**
  107. * Returns a callable for the given controller.
  108. *
  109. * @param string $controller A Controller string
  110. *
  111. * @return mixed A PHP callable
  112. */
  113. protected function createController($controller)
  114. {
  115. if (false === strpos($controller, '::')) {
  116. throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
  117. }
  118. list($class, $method) = explode('::', $controller);
  119. if (!class_exists($class)) {
  120. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  121. }
  122. return array(new $class(), $method);
  123. }
  124. }