diff --git a/.github/workflows/tasks.yml b/.github/workflows/tasks.yml index c8c6987..8019b84 100644 --- a/.github/workflows/tasks.yml +++ b/.github/workflows/tasks.yml @@ -1,6 +1,8 @@ name: Tasks -on: push +on: + push: + pull_request: jobs: lint-php: diff --git a/Classes/EventListener/ConsoleCommandEventListener.php b/Classes/EventListener/ConsoleCommandEventListener.php index 9e783a3..61ff48c 100644 --- a/Classes/EventListener/ConsoleCommandEventListener.php +++ b/Classes/EventListener/ConsoleCommandEventListener.php @@ -4,22 +4,33 @@ namespace Kanti\ServerTiming\EventListener; +use Exception; use Kanti\ServerTiming\Dto\ScriptResult; -use Kanti\ServerTiming\SqlLogging\SqlLoggerCore11; use Kanti\ServerTiming\Utility\TimingUtility; use Symfony\Component\Console\Event\ConsoleCommandEvent; use Symfony\Component\Console\Event\ConsoleTerminateEvent; +use Kanti\ServerTiming\Dto\StopWatch; final class ConsoleCommandEventListener { + /** @var StopWatch[] */ + private array $stopWatches = []; + public function start(ConsoleCommandEvent $event): void { - TimingUtility::start('console.command', (string)$event->getCommand()?->getName()); + $this->stopWatches[] = TimingUtility::stopWatch('console.command', (string)$event->getCommand()?->getName()); } public function stop(ConsoleTerminateEvent $event): void { - TimingUtility::end('console.command'); - TimingUtility::getInstance()->shutdown(ScriptResult::fromCli($event->getExitCode())); + $stopWatch = array_pop($this->stopWatches); + if ($stopWatch === null) { + throw new Exception('No stopWatch found, did you start the command already?'); + } + + $stopWatch->stop(); + if (!$this->stopWatches) { + TimingUtility::getInstance()->shutdown(ScriptResult::fromCli($event->getExitCode())); + } } }