HttpKernel.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace Symfony\Components\HttpKernel;
  3. use Symfony\Components\EventDispatcher\Event;
  4. use Symfony\Components\EventDispatcher\EventDispatcher;
  5. use Symfony\Components\HttpKernel\Exception\NotFoundHttpException;
  6. use Symfony\Components\HttpFoundation\Request;
  7. use Symfony\Components\HttpFoundation\Response;
  8. /*
  9. * This file is part of the Symfony package.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * For the full copyright and license information, please view the LICENSE
  14. * file that was distributed with this source code.
  15. */
  16. /**
  17. * HttpKernel notifies events to convert a Request object to a Response one.
  18. *
  19. * @package Symfony
  20. * @subpackage Components_HttpKernel
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. */
  23. class HttpKernel implements HttpKernelInterface
  24. {
  25. protected $dispatcher;
  26. protected $request;
  27. /**
  28. * Constructor
  29. *
  30. * @param EventDispatcher $dispatcher An event dispatcher instance
  31. */
  32. public function __construct(EventDispatcher $dispatcher)
  33. {
  34. $this->dispatcher = $dispatcher;
  35. }
  36. /**
  37. * Gets the Request instance associated with the master request.
  38. *
  39. * @return Request A Request instance
  40. */
  41. public function getRequest()
  42. {
  43. return $this->request;
  44. }
  45. /**
  46. * Handles a Request to convert it to a Response.
  47. *
  48. * All exceptions are caught, and a core.exception event is notified
  49. * for user management.
  50. *
  51. * @param Request $request A Request instance
  52. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
  53. * @param Boolean $raw Whether to catch exceptions or not
  54. *
  55. * @return Response A Response instance
  56. *
  57. * @throws \Exception When an Exception occurs during processing
  58. * and couldn't be caught by event processing or $raw is true
  59. */
  60. public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
  61. {
  62. if (HttpKernelInterface::EMBEDDED_REQUEST === $type) {
  63. return $this->handleEmbedded($request, $raw);
  64. }
  65. if (null === $request) {
  66. $request = new Request();
  67. }
  68. if (HttpKernelInterface::MASTER_REQUEST === $type) {
  69. $this->request = $request;
  70. }
  71. try {
  72. return $this->handleRaw($request, $type);
  73. } catch (\Exception $e) {
  74. if (true === $raw) {
  75. throw $e;
  76. }
  77. // exception
  78. $event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e)));
  79. if ($event->isProcessed()) {
  80. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
  81. }
  82. throw $e;
  83. }
  84. }
  85. /**
  86. * Handles a request to convert it to a response.
  87. *
  88. * Exceptions are not caught.
  89. *
  90. * @param Request $request A Request instance
  91. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
  92. *
  93. * @return Response A Response instance
  94. *
  95. * @throws \LogicException If one of the listener does not behave as expected
  96. * @throws NotFoundHttpException When controller cannot be found
  97. */
  98. protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  99. {
  100. // request
  101. $event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request)));
  102. if ($event->isProcessed()) {
  103. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
  104. }
  105. // load controller
  106. $event = $this->dispatcher->notifyUntil(new Event($this, 'core.load_controller', array('request_type' => $type, 'request' => $request)));
  107. if (!$event->isProcessed()) {
  108. throw new NotFoundHttpException('Unable to find the controller.');
  109. }
  110. list($controller, $arguments) = $event->getReturnValue();
  111. // controller must be a callable
  112. if (!is_callable($controller)) {
  113. throw new \LogicException(sprintf('The controller must be a callable (%s).', var_export($controller, true)));
  114. }
  115. // controller
  116. $event = $this->dispatcher->notifyUntil(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request, 'controller' => &$controller, 'arguments' => &$arguments)));
  117. if ($event->isProcessed()) {
  118. try {
  119. return $this->filterResponse($event->getReturnValue(), $request, 'A "core.controller" listener returned a non response object.', $type);
  120. } catch (\Exception $e) {
  121. $retval = $event->getReturnValue();
  122. }
  123. } else {
  124. // call controller
  125. $retval = call_user_func_array($controller, $arguments);
  126. }
  127. // view
  128. $event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval);
  129. 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);
  130. }
  131. /**
  132. * Handles a request that need to be embedded.
  133. *
  134. * @param Request $request A Request instance
  135. * @param Boolean $raw Whether to catch exceptions or not
  136. *
  137. * @return string|false The Response content or false if there is a problem
  138. *
  139. * @throws \RuntimeException When an Exception occurs during processing
  140. * and couldn't be caught by event processing or $raw is true
  141. */
  142. protected function handleEmbedded(Request $request, $raw = false)
  143. {
  144. try {
  145. $response = $this->handleRaw($request, HttpKernelInterface::EMBEDDED_REQUEST);
  146. if (200 != $response->getStatusCode()) {
  147. throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
  148. }
  149. return $response->getContent();
  150. } catch (\Exception $e) {
  151. if (true === $raw)
  152. {
  153. throw $e;
  154. }
  155. return false;
  156. }
  157. }
  158. /**
  159. * Filters a response object.
  160. *
  161. * @param Response $response A Response instance
  162. * @param string $message A error message in case the response is not a Response object
  163. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST, HttpKernelInterface::FORWARDED_REQUEST, or HttpKernelInterface::EMBEDDED_REQUEST)
  164. *
  165. * @return Response The filtered Response instance
  166. *
  167. * @throws \RuntimeException if the passed object is not a Response instance
  168. */
  169. protected function filterResponse($response, $request, $message, $type)
  170. {
  171. if (!$response instanceof Response) {
  172. throw new \RuntimeException($message);
  173. }
  174. $event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
  175. $response = $event->getReturnValue();
  176. if (!$response instanceof Response) {
  177. throw new \RuntimeException('A "core.response" listener returned a non response object.');
  178. }
  179. return $response;
  180. }
  181. }