8000 bug #38350 [TwigBundle] Only remove kernel exception listener if twig… · lucasaba/symfony@dadce4b · GitHub
[go: up one dir, main page]

Skip to content

Commit dadce4b

Browse files
bug symfony#38350 [TwigBundle] Only remove kernel exception listener if twig is used (dmolineus)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [TwigBundle] Only remove kernel exception listener if twig is used | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | contao/contao#1527 | License | MIT | Doc PR | In a setup using the template engine but not twig as the template engine no exceptions are logged. This is caused by the twig-bundle which removes the `exception_listener` service. For my understanding this should only happen if twig is used as template engine. This PR fixes the logic that only for the case where twig is enabled as template engine the http kernel exception listener is removed. Otherwise the twig exception listener got removed now. Disclaimer: I'm not too deep into the details, so maybe I oversee something why it's implemented the way it is. Commits ------- 7c34f6e [TwigBundle] Only remove kernel exception listener if twig is used
2 parents 8731a4f + 7c34f6e commit dadce4b

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed
10000

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ public function process(ContainerBuilder $container)
3333
// register the exception listener only if it's currently used, else use the provided by FrameworkBundle
3434
if (null === $container->getParameter('twig.exception_listener.controller') && $container->hasDefinition('exception_listener')) {
3535
$container->removeDefinition('twig.exception_listener');
36-
} else {
37-
$container->removeDefinition('exception_listener');
38-
39-
if ($container->hasParameter('templating.engines')) {
40-
$engines = $container->getParameter('templating.engines');
41-
if (!\in_array('twig', $engines, true)) {
42-
$container->removeDefinition('twig.exception_listener');
43-
}
36+
37+
return;
38+
}
39+
40+
if ($container->hasParameter('templating.engines')) {
41+
$engines = $container->getParameter('templating.engines');
42+
if (\in_array('twig', $engines, true)) {
43+
$container->removeDefinition('exception_listener');
44+
45+
return;
4446
}
4547
}
48+
49+
$container->removeDefinition('twig.exception_listener');
4650
}
4751
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Symfony\Bundle\TwigBundle\Tests\DependencyInjection\Compiler;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\Security\Http\Firewall\ExceptionListener;
20+
use Twig\Environment;
21+
22+
class ExceptionListenerPassTest extends TestCase
23+
{
24+
public function testExitsWhenTwigIsNotAvailable(): void
25+
{
26+
$builder = new ContainerBuilder();
27+
$builder->register('exception_listener', ExceptionListener::class);
28+
$builder->register('twig.exception_listener', ExceptionListener::class);
29+
30+
($pass = new ExceptionListenerPass())->process($builder);
31+
32+
$this->assertTrue($builder->hasDefinition('exception_listener'));
33+
$this->assertTrue($builder->hasDefinition('twig.exception_listener'));
34+
}
35+
36+
public function testRemovesTwigExceptionListenerWhenNoExceptionListenerControllerExists(): void
37+
{
38+
$builder = new ContainerBuilder();
39+
$builder->register('twig', Environment::class);
40+
$builder->register('exception_listener', ExceptionListener::class);
41+
$builder->register('twig.exception_listener', ExceptionListener::class);
42+
$builder->setParameter('twig.exception_listener.controller', null);
43+
44+
($pass = new ExceptionListenerPass())->process($builder);
45+
46+
$this->assertTrue($builder->hasDefinition('exception_listener'));
47+
$this->assertFalse($builder->hasDefinition('twig.exception_listener'));
48+
}
49+
50+
public function testRemovesTwigExceptionListenerIfTwigIsNotUsedAsTemplateEngine(): void
51+
{
52+
$builder = new ContainerBuilder();
53+
$builder->register('twig', Environment::class);
54+
$builder->register('exception_listener', ExceptionListener::class);
55+
$builder->register('twig.exception_listener', ExceptionListener::class);
56+
$builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction');
57+
$builder->setParameter('templating.engines', ['php']);
58+
59+
($pass = new ExceptionListenerPass())->process($builder);
60+
61+
$this->assertTrue($builder->hasDefinition('exception_listener'));
62+
$this->assertFalse($builder->hasDefinition('twig.exception_listener'));
63+
}
64+
65+
public function testRemovesKernelExceptionListenerIfTwigIsUsedAsTemplateEngine(): void
66+
{
67+
$builder = new ContainerBuilder();
68+
$builder->register('twig', Environment::class);
69+
$builder->register('exception_listener', ExceptionListener::class);
70+
$builder->register('twig.exception_listener', ExceptionListener::class);
71+
$builder->setParameter('twig.exception_listener.controller', 'exception_controller::showAction');
72+
$builder->setParameter('templating.engines', ['twig']);
73+
74+
($pass = new ExceptionListenerPass())->process($builder);
75+
76+
$this->assertFalse($builder->hasDefinition('exception_listener'));
77+
$this->assertTrue($builder->hasDefinition('twig.exception_listener'));
78+
}
79+
}

0 commit comments

Comments
 (0)
0