Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Timebox.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Throwable;
6
7 class Timebox
8 {
9 /**
10 * Indicates if the timebox is allowed to return early.
11 *
12 * @var bool
13 */
14 public $earlyReturn = false;
15
16 /**
17 * Invoke the given callback within the specified timebox minimum.
18 *
19 * @template TCallReturnType
20 *
21 * @param (callable($this): TCallReturnType) $callback
22 * @param int $microseconds
23 * @return TCallReturnType
24 *
25 * @throws \Throwable
26 */
27 public function call(callable $callback, int $microseconds)
28 {
29 $exception = null;
30
31 $start = microtime(true);
32
33 try {
34 $result = $callback($this);
35 } catch (Throwable $caught) {
36 $exception = $caught;
37 }
38
39 $remainder = (int) ($microseconds - ((microtime(true) - $start) * 1_000_000));
40
41 if (! $this->earlyReturn && $remainder > 0) {
42 $this->usleep($remainder);
43 }
44
45 if ($exception) {
46 throw $exception;
47 }
48
49 return $result;
50 }
51
52 /**
53 * Indicate that the timebox can return early.
54 *
55 * @return $this
56 */
57 public function returnEarly()
58 {
59 $this->earlyReturn = true;
60
61 return $this;
62 }
63
64 /**
65 * Indicate that the timebox cannot return early.
66 *
67 * @return $this
68 */
69 public function dontReturnEarly()
70 {
71 $this->earlyReturn = false;
72
73 return $this;
74 }
75
76 /**
77 * Sleep for the specified number of microseconds.
78 *
79 * @param int $microseconds
80 * @return void
81 */
82 protected function usleep(int $microseconds)
83 {
84 Sleep::usleep($microseconds);
85 }
86 }