Skip to content

Commit

Permalink
feat: nested profiler now check state of parent profile on profile
Browse files Browse the repository at this point in the history
  • Loading branch information
petrknap committed Oct 12, 2024
1 parent 6a97357 commit 35e8e15
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/Exception/ParentProfileIsNotStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Profiler\Exception;

use LogicException;

/**
* @internal exception should never be thrown out
*/
final class ParentProfileIsNotStarted extends LogicException implements ProfilerException
{
}
5 changes: 5 additions & 0 deletions src/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function __construct()
$this->outputOption = Optional::empty();
}

public function getState(): ProfileState
{
return $this->state;
}

/**
* @throws Exception\ProfileCouldNotBeStarted
*/
Expand Down
24 changes: 15 additions & 9 deletions src/Profiling.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

final class Profiling
{
private bool $wasFinished = false;

/**
* @param Profile<mixed> $profile
*/
Expand All @@ -29,14 +27,13 @@ public static function start(): self
*/
public function finish(): ProfileInterface
{
if ($this->wasFinished) {
throw new Exception\ProfilingHasBeenAlreadyFinished();
}
try {
$this->profile->finish();

$this->profile->finish();
$this->wasFinished = true;

return $this->profile;
return $this->profile;
} catch (Exception\ProfileException $profileException) {
throw new Exception\ProfilingHasBeenAlreadyFinished(previous: $profileException);
}
}

/**
Expand All @@ -53,6 +50,15 @@ public function __construct(
) {
$this->parentProfile = $parentProfile;
}

public function profile(callable $callable): ProcessableProfileInterface & ProfileWithOutputInterface
{
if ($this->parentProfile?->getState() !== ProfileState::Started) {
throw new Exception\ParentProfileIsNotStarted();
}

return parent::profile($callable);
}
};
}
}

0 comments on commit 35e8e15

Please sign in to comment.