Skip to content

Commit 46171b2

Browse files
bendaviesdunglas
authored andcommitted
Fix symfony 4.4 compat (#3285)
* rename for clarity * correctly detect if we can use the new ErrorListener in symfony 4.4 The ErrorListener may be remvoved from the container if deprecated configuration is used. We must use the deprecated Exception listener if so. https://github.com/symfony/symfony/blob/bfae515d521a421b413cb39e8fae727ffb4ca28b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php#L37
1 parent 86fd082 commit 46171b2

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@
214214
<service id="api_platform.listener.exception" class="ApiPlatform\Core\EventListener\ExceptionListener">
215215
<argument>api_platform.action.exception</argument>
216216
<argument type="service" id="logger" on-invalid="null" />
217+
<argument>false</argument>
218+
<argument type="service" id="exception_listener" on-invalid="null" />
217219

218220
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-96" />
219221
<tag name="monolog.logger" channel="request" />

src/EventListener/ExceptionListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1919
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
20-
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
20+
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as LegacyExceptionListener;
2121

2222
/**
2323
* Handles requests errors.
@@ -29,12 +29,12 @@ final class ExceptionListener
2929
{
3030
private $exceptionListener;
3131

32-
public function __construct($controller, LoggerInterface $logger = null, $debug = false)
32+
public function __construct($controller, LoggerInterface $logger = null, $debug = false, ErrorListener $errorListener = null)
3333
{
34-
if (class_exists(ErrorListener::class)) {
34+
if (null !== $errorListener) {
3535
$this->exceptionListener = new ErrorListener($controller, $logger, $debug);
3636
} else {
37-
$this->exceptionListener = new BaseExceptionListener($controller, $logger, $debug);
37+
$this->exceptionListener = new LegacyExceptionListener($controller, $logger, $debug);
3838
}
3939
}
4040

tests/EventListener/ExceptionListenerTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\HttpFoundation\Request;
2020
use Symfony\Component\HttpFoundation\Response;
2121
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
22+
use Symfony\Component\HttpKernel\EventListener\ErrorListener;
2223
use Symfony\Component\HttpKernel\HttpKernelInterface;
2324

2425
/**
@@ -44,7 +45,7 @@ public function testOnKernelException(Request $request)
4445
$eventProphecy->getKernel()->willReturn($kernel);
4546
$eventProphecy->setResponse(Argument::type(Response::class))->shouldBeCalled();
4647

47-
$listener = new ExceptionListener('foo:bar');
48+
$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
4849
$listener->onKernelException($eventProphecy->reveal());
4950
}
5051

@@ -62,7 +63,7 @@ public function testDoNothingWhenNotAnApiCall()
6263
$eventProphecy->getRequest()->willReturn(new Request());
6364
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
6465

65-
$listener = new ExceptionListener('foo:bar');
66+
$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
6667
$listener->onKernelException($eventProphecy->reveal());
6768
}
6869

@@ -75,7 +76,7 @@ public function testDoNothingWhenHtmlRequested()
7576
$eventProphecy->getRequest()->willReturn($request);
7677
$eventProphecy->setResponse(Argument::type(Response::class))->shouldNotBeCalled();
7778

78-
$listener = new ExceptionListener('foo:bar');
79+
$listener = new ExceptionListener('foo:bar', null, false, class_exists(ErrorListener::class) ? $this->prophesize(ErrorListener::class)->reveal() : null);
7980
$listener->onKernelException($eventProphecy->reveal());
8081
}
8182
}

0 commit comments

Comments
 (0)