Skip to content

Commit 8691af2

Browse files
committed
feature symfony#54854 [Stopwatch] Add ROOT constant to make it easier to reference (hacfi)
This PR was merged into the 7.2 branch. Discussion ---------- [Stopwatch] Add `ROOT` constant to make it easier to reference | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT Just an DX improvement so you can reference the root section with `$stopwatch->getSectionEvents(Stopwatch::ROOT)` instead of `$stopwatch->getSectionEvents('__root__')`. Commits ------- fadeafd [Stopwatch] Add `getRootSectionEvents` method `ROOT` constant to `Stopwatch`
2 parents 2ea870f + fadeafd commit 8691af2

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/Stopwatch/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add method `getLastPeriod()` to `StopwatchEvent`
8+
* Add `getRootSectionEvents()` method and `ROOT` constant to `Stopwatch`
89

910
5.2
1011
---

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class_exists(Section::class);
2323
*/
2424
class Stopwatch implements ResetInterface
2525
{
26+
public const ROOT = '__root__';
27+
2628
/**
2729
* @var Section[]
2830
*/
@@ -138,14 +140,24 @@ public function getEvent(string $name): StopwatchEvent
138140
*/
139141
public function getSectionEvents(string $id): array
140142
{
141-
return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
143+
return $this->sections[$id]->getEvents() ?? [];
144+
}
145+
146+
/**
147+
* Gets all events for the root section.
148+
*
149+
* @return StopwatchEvent[]
150+
*/
151+
public function getRootSectionEvents(): array
152+
{
153+
return $this->sections[self::ROOT]->getEvents() ?? [];
142154
}
143155

144156
/**
145157
* Resets the stopwatch to its original state.
146158
*/
147159
public function reset(): void
148160
{
149-
$this->sections = $this->activeSections = ['__root__' => new Section(null, $this->morePrecision)];
161+
$this->sections = $this->activeSections = [self::ROOT => new Section(null, $this->morePrecision)];
150162
}
151163
}

0 commit comments

Comments
 (0)