Skip to content

Commit c266ab1

Browse files
committed
bug #36261 [FrameworkBundle] revert to legacy wiring of the session when circular refs are detected (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [FrameworkBundle] revert to legacy wiring of the session when circular refs are detected | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #36063 | License | MIT | Doc PR | - As introduced and reported in the linked PR. Commits ------- 35644cf [FrameworkBundle] revert to legacy wiring of the session when circular refs are detected
2 parents 2555bff + 35644cf commit c266ab1

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
18+
/**
19+
* @internal to be removed in 6.0
20+
*/
21+
class SessionPass implements CompilerPassInterface
22+
{
23+
public function process(ContainerBuilder $container)
24+
{
25+
if (!$container->hasDefinition('session')) {
26+
return;
27+
}
28+
29+
$bags = [
30+
'session.flash_bag' => $container->hasDefinition('session.flash_bag') ? $container->getDefinition('session.flash_bag') : null,
31+
'session.attribute_bag' => $container->hasDefinition('session.attribute_bag') ? $container->getDefinition('session.attribute_bag') : null,
32+
];
33+
34+
foreach ($container->getDefinition('session')->getArguments() as $v) {
35+
if (!$v instanceof Reference || !isset($bags[$bag = (string) $v]) || !\is_array($factory = $bags[$bag]->getFactory())) {
36+
continue;
37+
}
38+
39+
if ([0, 1] !== array_keys($factory) || !$factory[0] instanceof Reference || 'session' !== (string) $factory[0]) {
40+
continue;
41+
}
42+
43+
if ('get'.ucfirst(substr($bag, 8, -4)).'Bag' !== $factory[1]) {
44+
continue;
45+
}
46+
47+
$bags[$bag]->setFactory(null);
48+
}
49+
}
50+
}

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\DataCollectorTranslatorPass;
2323
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\LoggingTranslatorPass;
2424
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ProfilerPass;
25+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SessionPass;
2526
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
2627
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
2728
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\WorkflowGuardListenerPass;
@@ -125,6 +126,7 @@ public function build(ContainerBuilder $container)
125126
$this->addCompilerPassIfExists($container, FormPass::class);
126127
$container->addCompilerPass(new WorkflowGuardListenerPass());
127128
$container->addCompilerPass(new ResettableServicePass());
129+
$container->addCompilerPass(new SessionPass());
128130

129131
if ($container->getParameter('kernel.debug')) {
130132
$container->addCompilerPass(new AddDebugLogProcessorPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -32);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
namespace Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SessionPass;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
class SessionPassTest extends TestCase
20+
{
21+
public function testProcess()
22+
{
23+
$arguments = [
24+
new Reference('session.flash_bag'),
25+
new Reference('session.attribute_bag'),
26+
];
27+
$container = new ContainerBuilder();
28+
$container
29+
->register('session')
30+
->setArguments($arguments);
31+
$container
32+
->register('session.flash_bag')
33+
->setFactory([new Reference('session'), 'getFlashBag']);
34+
$container
35+
->register('session.attribute_bag')
36+
->setFactory([new Reference('session'), 'getAttributeBag']);
37+
38+
(new SessionPass())->process($container);
39+
40+
$this->assertSame($arguments, $container->getDefinition('session')->getArguments());
41+
$this->assertNull($container->getDefinition('session.flash_bag')->getFactory());
42+
$this->assertNull($container->getDefinition('session.attribute_bag')->getFactory());
43+
}
44+
}

0 commit comments

Comments
 (0)