Exceptions
2 weeks ago
Facades
2 weeks ago
Testing
2 weeks ago
Traits
2 weeks ago
AggregateServiceProvider.php
2 years ago
Benchmark.php
2 weeks ago
Carbon.php
2 weeks ago
Composer.php
2 weeks ago
ConfigurationUrlParser.php
2 weeks ago
DateFactory.php
2 weeks ago
Env.php
2 weeks ago
Fluent.php
2 weeks ago
HigherOrderTapProxy.php
2 years ago
HtmlString.php
2 years ago
InteractsWithTime.php
2 years ago
Js.php
2 weeks ago
Lottery.php
2 weeks ago
Manager.php
2 years ago
MessageBag.php
2 weeks ago
MultipleInstanceManager.php
2 weeks ago
NamespacedItemResolver.php
2 weeks ago
Optional.php
2 weeks ago
Pluralizer.php
2 weeks ago
ProcessUtils.php
2 weeks ago
Reflector.php
2 weeks ago
ServiceProvider.php
2 years ago
Str.php
2 weeks ago
Stringable.php
2 weeks ago
Timebox.php
2 weeks ago
ValidatedInput.php
2 weeks ago
ViewErrorBag.php
2 weeks ago
helpers.php
2 weeks ago
Timebox.php
64 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Support; |
| 4 | |
| 5 | /** @internal */ |
| 6 | class Timebox |
| 7 | { |
| 8 | /** |
| 9 | * Indicates if the timebox is allowed to return early. |
| 10 | * |
| 11 | * @var bool |
| 12 | */ |
| 13 | public $earlyReturn = \false; |
| 14 | /** |
| 15 | * Invoke the given callback within the specified timebox minimum. |
| 16 | * |
| 17 | * @template TCallReturnType |
| 18 | * |
| 19 | * @param (callable($this): TCallReturnType) $callback |
| 20 | * @param int $microseconds |
| 21 | * @return TCallReturnType |
| 22 | */ |
| 23 | public function call(callable $callback, int $microseconds) |
| 24 | { |
| 25 | $start = \microtime(\true); |
| 26 | $result = $callback($this); |
| 27 | $remainder = \intval($microseconds - (\microtime(\true) - $start) * 1000000); |
| 28 | if (!$this->earlyReturn && $remainder > 0) { |
| 29 | $this->usleep($remainder); |
| 30 | } |
| 31 | return $result; |
| 32 | } |
| 33 | /** |
| 34 | * Indicate that the timebox can return early. |
| 35 | * |
| 36 | * @return $this |
| 37 | */ |
| 38 | public function returnEarly() |
| 39 | { |
| 40 | $this->earlyReturn = \true; |
| 41 | return $this; |
| 42 | } |
| 43 | /** |
| 44 | * Indicate that the timebox cannot return early. |
| 45 | * |
| 46 | * @return $this |
| 47 | */ |
| 48 | public function dontReturnEarly() |
| 49 | { |
| 50 | $this->earlyReturn = \false; |
| 51 | return $this; |
| 52 | } |
| 53 | /** |
| 54 | * Sleep for the specified number of microseconds. |
| 55 | * |
| 56 | * @param int $microseconds |
| 57 | * @return void |
| 58 | */ |
| 59 | protected function usleep(int $microseconds) |
| 60 | { |
| 61 | \usleep($microseconds); |
| 62 | } |
| 63 | } |
| 64 |