ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CallbackDuration.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
27{
31 public function stretch(callable $callback)
32 {
33 $start_time = microtime(true);
34 $halted = true;
35
36 register_shutdown_function(static function () use (&$halted): void {
37 if ($halted) {
38 throw new \LogicException("Callback could not be stretched because it halted the programm.");
39 }
40 });
41
42 $return = $callback();
43 $halted = false;
44
45 $elapsed_time_in_us = ((microtime(true) - $start_time) * self::S_TO_US);
46 $duration_in_us = ($this->duration_in_ms * self::MS_TO_US);
47
48 if ($elapsed_time_in_us > $duration_in_us) {
49 throw new \RuntimeException("Execution of callback exceeded the given duration and could not be stretched.");
50 }
51
52 if ($elapsed_time_in_us < $duration_in_us) {
53 usleep((int) round($duration_in_us - $elapsed_time_in_us));
54 }
55
56 return $return;
57 }
58}
const MS_TO_US
number used for converting miliseconds (ms) into microseconds (us/ยต) or vise-versa.
Definition: Duration.php:36
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...