ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
CallbackDuration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 
22 use Closure;
23 
28 {
32  public function stretch(callable $callback)
33  {
34  $start_time = microtime(true);
35  $halted = true;
36 
37  register_shutdown_function(static function () use (&$halted) {
38  if ($halted) {
39  throw new \LogicException("Callback could not be stretched because it halted the programm.");
40  }
41  });
42 
43  $return = $callback();
44  $halted = false;
45 
46  $elapsed_time_in_us = ((microtime(true) - $start_time) * self::S_TO_US);
47  $duration_in_us = ($this->duration_in_ms * self::MS_TO_US);
48 
49  if ($elapsed_time_in_us > $duration_in_us) {
50  throw new \RuntimeException("Execution of callback exceeded the given duration and could not be stretched.");
51  }
52 
53  if ($elapsed_time_in_us < $duration_in_us) {
54  usleep((int) round($duration_in_us - $elapsed_time_in_us));
55  }
56 
57  return $return;
58  }
59 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...