Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Symfony/Component/HttpKernel/HttpKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
{
$request->headers->set('X-Php-Ob-Level', (string) ob_get_level());

$this->requestStack->push($request);
try {
return $this->handleRaw($request, $type);
} catch (\Exception $e) {
Expand All @@ -89,6 +90,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
}

return $this->handleThrowable($e, $request, $type);
} finally {
$this->requestStack->pop();
}
}

Expand Down Expand Up @@ -127,8 +130,6 @@ public function terminateWithException(\Throwable $exception, Request $request =
*/
private function handleRaw(Request $request, int $type = self::MASTER_REQUEST): Response
{
$this->requestStack->push($request);

// request
$event = new RequestEvent($this, $request, $type);
$this->dispatcher->dispatch($event, KernelEvents::REQUEST);
Expand Down Expand Up @@ -205,7 +206,6 @@ private function filterResponse(Response $response, Request $request, int $type)
private function finishRequest(Request $request, int $type)
{
$this->dispatcher->dispatch(new FinishRequestEvent($this, $request, $type), KernelEvents::FINISH_REQUEST);
$this->requestStack->pop();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,45 @@ public function testHandleWhenControllerThrowsAnExceptionAndCatchIsTrue()
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
}

public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsTrue()
{
$requestStack = new RequestStack();
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);

try {
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
} catch (\Throwable $exception) {
}

self::assertNull($requestStack->getCurrentRequest());
}

public function testRequestStackIsNotBrokenWhenControllerThrowsAnExceptionAndCatchIsFalse()
{
$requestStack = new RequestStack();
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \RuntimeException(); }, $requestStack);

try {
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, false);
} catch (\Throwable $exception) {
}

self::assertNull($requestStack->getCurrentRequest());
}

public function testRequestStackIsNotBrokenWhenControllerThrowsAnThrowable()
{
$requestStack = new RequestStack();
$kernel = $this->getHttpKernel(new EventDispatcher(), function () { throw new \Error(); }, $requestStack);

try {
$kernel->handle(new Request(), HttpKernelInterface::MASTER_REQUEST, true);
} catch (\Throwable $exception) {
}

self::assertNull($requestStack->getCurrentRequest());
}

public function testHandleWhenControllerThrowsAnExceptionAndCatchIsFalseAndNoListenerIsRegistered()
{
$this->expectException(\RuntimeException::class);
Expand Down