/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/HttpKernel.php

https://bitbucket.org/laborautonomo/laborautonomo-site · PHP · 255 lines · 128 code · 37 blank · 90 comment · 19 complexity · 41b19213bd3d7f319ce2b9aa3be60f9e MD5 · raw file

  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\HttpKernel\Controller\ControllerResolverInterface;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  14. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  15. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  16. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  17. use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
  18. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  19. use Symfony\Component\HttpKernel\Event\PostResponseEvent;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  23. /**
  24. * HttpKernel notifies events to convert a Request object to a Response one.
  25. *
  26. * @author Fabien Potencier <fabien@symfony.com>
  27. *
  28. * @api
  29. */
  30. class HttpKernel implements HttpKernelInterface, TerminableInterface
  31. {
  32. protected $dispatcher;
  33. protected $resolver;
  34. /**
  35. * Constructor
  36. *
  37. * @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
  38. * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
  39. *
  40. * @api
  41. */
  42. public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver)
  43. {
  44. $this->dispatcher = $dispatcher;
  45. $this->resolver = $resolver;
  46. }
  47. /**
  48. * Handles a Request to convert it to a Response.
  49. *
  50. * When $catch is true, the implementation must catch all exceptions
  51. * and do its best to convert them to a Response instance.
  52. *
  53. * @param Request $request A Request instance
  54. * @param integer $type The type of the request
  55. * (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  56. * @param Boolean $catch Whether to catch exceptions or not
  57. *
  58. * @return Response A Response instance
  59. *
  60. * @throws \Exception When an Exception occurs during processing
  61. *
  62. * @api
  63. */
  64. public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
  65. {
  66. try {
  67. return $this->handleRaw($request, $type);
  68. } catch (\Exception $e) {
  69. if (false === $catch) {
  70. throw $e;
  71. }
  72. return $this->handleException($e, $request, $type);
  73. }
  74. }
  75. /**
  76. * {@inheritdoc}
  77. *
  78. * @api
  79. */
  80. public function terminate(Request $request, Response $response)
  81. {
  82. $this->dispatcher->dispatch(KernelEvents::TERMINATE, new PostResponseEvent($this, $request, $response));
  83. }
  84. /**
  85. * Handles a request to convert it to a response.
  86. *
  87. * Exceptions are not caught.
  88. *
  89. * @param Request $request A Request instance
  90. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  91. *
  92. * @return Response A Response instance
  93. *
  94. * @throws \LogicException If one of the listener does not behave as expected
  95. * @throws NotFoundHttpException When controller cannot be found
  96. */
  97. private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
  98. {
  99. // request
  100. $event = new GetResponseEvent($this, $request, $type);
  101. $this->dispatcher->dispatch(KernelEvents::REQUEST, $event);
  102. if ($event->hasResponse()) {
  103. return $this->filterResponse($event->getResponse(), $request, $type);
  104. }
  105. // load controller
  106. if (false === $controller = $this->resolver->getController($request)) {
  107. 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()));
  108. }
  109. $event = new FilterControllerEvent($this, $controller, $request, $type);
  110. $this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
  111. $controller = $event->getController();
  112. // controller arguments
  113. $arguments = $this->resolver->getArguments($request, $controller);
  114. // call controller
  115. $response = call_user_func_array($controller, $arguments);
  116. // view
  117. if (!$response instanceof Response) {
  118. $event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
  119. $this->dispatcher->dispatch(KernelEvents::VIEW, $event);
  120. if ($event->hasResponse()) {
  121. $response = $event->getResponse();
  122. }
  123. if (!$response instanceof Response) {
  124. $msg = sprintf('The controller must return a response (%s given).', $this->varToString($response));
  125. // the user may have forgotten to return something
  126. if (null === $response) {
  127. $msg .= ' Did you forget to add a return statement somewhere in your controller?';
  128. }
  129. throw new \LogicException($msg);
  130. }
  131. }
  132. return $this->filterResponse($response, $request, $type);
  133. }
  134. /**
  135. * Filters a response object.
  136. *
  137. * @param Response $response A Response instance
  138. * @param Request $request A error message in case the response is not a Response object
  139. * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
  140. *
  141. * @return Response The filtered Response instance
  142. *
  143. * @throws \RuntimeException if the passed object is not a Response instance
  144. */
  145. private function filterResponse(Response $response, Request $request, $type)
  146. {
  147. $event = new FilterResponseEvent($this, $request, $type, $response);
  148. $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event);
  149. return $event->getResponse();
  150. }
  151. /**
  152. * Handles an exception by trying to convert it to a Response.
  153. *
  154. * @param \Exception $e An \Exception instance
  155. * @param Request $request A Request instance
  156. * @param integer $type The type of the request
  157. *
  158. * @return Response A Response instance
  159. *
  160. * @throws \Exception
  161. */
  162. private function handleException(\Exception $e, $request, $type)
  163. {
  164. $event = new GetResponseForExceptionEvent($this, $request, $type, $e);
  165. $this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
  166. // a listener might have replaced the exception
  167. $e = $event->getException();
  168. if (!$event->hasResponse()) {
  169. throw $e;
  170. }
  171. $response = $event->getResponse();
  172. // the developer asked for a specific status code
  173. if ($response->headers->has('X-Status-Code')) {
  174. $response->setStatusCode($response->headers->get('X-Status-Code'));
  175. $response->headers->remove('X-Status-Code');
  176. } elseif (!$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
  177. // ensure that we actually have an error response
  178. if ($e instanceof HttpExceptionInterface) {
  179. // keep the HTTP status code and headers
  180. $response->setStatusCode($e->getStatusCode());
  181. $response->headers->add($e->getHeaders());
  182. } else {
  183. $response->setStatusCode(500);
  184. }
  185. }
  186. try {
  187. return $this->filterResponse($response, $request, $type);
  188. } catch (\Exception $e) {
  189. return $response;
  190. }
  191. }
  192. private function varToString($var)
  193. {
  194. if (is_object($var)) {
  195. return sprintf('Object(%s)', get_class($var));
  196. }
  197. if (is_array($var)) {
  198. $a = array();
  199. foreach ($var as $k => $v) {
  200. $a[] = sprintf('%s => %s', $k, $this->varToString($v));
  201. }
  202. return sprintf("Array(%s)", implode(', ', $a));
  203. }
  204. if (is_resource($var)) {
  205. return sprintf('Resource(%s)', get_resource_type($var));
  206. }
  207. if (null === $var) {
  208. return 'null';
  209. }
  210. if (false === $var) {
  211. return 'false';
  212. }
  213. if (true === $var) {
  214. return 'true';
  215. }
  216. return (string) $var;
  217. }
  218. }