HttpKernel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  27. * Constructor
  28. *
  29. * @param EventDispatcher $dispatcher An event dispatcher instance
  30. * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
  31. */
  32. public function __construct(EventDispatcher $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. return $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. $this->dispatcher->notifyUntil($event);
  51. if ($event->isProcessed()) {
  52. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
  53. }
  54. throw $e;
  55. }
  56. }
  57. /**
  58. * Handles a request to convert it to a response.
  59. *
  60. * Exceptions are not caught.
  61. *
  62. * @param Request $request A Request instance
  63. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  64. *
  65. * @return Response A Response instance
  66. *
  67. * @throws \LogicException If one of the listener does not behave as expected
  68. * @throws NotFoundHttpException When controller cannot be found
  69. */
  70. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  71. {
  72. // request
  73. $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
  74. $this->dispatcher->notifyUntil($event);
  75. if ($event->isProcessed()) {
  76. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
  77. }
  78. // load controller
  79. if (false === $controller = $this->resolver->getController($request)) {
  80. throw new NotFoundHttpException('Unable to find the controller.');
  81. }
  82. $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
  83. $this->dispatcher->filter($event, $controller);
  84. $controller = $event->getReturnValue();
  85. // controller must be a callable
  86. if (!is_callable($controller)) {
  87. throw new \LogicException(sprintf('The controller must be a callable (%s).', var_export($controller, true)));
  88. }
  89. // controller arguments
  90. $arguments = $this->resolver->getArguments($request, $controller);
  91. // call controller
  92. $retval = call_user_func_array($controller, $arguments);
  93. // view
  94. $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
  95. $this->dispatcher->filter($event, $retval);
  96. 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);
  97. }
  98. /**
  99. * Filters a response object.
  100. *
  101. * @param Response $response A Response instance
  102. * @param string $message A error message in case the response is not a Response object
  103. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  104. *
  105. * @return Response The filtered Response instance
  106. *
  107. * @throws \RuntimeException if the passed object is not a Response instance
  108. */
  109. protected function filterResponse($response, $request, $message, $type)
  110. {
  111. if (!$response instanceof Response) {
  112. throw new \RuntimeException($message);
  113. }
  114. $event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  115. $response = $event->getReturnValue();
  116. if (!$response instanceof Response) {
  117. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  118. }
  119. return $response;
  120. }
  121. }