Skip to content

Commit b7d3e0d

Browse files
refactor: restore run() as deprecated alias for backward compatibility (#11)
- Reintroduce run() which was previously removed when adding watch() - Mark run() as deprecated but keep it fully functional
1 parent 91c84d9 commit b7d3e0d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/TimeTracker.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,57 @@ public function getActiveTimers(): array
267267
return $activeTimers;
268268
}
269269

270+
/**
271+
* @codeCoverageIgnore
272+
*
273+
* Executes a callback while tracking its execution time.
274+
*
275+
* @deprecated Use `watch` instead
276+
*
277+
* @param callable $callback The callback function to execute.
278+
* @param array $params Parameters to pass to the callback.
279+
* @param string $unit The unit for measuring execution time.
280+
* @return array{result: Result, time: float|int, unit: string, output: mixed} An array containing Result, the execution time, unit, and callback result.
281+
*/
282+
public static function run(callable $callback, array $params = [], string $unit = 's'): array
283+
{
284+
$timeTracker = new self();
285+
286+
$randomId = bin2hex(random_bytes(16));
287+
288+
$container = new Container();
289+
290+
$timeTracker->start($randomId);
291+
292+
try {
293+
294+
$output = $container->call($callback, $params);
295+
296+
} catch (\Throwable $e) {
297+
$timeTracker->stop($randomId);
298+
299+
throw new \RuntimeException(
300+
$timeTracker->calculate($randomId)->format('Error occurring during executing callback, end in %s%s')->get() .
301+
"\n{$e->getMessage()}",
302+
$e->getCode(),
303+
$e
304+
);
305+
} finally {
306+
if (!$timeTracker->isStopped($randomId)) {
307+
$timeTracker->stop($randomId);
308+
}
309+
}
310+
311+
$result = $timeTracker->calculate($randomId);
312+
313+
return [
314+
'result' => $result,
315+
'time' => $result->convert($unit)->get(),
316+
'unit' => $unit,
317+
'output' => $output ?? null
318+
];
319+
}
320+
270321
/**
271322
* Executes a callback while tracking its execution time.
272323
*

0 commit comments

Comments
 (0)