HttpKernel.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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;
  11. use Symfony\Component\EventDispatcher\Event;
  12. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  13. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. /**
  18. * HttpKernel notifies events to convert a Request object to a Response one.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class HttpKernel implements HttpKernelInterface
  23. {
  24. protected $dispatcher;
  25. protected $resolver;
  26. /**
  27. * Constructor
  28. *
  29. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  30. * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
  31. */
  32. public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver)
  33. {
  34. $this->dispatcher = $dispatcher;
  35. $this->resolver = $resolver;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  41. {
  42. try {
  43. $response = $this->handleRaw($request, $type);
  44. } catch (\Exception $e) {
  45. if (false === $catch) {
  46. throw $e;
  47. }
  48. // exception
  49. $event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
  50. $response = $this->dispatcher->notifyUntil($event);
  51. if (!$event->isProcessed()) {
  52. throw $e;
  53. }
  54. $response = $this->filterResponse($response, $request, 'A "core.exception" listener returned a non response object.', $type);
  55. }
  56. return $response;
  57. }
  58. /**
  59. * Handles a request to convert it to a response.
  60. *
  61. * Exceptions are not caught.
  62. *
  63. * @param Request $request A Request instance
  64. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  65. *
  66. * @return Response A Response instance
  67. *
  68. * @throws \LogicException If one of the listener does not behave as expected
  69. * @throws NotFoundHttpException When controller cannot be found
  70. */
  71. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  72. {
  73. // request
  74. $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
  75. $response = $this->dispatcher->notifyUntil($event);
  76. if ($event->isProcessed()) {
  77. return $this->filterResponse($response, $request, 'A "core.request" listener returned a non response object.', $type);
  78. }
  79. // load controller
  80. if (false === $controller = $this->resolver->getController($request)) {
  81. throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". Maybe you forgot to add the matching route in your routing configuration?', $request->getPathInfo()));
  82. }
  83. $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
  84. $controller = $this->dispatcher->filter($event, $controller);
  85. // controller must be a callable
  86. if (!is_callable($controller)) {
  87. throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
  88. }
  89. // controller arguments
  90. $arguments = $this->resolver->getArguments($request, $controller);
  91. // call controller
  92. $response = call_user_func_array($controller, $arguments);
  93. // view
  94. if (!$response instanceof Response) {
  95. $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request, 'controller_value' => $response));
  96. $retval = $this->dispatcher->notifyUntil($event);
  97. if ($event->isProcessed()) {
  98. $response = $retval;
  99. }
  100. }
  101. return $this->filterResponse($response, $request, sprintf('The controller must return a response (%s given).', $this->varToString($response)), $type);
  102. }
  103. /**
  104. * Filters a response object.
  105. *
  106. * @param Response $response A Response instance
  107. * @param string $message A error message in case the response is not a Response object
  108. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  109. *
  110. * @return Response The filtered Response instance
  111. *
  112. * @throws \RuntimeException if the passed object is not a Response instance
  113. */
  114. protected function filterResponse($response, $request, $message, $type)
  115. {
  116. if (!$response instanceof Response) {
  117. throw new \RuntimeException($message);
  118. }
  119. $response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  120. if (!$response instanceof Response) {
  121. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  122. }
  123. return $response;
  124. }
  125. protected function varToString($var)
  126. {
  127. if (is_object($var)) {
  128. return sprintf('[object](%s)', get_class($var));
  129. }
  130. if (is_array($var)) {
  131. $a = array();
  132. foreach ($var as $k => $v) {
  133. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  134. }
  135. return sprintf("[array](%s)", implode(', ', $a));
  136. }
  137. if (is_resource($var)) {
  138. return '[resource]';
  139. }
  140. return str_replace("\n", '', var_export((string) $var, true));
  141. }
  142. }