Skip to content

Instantly share code, notes, and snippets.

@anvodev
Last active May 3, 2020 13:35
Show Gist options
  • Save anvodev/2ef085ce76ad1dcec41a8a92a5766313 to your computer and use it in GitHub Desktop.
Save anvodev/2ef085ce76ad1dcec41a8a92a5766313 to your computer and use it in GitHub Desktop.
The main part of handle function in HttpKernel.php in Symfony 5
<?php
public function handle(Request $request, int $type = HttpKernelInterface::MASTER_REQUEST, bool $catch = true)
{
try {
return $this->handleRaw($request, $type);
} catch (\Exception $e) {
return $this->handleThrowable($e, $request, $type);
}
}
private function handleRaw(Request $request, int $type = self::MASTER_REQUEST): Response
{
// request
$this->requestStack->push($request);
$event = new RequestEvent($this, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);
// controller
$controller = $this->resolver->getController($request);
$event = new ControllerEvent($this, $controller, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER);
// controller arguments
$arguments = $this->argumentResolver->getArguments($request, $controller);
$event = new ControllerArgumentsEvent($this, $controller, $arguments, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);
// call controller
$response = $controller(...$arguments);
// view
if (!$response instanceof Response) {
$event = new ViewEvent($this, $request, $type, $response);
$this->dispatcher->dispatch($event, KernelEvents::VIEW);
$response = $event->getResponse();
}
return $this->filterResponse($response, $request, $type);
}
private function filterResponse(Response $response, Request $request, int $type): Response
{
$event = new ResponseEvent($this, $request, $type, $response);
$this->dispatcher->dispatch($event, KernelEvents::RESPONSE);
$this->finishRequest($request, $type);
return $event->getResponse();
}
private function finishRequest(Request $request, int $type)
{
$this->dispatcher->dispatch(new FinishRequestEvent($this, $request, $type), KernelEvents::FINISH_REQUEST);
$this->requestStack->pop();
}
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
{
$event = new ExceptionEvent($this, $request, $type, $e);
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
$response = $event->getResponse();
return $this->filterResponse($response, $request, $type);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment