Skip to content

Commit bc433e8

Browse files
feature #36129 [HttpFoundation][HttpKernel][Security] Improve UnexpectedSessionUsageException backtrace (mtarld)
This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpFoundation][HttpKernel][Security] Improve UnexpectedSessionUsageException backtrace | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | | License | MIT | Doc PR | Improve `UnexceptedSessionUsageException` backtrace so that it leads to the place in the userland where it was told to use session. Commits ------- 1e1d332c7c Improve UnexcpectedSessionUsageException backtrace
2 parents 1767b98 + 9ae4633 commit bc433e8

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
according to [RFC 8674](https://tools.ietf.org/html/rfc8674)
1515
* made the Mime component an optional dependency
1616
* added `MarshallingSessionHandler`, `IdentityMarshaller`
17+
* made `Session` accept a callback to report when the session is being used
1718

1819
5.0.0
1920
-----

Session/Session.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class Session implements SessionInterface, \IteratorAggregate, \Countable
3535
private $attributeName;
3636
private $data = [];
3737
private $usageIndex = 0;
38+
private $usageReporter;
3839

39-
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
40+
public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null, callable $usageReporter = null)
4041
{
4142
$this->storage = $storage ?: new NativeSessionStorage();
43+
$this->usageReporter = $usageReporter;
4244

4345
$attributes = $attributes ?: new AttributeBag();
4446
$this->attributeName = $attributes->getName();
@@ -153,6 +155,9 @@ public function isEmpty(): bool
153155
{
154156
if ($this->isStarted()) {
155157
++$this->usageIndex;
158+
if ($this->usageReporter && 0 <= $this->usageIndex) {
159+
($this->usageReporter)();
160+
}
156161
}
157162
foreach ($this->data as &$data) {
158163
if (!empty($data)) {
@@ -229,6 +234,9 @@ public function setName(string $name)
229234
public function getMetadataBag()
230235
{
231236
++$this->usageIndex;
237+
if ($this->usageReporter && 0 <= $this->usageIndex) {
238+
($this->usageReporter)();
239+
}
232240

233241
return $this->storage->getMetadataBag();
234242
}
@@ -238,7 +246,7 @@ public function getMetadataBag()
238246
*/
239247
public function registerBag(SessionBagInterface $bag)
240248
{
241-
$this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex));
249+
$this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->usageIndex, $this->usageReporter));
242250
}
243251

244252
/**

Session/SessionBagProxy.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ final class SessionBagProxy implements SessionBagInterface
2121
private $bag;
2222
private $data;
2323
private $usageIndex;
24+
private $usageReporter;
2425

25-
public function __construct(SessionBagInterface $bag, array &$data, ?int &$usageIndex)
26+
public function __construct(SessionBagInterface $bag, array &$data, ?int &$usageIndex, ?callable $usageReporter)
2627
{
2728
$this->bag = $bag;
2829
$this->data = &$data;
2930
$this->usageIndex = &$usageIndex;
31+
$this->usageReporter = $usageReporter;
3032
}
3133

3234
public function getBag(): SessionBagInterface
3335
{
3436
++$this->usageIndex;
37+
if ($this->usageReporter && 0 <= $this->usageIndex) {
38+
($this->usageReporter)();
39+
}
3540

3641
return $this->bag;
3742
}
@@ -42,6 +47,9 @@ public function isEmpty(): bool
4247
return true;
4348
}
4449
++$this->usageIndex;
50+
if ($this->usageReporter && 0 <= $this->usageIndex) {
51+
($this->usageReporter)();
52+
}
4553

4654
return empty($this->data[$this->bag->getStorageKey()]);
4755
}
@@ -60,6 +68,10 @@ public function getName(): string
6068
public function initialize(array &$array): void
6169
{
6270
++$this->usageIndex;
71+
if ($this->usageReporter && 0 <= $this->usageIndex) {
72+
($this->usageReporter)();
73+
}
74+
6375
$this->data[$this->bag->getStorageKey()] = &$array;
6476

6577
$this->bag->initialize($array);

Tests/Session/SessionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ public function testGetBagWithBagNotImplementingGetBag()
281281
$bag->setName('foo');
282282

283283
$storage = new MockArraySessionStorage();
284-
$storage->registerBag(new SessionBagProxy($bag, $data, $usageIndex));
284+
$storage->registerBag(new SessionBagProxy($bag, $data, $usageIndex, null));
285285

286286
$this->assertSame($bag, (new Session($storage))->getBag('foo'));
287287
}

0 commit comments

Comments
 (0)