Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Support;
4
 
5
class Timebox
6
{
7
    /**
8
     * Indicates if the timebox is allowed to return early.
9
     *
10
     * @var bool
11
     */
12
    public $earlyReturn = false;
13
 
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
 
27
        $result = $callback($this);
28
 
29
        $remainder = intval($microseconds - ((microtime(true) - $start) * 1000000));
30
 
31
        if (! $this->earlyReturn && $remainder > 0) {
32
            $this->usleep($remainder);
33
        }
34
 
35
        return $result;
36
    }
37
 
38
    /**
39
     * Indicate that the timebox can return early.
40
     *
41
     * @return $this
42
     */
43
    public function returnEarly()
44
    {
45
        $this->earlyReturn = true;
46
 
47
        return $this;
48
    }
49
 
50
    /**
51
     * Indicate that the timebox cannot return early.
52
     *
53
     * @return $this
54
     */
55
    public function dontReturnEarly()
56
    {
57
        $this->earlyReturn = false;
58
 
59
        return $this;
60
    }
61
 
62
    /**
63
     * Sleep for the specified number of microseconds.
64
     *
65
     * @param  int  $microseconds
66
     * @return void
67
     */
68
    protected function usleep(int $microseconds)
69
    {
70
        usleep($microseconds);
71
    }
72
}