Skip to content

Commit c042b5b

Browse files
committed
bug symfony#32137 [HttpFoundation] fix accessing session bags (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] fix accessing session bags | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#30682 | License | MIT | Doc PR | Commits ------- 7a4570d fix accessing session bags
2 parents c511e46 + 7a4570d commit c042b5b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Component/HttpFoundation/Session/Session.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,9 @@ public function registerBag(SessionBagInterface $bag)
253253
*/
254254
public function getBag($name)
255255
{
256-
return $this->storage->getBag($name)->getBag();
256+
$bag = $this->storage->getBag($name);
257+
258+
return method_exists($bag, 'getBag') ? $bag->getBag() : $bag;
257259
}
258260

259261
/**

src/Symfony/Component/HttpFoundation/Tests/Session/SessionTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
1616
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
1717
use Symfony\Component\HttpFoundation\Session\Session;
18+
use Symfony\Component\HttpFoundation\Session\SessionBagProxy;
1819
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1920

2021
/**
@@ -260,4 +261,28 @@ public function testIsEmpty()
260261
$flash->get('hello');
261262
$this->assertTrue($this->session->isEmpty());
262263
}
264+
265+
public function testGetBagWithBagImplementingGetBag()
266+
{
267+
$bag = new AttributeBag();
268+
$bag->setName('foo');
269+
270+
$storage = new MockArraySessionStorage();
271+
$storage->registerBag($bag);
272+
273+
$this->assertSame($bag, (new Session($storage))->getBag('foo'));
274+
}
275+
276+
public function testGetBagWithBagNotImplementingGetBag()
277+
{
278+
$data = [];
279+
280+
$bag = new AttributeBag();
281+
$bag->setName('foo');
282+
283+
$storage = new MockArraySessionStorage();
284+
$storage->registerBag(new SessionBagProxy($bag, $data, $usageIndex));
285+
286+
$this->assertSame($bag, (new Session($storage))->getBag('foo'));
287+
}
263288
}

0 commit comments

Comments
 (0)