You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
10 KiB

3 years ago
  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\HttpFoundation\Exception\RequestExceptionInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  16. use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
  17. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  18. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  19. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  20. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  21. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  22. use Symfony\Component\HttpKernel\Event\RequestEvent;
  23. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  24. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  25. use Symfony\Component\HttpKernel\Event\ViewEvent;
  26. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  27. use Symfony\Component\HttpKernel\Exception\ControllerDoesNotReturnResponseException;
  28. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  29. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  30. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  31. // Help opcache.preload discover always-needed symbols
  32. class_exists(LegacyEventDispatcherProxy::class);
  33. class_exists(ControllerArgumentsEvent::class);
  34. class_exists(ControllerEvent::class);
  35. class_exists(ExceptionEvent::class);
  36. class_exists(FinishRequestEvent::class);
  37. class_exists(RequestEvent::class);
  38. class_exists(ResponseEvent::class);
  39. class_exists(TerminateEvent::class);
  40. class_exists(ViewEvent::class);
  41. class_exists(KernelEvents::class);
  42. /**
  43. * HttpKernel notifies events to convert a Request object to a Response one.
  44. *
  45. * @author Fabien Potencier <fabien@symfony.com>
  46. */
  47. class HttpKernel implements HttpKernelInterface, TerminableInterface
  48. {
  49. protected $dispatcher;
  50. protected $resolver;
  51. protected $requestStack;
  52. private $argumentResolver;
  53. public function __construct(EventDispatcherInterface $dispatcher, ControllerResolverInterface $resolver, RequestStack $requestStack = null, ArgumentResolverInterface $argumentResolver = null)
  54. {
  55. $this->dispatcher = $dispatcher;
  56. $this->resolver = $resolver;
  57. $this->requestStack = $requestStack ?? new RequestStack();
  58. $this->argumentResolver = $argumentResolver;
  59. if (null === $this->argumentResolver) {
  60. $this->argumentResolver = new ArgumentResolver();
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function handle(Request $request, int $type = HttpKernelInterface::MAIN_REQUEST, bool $catch = true)
  67. {
  68. $request->headers->set('X-Php-Ob-Level', (string) ob_get_level());
  69. try {
  70. return $this->handleRaw($request, $type);
  71. } catch (\Exception $e) {
  72. if ($e instanceof RequestExceptionInterface) {
  73. $e = new BadRequestHttpException($e->getMessage(), $e);
  74. }
  75. if (false === $catch) {
  76. $this->finishRequest($request, $type);
  77. throw $e;
  78. }
  79. return $this->handleThrowable($e, $request, $type);
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function terminate(Request $request, Response $response)
  86. {
  87. $this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);
  88. }
  89. /**
  90. * @internal
  91. */
  92. public function terminateWithException(\Throwable $exception, Request $request = null)
  93. {
  94. if (!$request = $request ?: $this->requestStack->getMainRequest()) {
  95. throw $exception;
  96. }
  97. $response = $this->handleThrowable($exception, $request, self::MAIN_REQUEST);
  98. $response->sendHeaders();
  99. $response->sendContent();
  100. $this->terminate($request, $response);
  101. }
  102. /**
  103. * Handles a request to convert it to a response.
  104. *
  105. * Exceptions are not caught.
  106. *
  107. * @throws \LogicException If one of the listener does not behave as expected
  108. * @throws NotFoundHttpException When controller cannot be found
  109. */
  110. private function handleRaw(Request $request, int $type = self::MAIN_REQUEST): Response
  111. {
  112. $this->requestStack->push($request);
  113. // request
  114. $event = new RequestEvent($this, $request, $type);
  115. $this->dispatcher->dispatch($event, KernelEvents::REQUEST);
  116. if ($event->hasResponse()) {
  117. return $this->filterResponse($event->getResponse(), $request, $type);
  118. }
  119. // load controller
  120. if (false === $controller = $this->resolver->getController($request)) {
  121. throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". The route is wrongly configured.', $request->getPathInfo()));
  122. }
  123. $event = new ControllerEvent($this, $controller, $request, $type);
  124. $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
  125. $controller = $event->getController();
  126. // controller arguments
  127. $arguments = $this->argumentResolver->getArguments($request, $controller);
  128. $event = new ControllerArgumentsEvent($this, $controller, $arguments, $request, $type);
  129. $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
  130. $controller = $event->getController();
  131. $arguments = $event->getArguments();
  132. // call controller
  133. $response = $controller(...$arguments);
  134. // view
  135. if (!$response instanceof Response) {
  136. $event = new ViewEvent($this, $request, $type, $response);
  137. $this->dispatcher->dispatch($event, KernelEvents::VIEW);
  138. if ($event->hasResponse()) {
  139. $response = $event->getResponse();
  140. } else {
  141. $msg = sprintf('The controller must return a "Symfony\Component\HttpFoundation\Response" object but it returned %s.', $this->varToString($response));
  142. // the user may have forgotten to return something
  143. if (null === $response) {
  144. $msg .= ' Did you forget to add a return statement somewhere in your controller?';
  145. }
  146. throw new ControllerDoesNotReturnResponseException($msg, $controller, __FILE__, __LINE__ - 17);
  147. }
  148. }
  149. return $this->filterResponse($response, $request, $type);
  150. }
  151. /**
  152. * Filters a response object.
  153. *
  154. * @throws \RuntimeException if the passed object is not a Response instance
  155. */
  156. private function filterResponse(Response $response, Request $request, int $type): Response
  157. {
  158. $event = new ResponseEvent($this, $request, $type, $response);
  159. $this->dispatcher->dispatch($event, KernelEvents::RESPONSE);
  160. $this->finishRequest($request, $type);
  161. return $event->getResponse();
  162. }
  163. /**
  164. * Publishes the finish request event, then pop the request from the stack.
  165. *
  166. * Note that the order of the operations is important here, otherwise
  167. * operations such as {@link RequestStack::getParentRequest()} can lead to
  168. * weird results.
  169. */
  170. private function finishRequest(Request $request, int $type)
  171. {
  172. $this->dispatcher->dispatch(new FinishRequestEvent($this, $request, $type), KernelEvents::FINISH_REQUEST);
  173. $this->requestStack->pop();
  174. }
  175. /**
  176. * Handles a throwable by trying to convert it to a Response.
  177. *
  178. * @throws \Exception
  179. */
  180. private function handleThrowable(\Throwable $e, Request $request, int $type): Response
  181. {
  182. $event = new ExceptionEvent($this, $request, $type, $e);
  183. $this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
  184. // a listener might have replaced the exception
  185. $e = $event->getThrowable();
  186. if (!$event->hasResponse()) {
  187. $this->finishRequest($request, $type);
  188. throw $e;
  189. }
  190. $response = $event->getResponse();
  191. // the developer asked for a specific status code
  192. if (!$event->isAllowingCustomResponseCode() && !$response->isClientError() && !$response->isServerError() && !$response->isRedirect()) {
  193. // ensure that we actually have an error response
  194. if ($e instanceof HttpExceptionInterface) {
  195. // keep the HTTP status code and headers
  196. $response->setStatusCode($e->getStatusCode());
  197. $response->headers->add($e->getHeaders());
  198. } else {
  199. $response->setStatusCode(500);
  200. }
  201. }
  202. try {
  203. return $this->filterResponse($response, $request, $type);
  204. } catch (\Exception $e) {
  205. return $response;
  206. }
  207. }
  208. /**
  209. * Returns a human-readable string for the specified variable.
  210. */
  211. private function varToString($var): string
  212. {
  213. if (\is_object($var)) {
  214. return sprintf('an object of type %s', \get_class($var));
  215. }
  216. if (\is_array($var)) {
  217. $a = [];
  218. foreach ($var as $k => $v) {
  219. $a[] = sprintf('%s => ...', $k);
  220. }
  221. return sprintf('an array ([%s])', mb_substr(implode(', ', $a), 0, 255));
  222. }
  223. if (\is_resource($var)) {
  224. return sprintf('a resource (%s)', get_resource_type($var));
  225. }
  226. if (null === $var) {
  227. return 'null';
  228. }
  229. if (false === $var) {
  230. return 'a boolean value (false)';
  231. }
  232. if (true === $var) {
  233. return 'a boolean value (true)';
  234. }
  235. if (\is_string($var)) {
  236. return sprintf('a string ("%s%s")', mb_substr($var, 0, 255), mb_strlen($var) > 255 ? '...' : '');
  237. }
  238. if (is_numeric($var)) {
  239. return sprintf('a number (%s)', (string) $var);
  240. }
  241. return (string) $var;
  242. }
  243. }