From b7e4faababc85c0b5b0abe893728745659bf7a32 Mon Sep 17 00:00:00 2001 From: naingaunglwin-dev Date: Tue, 9 Dec 2025 15:49:55 +0630 Subject: [PATCH] refactor: restore run() as deprecated alias for backward compatibility --- src/TimeTracker.php | 51 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/TimeTracker.php b/src/TimeTracker.php index ab6c248..a5999ac 100644 --- a/src/TimeTracker.php +++ b/src/TimeTracker.php @@ -267,6 +267,57 @@ public function getActiveTimers(): array return $activeTimers; } + /** + * @codeCoverageIgnore + * + * Executes a callback while tracking its execution time. + * + * @deprecated Use `watch` instead + * + * @param callable $callback The callback function to execute. + * @param array $params Parameters to pass to the callback. + * @param string $unit The unit for measuring execution time. + * @return array{result: Result, time: float|int, unit: string, output: mixed} An array containing Result, the execution time, unit, and callback result. + */ + public static function run(callable $callback, array $params = [], string $unit = 's'): array + { + $timeTracker = new self(); + + $randomId = bin2hex(random_bytes(16)); + + $container = new Container(); + + $timeTracker->start($randomId); + + try { + + $output = $container->call($callback, $params); + + } catch (\Throwable $e) { + $timeTracker->stop($randomId); + + throw new \RuntimeException( + $timeTracker->calculate($randomId)->format('Error occurring during executing callback, end in %s%s')->get() . + "\n{$e->getMessage()}", + $e->getCode(), + $e + ); + } finally { + if (!$timeTracker->isStopped($randomId)) { + $timeTracker->stop($randomId); + } + } + + $result = $timeTracker->calculate($randomId); + + return [ + 'result' => $result, + 'time' => $result->convert($unit)->get(), + 'unit' => $unit, + 'output' => $output ?? null + ]; + } + /** * Executes a callback while tracking its execution time. *