PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 156 lines | 79 code | 21 blank | 56 comment | 18 complexity | a3f3a09c8ae54ae448f7460608c62b1f MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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) && method_exists($controller, '__invoke'))) {
  60. return $controller;
  61. }
  62. if (false === strpos($controller, ':') && method_exists($controller, '__invoke')) {
  63. return new $controller;
  64. }
  65. list($controller, $method) = $this->createController($controller);
  66. if (!method_exists($controller, $method)) {
  67. throw new \InvalidArgumentException(sprintf('Method "%s::%s" does not exist.', get_class($controller), $method));
  68. }
  69. return array($controller, $method);
  70. }
  71. /**
  72. * Returns the arguments to pass to the controller.
  73. *
  74. * @param Request $request A Request instance
  75. * @param mixed $controller A PHP callable
  76. *
  77. * @throws \RuntimeException When value for argument given is not provided
  78. *
  79. * @api
  80. */
  81. public function getArguments(Request $request, $controller)
  82. {
  83. if (is_array($controller)) {
  84. $r = new \ReflectionMethod($controller[0], $controller[1]);
  85. } elseif (is_object($controller) && !$controller instanceof \Closure) {
  86. $r = new \ReflectionObject($controller);
  87. $r = $r->getMethod('__invoke');
  88. } else {
  89. $r = new \ReflectionFunction($controller);
  90. }
  91. return $this->doGetArguments($request, $controller, $r->getParameters());
  92. }
  93. protected function doGetArguments(Request $request, $controller, array $parameters)
  94. {
  95. $attributes = $request->attributes->all();
  96. $arguments = array();
  97. foreach ($parameters as $param) {
  98. if (array_key_exists($param->getName(), $attributes)) {
  99. $arguments[] = $attributes[$param->getName()];
  100. } elseif ($param->getClass() && $param->getClass()->isInstance($request)) {
  101. $arguments[] = $request;
  102. } elseif ($param->isDefaultValueAvailable()) {
  103. $arguments[] = $param->getDefaultValue();
  104. } else {
  105. if (is_array($controller)) {
  106. $repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]);
  107. } elseif (is_object($controller)) {
  108. $repr = get_class($controller);
  109. } else {
  110. $repr = $controller;
  111. }
  112. 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()));
  113. }
  114. }
  115. return $arguments;
  116. }
  117. /**
  118. * Returns a callable for the given controller.
  119. *
  120. * @param string $controller A Controller string
  121. *
  122. * @return mixed A PHP callable
  123. */
  124. protected function createController($controller)
  125. {
  126. if (false === strpos($controller, '::')) {
  127. throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
  128. }
  129. list($class, $method) = explode('::', $controller, 2);
  130. if (!class_exists($class)) {
  131. throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
  132. }
  133. return array(new $class(), $method);
  134. }
  135. }