13
13
14
14
use Psr \Log \LoggerInterface ;
15
15
use Symfony \Component \Debug \Exception \FlattenException ;
16
+ use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
16
17
use Symfony \Component \HttpFoundation \Request ;
17
18
use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
18
19
use Symfony \Component \HttpKernel \Log \DebugLoggerInterface ;
@@ -31,7 +32,6 @@ class ExceptionListener implements EventSubscriberInterface
31
32
{
32
33
protected $ controller ;
33
34
protected $ logger ;
34
- private $ exceptionHasBeenThrown = false ;
35
35
36
36
public function __construct ($ controller , LoggerInterface $ logger = null )
37
37
{
@@ -43,7 +43,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
43
43
{
44
44
$ exception = $ event ->getException ();
45
45
$ request = $ event ->getRequest ();
46
- $ this -> exceptionHasBeenThrown = true ;
46
+ $ eventDispatcher = func_get_arg ( 2 ) ;
47
47
48
48
$ this ->logException ($ exception , sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , get_class ($ exception ), $ exception ->getMessage (), $ exception ->getFile (), $ exception ->getLine ()));
49
49
@@ -70,20 +70,21 @@ public function onKernelException(GetResponseForExceptionEvent $event)
70
70
}
71
71
72
72
$ event ->setResponse ($ response );
73
- }
74
73
75
- public function onKernelResponse (FilterResponseEvent $ event )
76
- {
77
- if ($ this ->exceptionHasBeenThrown ) {
74
+ $ cspRemovalListener = function (FilterResponseEvent $ event ) use (&$ cspRemovalListener , $ eventDispatcher ) {
78
75
$ 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 );
79
81
}
80
82
}
81
83
82
84
public static function getSubscribedEvents ()
83
85
{
84
86
return array (
85
87
KernelEvents::EXCEPTION => array ('onKernelException ' , -128 ),
86
- KernelEvents::RESPONSE => array ('onKernelResponse ' , -128 ),
87
88
);
88
89
}
89
90
0 commit comments