HttpKernel.php 6.9 KB

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