HttpKernel.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace Symfony\Component\HttpKernel;
  3. use Symfony\Component\EventDispatcher\Event;
  4. use Symfony\Component\EventDispatcher\EventDispatcher;
  5. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. /*
  10. * This file is part of the Symfony package.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  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. protected $request;
  27. /**
  28. * Constructor
  29. *
  30. * @param EventDispatcher $dispatcher An event dispatcher instance
  31. * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
  32. */
  33. public function __construct(EventDispatcher $dispatcher, ControllerResolverInterface $resolver)
  34. {
  35. $this->dispatcher = $dispatcher;
  36. $this->resolver = $resolver;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  42. {
  43. // set the current request, stash the previous one
  44. $previousRequest = $this->request;
  45. $this->request = $request;
  46. try {
  47. $response = $this->handleRaw($request, $type);
  48. } catch (\Exception $e) {
  49. if (false === $catch) {
  50. throw $e;
  51. }
  52. // exception
  53. $event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
  54. $this->dispatcher->notifyUntil($event);
  55. if ($event->isProcessed()) {
  56. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
  57. }
  58. // restore the previous request
  59. $this->request = $previousRequest;
  60. throw $e;
  61. }
  62. // restore the previous request
  63. $this->request = $previousRequest;
  64. return $response;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getRequest()
  70. {
  71. return $this->request;
  72. }
  73. /**
  74. * Handles a request to convert it to a response.
  75. *
  76. * Exceptions are not caught.
  77. *
  78. * @param Request $request A Request instance
  79. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  80. *
  81. * @return Response A Response instance
  82. *
  83. * @throws \LogicException If one of the listener does not behave as expected
  84. * @throws NotFoundHttpException When controller cannot be found
  85. */
  86. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  87. {
  88. // request
  89. $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
  90. $this->dispatcher->notifyUntil($event);
  91. if ($event->isProcessed()) {
  92. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
  93. }
  94. // load controller
  95. if (false === $controller = $this->resolver->getController($request)) {
  96. throw new NotFoundHttpException('Unable to find the controller.');
  97. }
  98. $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
  99. $this->dispatcher->filter($event, $controller);
  100. $controller = $event->getReturnValue();
  101. // controller must be a callable
  102. if (!is_callable($controller)) {
  103. throw new \LogicException(sprintf('The controller must be a callable (%s).', var_export($controller, true)));
  104. }
  105. // controller arguments
  106. $arguments = $this->resolver->getArguments($request, $controller);
  107. // call controller
  108. $retval = call_user_func_array($controller, $arguments);
  109. // view
  110. $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
  111. $this->dispatcher->filter($event, $retval);
  112. return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type);
  113. }
  114. /**
  115. * Filters a response object.
  116. *
  117. * @param Response $response A Response instance
  118. * @param string $message A error message in case the response is not a Response object
  119. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  120. *
  121. * @return Response The filtered Response instance
  122. *
  123. * @throws \RuntimeException if the passed object is not a Response instance
  124. */
  125. protected function filterResponse($response, $request, $message, $type)
  126. {
  127. if (!$response instanceof Response) {
  128. throw new \RuntimeException($message);
  129. }
  130. $event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  131. $response = $event->getReturnValue();
  132. if (!$response instanceof Response) {
  133. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  134. }
  135. return $response;
  136. }
  137. }