Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b40940

Browse filesBrowse files
committed
Attach listener dynamically
1 parent 7ca7d86 commit 4b40940
Copy full SHA for 4b40940

File tree

Expand file treeCollapse file tree

1 file changed

+8
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+8
-7
lines changed

‎src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php
+8-7Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\Debug\Exception\FlattenException;
16+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1819
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
@@ -31,7 +32,6 @@ class ExceptionListener implements EventSubscriberInterface
3132
{
3233
protected $controller;
3334
protected $logger;
34-
private $exceptionHasBeenThrown = false;
3535

3636
public function __construct($controller, LoggerInterface $logger = null)
3737
{
@@ -43,7 +43,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
4343
{
4444
$exception = $event->getException();
4545
$request = $event->getRequest();
46-
$this->exceptionHasBeenThrown = true;
46+
$eventDispatcher = func_get_arg(2);
4747

4848
$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
4949

@@ -70,20 +70,21 @@ public function onKernelException(GetResponseForExceptionEvent $event)
7070
}
7171

7272
$event->setResponse($response);
73-
}
7473

75-
public function onKernelResponse(FilterResponseEvent $event)
76-
{
77-
if ($this->exceptionHasBeenThrown) {
74+
$cspRemovalListener = function (FilterResponseEvent $event) use (&$cspRemovalListener, $eventDispatcher) {
7875
$event->getResponse()->headers->remove('Content-Security-Policy');
76+
$eventDispatcher->removeListener(KernelEvents::RESPONSE, $cspRemovalListener);
77+
};
78+
79+
if ($eventDispatcher instanceof EventDispatcherInterface) {
80+
$eventDispatcher->addListener(KernelEvents::RESPONSE, $cspRemovalListener, -128);
7981
}
8082
}
8183

8284
public static function getSubscribedEvents()
8385
{
8486
return array(
8587
KernelEvents::EXCEPTION => array('onKernelException', -128),
86-
KernelEvents::RESPONSE => array('onKernelResponse', -128),
8788
);
8889
}
8990

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.