14
14
use Psr \Log \LoggerInterface ;
15
15
use Symfony \Component \Debug \Exception \FlattenException as LegacyFlattenException ;
16
16
use Symfony \Component \ErrorHandler \Exception \FlattenException ;
17
- use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
18
17
use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
19
18
use Symfony \Component \HttpFoundation \Request ;
20
19
use Symfony \Component \HttpKernel \Event \ControllerArgumentsEvent ;
21
20
use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
21
+ use Symfony \Component \HttpKernel \Event \ResponseEvent ;
22
22
use Symfony \Component \HttpKernel \Exception \HttpExceptionInterface ;
23
23
use Symfony \Component \HttpKernel \HttpKernelInterface ;
24
24
use Symfony \Component \HttpKernel \KernelEvents ;
@@ -33,7 +33,7 @@ class ErrorListener implements EventSubscriberInterface
33
33
protected $ logger ;
34
34
protected $ debug ;
35
35
36
- public function __construct ($ controller , LoggerInterface $ logger = null , $ debug = false )
36
+ public function __construct ($ controller , LoggerInterface $ logger = null , bool $ debug = false )
37
37
{
38
38
$ this ->controller = $ controller ;
39
39
$ this ->logger = $ logger ;
@@ -47,7 +47,7 @@ public function logKernelException(ExceptionEvent $event)
47
47
$ this ->logException ($ event ->getThrowable (), sprintf ('Uncaught PHP Exception %s: "%s" at %s line %s ' , $ e ->getClass (), $ e ->getMessage (), $ e ->getFile (), $ e ->getLine ()));
48
48
}
49
49
50
- public function onKernelException (ExceptionEvent $ event, string $ eventName = null , EventDispatcherInterface $ eventDispatcher = null )
50
+ public function onKernelException (ExceptionEvent $ event )
51
51
{
52
52
if (null === $ this ->controller ) {
53
53
return ;
@@ -79,12 +79,15 @@ public function onKernelException(ExceptionEvent $event, string $eventName = nul
79
79
80
80
$ event ->setResponse ($ response );
81
81
82
- if ($ this ->debug && $ eventDispatcher instanceof EventDispatcherInterface) {
83
- $ cspRemovalListener = function ($ event ) use (&$ cspRemovalListener , $ eventDispatcher ) {
84
- $ event ->getResponse ()->headers ->remove ('Content-Security-Policy ' );
85
- $ eventDispatcher ->removeListener (KernelEvents::RESPONSE , $ cspRemovalListener );
86
- };
87
- $ eventDispatcher ->addListener (KernelEvents::RESPONSE , $ cspRemovalListener , -128 );
82
+ if ($ this ->debug ) {
83
+ $ event ->getRequest ()->attributes ->set ('_remove_csp_headers ' , true );
84
+ }
85
+ }
86
+
87
+ public function removeCspHeader (ResponseEvent $ event ): void
88
+ {
89
+ if ($ event ->getRequest ()->attributes ->get ('_remove_csp_headers ' , false )) {
90
+ $ event ->getResponse ()->headers ->remove ('Content-Security-Policy ' );
88
91
}
89
92
}
90
93
@@ -114,6 +117,7 @@ public static function getSubscribedEvents(): array
114
117
['logKernelException ' , 0 ],
115
118
['onKernelException ' , -128 ],
116
119
],
120
+ KernelEvents::RESPONSE => ['removeCspHeader ' , -128 ],
117
121
];
118
122
}
119
123
0 commit comments