Skip to content

Commit

Permalink
Correctly trigger kernel response events when handling exceptions (see
Browse files Browse the repository at this point in the history
…#1020)

Description
-----------

In #525 we added our own exception handling for legacy entry points. To correctly behave like a kernel, we also need to trigger all respective events. Without it, somehow the session does not get stored correctly.

The code is copied and simplified from `HttpKernel`. As this is Symfony 3.4, we have to use the old event names and order. Maybe this should be updated when merging into master.

fixes isotope/core#2087

Commits
-------

d45c4bf Correctly trigger kernel response events when handling exceptions
8e5c885 Add the missing use statements
  • Loading branch information
aschempp authored and leofeyer committed Nov 27, 2019
1 parent df26058 commit 9285b89
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core-bundle/src/Controller/InitializeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use Contao\CoreBundle\Response\InitializeControllerResponse;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\HttpKernel;
Expand Down Expand Up @@ -116,6 +118,21 @@ private function handleException(\Exception $e, Request $request, $type)
}
}

try {
$event = new FilterResponseEvent($this->get('http_kernel'), $request, $type, $response);
$this->get('event_dispatcher')->dispatch(KernelEvents::RESPONSE, $event);
$response = $event->getResponse();

$this->get('event_dispatcher')->dispatch(
KernelEvents::FINISH_REQUEST,
new FinishRequestEvent($this->get('http_kernel'), $request, $type)
);

$this->get('request_stack')->pop();
} catch (\Exception $e) {
// ignore and continue with original response
}

$response->send();
$this->get('kernel')->terminate($request, $response);
exit;
Expand Down

0 comments on commit 9285b89

Please sign in to comment.