HttpKernel.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. * Handles a Request to convert it to a Response.
  39. *
  40. * When $catch is true, the implementation must catch all exceptions
  41. * and do its best to convert them to a Response instance.
  42. *
  43. * @param Request $request A Request instance
  44. * @param integer $type The type of the request
  45. * (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  46. * @param Boolean $catch Whether to catch exceptions or not
  47. *
  48. * @return Response A Response instance
  49. *
  50. * @throws \Exception When an Exception occurs during processing
  51. */
  52. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  53. {
  54. try {
  55. $response = $this->handleRaw($request, $type);
  56. } catch (\Exception $e) {
  57. if (false === $catch) {
  58. throw $e;
  59. }
  60. // exception
  61. $event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
  62. $response = $this->dispatcher->notifyUntil($event);
  63. if (!$event->isProcessed()) {
  64. throw $e;
  65. }
  66. $response = $this->filterResponse($response, $request, 'A "core.exception" listener returned a non response object.', $type);
  67. }
  68. return $response;
  69. }
  70. /**
  71. * Handles a request to convert it to a response.
  72. *
  73. * Exceptions are not caught.
  74. *
  75. * @param Request $request A Request instance
  76. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  77. *
  78. * @return Response A Response instance
  79. *
  80. * @throws \LogicException If one of the listener does not behave as expected
  81. * @throws NotFoundHttpException When controller cannot be found
  82. */
  83. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  84. {
  85. // request
  86. $event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
  87. $response = $this->dispatcher->notifyUntil($event);
  88. if ($event->isProcessed()) {
  89. return $this->filterResponse($response, $request, 'A "core.request" listener returned a non response object.', $type);
  90. }
  91. // load controller
  92. if (false === $controller = $this->resolver->getController($request)) {
  93. 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()));
  94. }
  95. $event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
  96. $controller = $this->dispatcher->filter($event, $controller);
  97. // controller must be a callable
  98. if (!is_callable($controller)) {
  99. throw new \LogicException(sprintf('The controller must be a callable (%s given).', $this->varToString($controller)));
  100. }
  101. // controller arguments
  102. $arguments = $this->resolver->getArguments($request, $controller);
  103. // call controller
  104. $response = call_user_func_array($controller, $arguments);
  105. // view
  106. if (!$response instanceof Response) {
  107. $event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request, 'controller_value' => $response));
  108. $retval = $this->dispatcher->notifyUntil($event);
  109. if ($event->isProcessed()) {
  110. $response = $retval;
  111. }
  112. }
  113. return $this->filterResponse($response, $request, sprintf('The controller must return a response (%s given).', $this->varToString($response)), $type);
  114. }
  115. /**
  116. * Filters a response object.
  117. *
  118. * @param Response $response A Response instance
  119. * @param string $message A error message in case the response is not a Response object
  120. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  121. *
  122. * @return Response The filtered Response instance
  123. *
  124. * @throws \RuntimeException if the passed object is not a Response instance
  125. */
  126. protected function filterResponse($response, $request, $message, $type)
  127. {
  128. if (!$response instanceof Response) {
  129. throw new \RuntimeException($message);
  130. }
  131. $response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  132. if (!$response instanceof Response) {
  133. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  134. }
  135. return $response;
  136. }
  137. protected function varToString($var)
  138. {
  139. if (is_object($var)) {
  140. return sprintf('[object](%s)', get_class($var));
  141. }
  142. if (is_array($var)) {
  143. $a = array();
  144. foreach ($var as $k => $v) {
  145. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  146. }
  147. return sprintf("[array](%s)", implode(', ', $a));
  148. }
  149. if (is_resource($var)) {
  150. return '[resource]';
  151. }
  152. return str_replace("\n", '', var_export((string) $var, true));
  153. }
  154. }