Skip to content

[Stopwatch] Add ROOT constant to make it easier to reference #54854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Stopwatch] Add getRootSectionEvents method ROOT constant to `Sto…
…pwatch`
  • Loading branch information
hacfi committed May 13, 2024
commit fadeafde9a46f68ee07633403bc3af32c69a8f2b
5 changes: 5 additions & 0 deletions src/Symfony/Component/Stopwatch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
---

* Add `getRootSectionEvents()` method and `ROOT` constant to `Stopwatch`

5.2
---

Expand Down
16 changes: 14 additions & 2 deletions src/Symfony/Component/Stopwatch/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class_exists(Section::class);
*/
class Stopwatch implements ResetInterface
{
public const ROOT = '__root__';

/**
* @var Section[]
*/
Expand Down Expand Up @@ -138,14 +140,24 @@ public function getEvent(string $name): StopwatchEvent
*/
public function getSectionEvents(string $id): array
{
return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
return $this->sections[$id]->getEvents() ?? [];
}

/**
* Gets all events for the root section.
*
* @return StopwatchEvent[]
*/
public function getRootSectionEvents(): array
{
return $this->sections[self::ROOT]->getEvents() ?? [];
}

/**
* Resets the stopwatch to its original state.
*/
public function reset(): void
{
$this->sections = $this->activeSections = ['__root__' => new Section(null, $this->morePrecision)];
$this->sections = $this->activeSections = [self::ROOT => new Section(null, $this->morePrecision)];
}
}
Loading