1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Cubiche package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Core\Async\Loop; |
13
|
|
|
|
14
|
|
|
use Cubiche\Core\Async\Loop\Timer\Timer; |
15
|
|
|
use Cubiche\Core\Async\Promise\CallablePromisor; |
16
|
|
|
use Cubiche\Core\Async\Promise\PromiseInterface; |
17
|
|
|
use React\EventLoop\Factory; |
18
|
|
|
use React\EventLoop\LoopInterface as BaseLoopInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Loop Class. |
22
|
|
|
* |
23
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Loop implements LoopInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var BaseLoopInterface |
29
|
|
|
*/ |
30
|
|
|
protected $loop = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param BaseLoopInterface $loop |
34
|
|
|
*/ |
35
|
|
|
public function __construct(BaseLoopInterface $loop = null) |
36
|
|
|
{ |
37
|
|
|
$this->loop = $loop; |
38
|
|
|
if ($this->loop === null) { |
39
|
|
|
$this->loop = Factory::create(); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
|
|
public function addReadStream($stream, callable $listener) |
47
|
|
|
{ |
48
|
|
|
$this->loop()->addReadStream($stream, $listener); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function addWriteStream($stream, callable $listener) |
55
|
|
|
{ |
56
|
|
|
$this->loop()->addWriteStream($stream, $listener); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function removeReadStream($stream) |
63
|
|
|
{ |
64
|
|
|
$this->loop()->removeReadStream($stream); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function removeWriteStream($stream) |
71
|
|
|
{ |
72
|
|
|
$this->loop()->removeWriteStream($stream); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function removeStream($stream) |
79
|
|
|
{ |
80
|
|
|
$this->loop()->removeStream($stream); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
|
|
public function timeout(callable $task, $delay) |
87
|
|
|
{ |
88
|
|
|
return new Timer($this->loop(), $task, $delay); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
|
|
public function timer(callable $task, $interval, $count = null) |
95
|
|
|
{ |
96
|
|
|
return new Timer($this->loop(), $task, $interval, true, $count); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param callable $task |
101
|
|
|
* |
102
|
|
|
* @return PromiseInterface |
103
|
|
|
*/ |
104
|
|
|
public function next(callable $task) |
105
|
|
|
{ |
106
|
|
|
$promisor = new CallablePromisor($task); |
107
|
|
|
$this->loop()->nextTick($promisor); |
108
|
|
|
|
109
|
|
|
return $promisor->promise(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
*/ |
115
|
|
|
public function enqueue(callable $task) |
116
|
|
|
{ |
117
|
|
|
$promisor = new CallablePromisor($task); |
118
|
|
|
$this->loop()->futureTick($promisor); |
119
|
|
|
|
120
|
|
|
return $promisor->promise(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function tick() |
127
|
|
|
{ |
128
|
|
|
$this->loop()->tick(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function run() |
135
|
|
|
{ |
136
|
|
|
$this->loop()->run(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
|
|
public function stop() |
143
|
|
|
{ |
144
|
|
|
$this->loop()->stop(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return \React\EventLoop\LoopInterface |
149
|
|
|
*/ |
150
|
|
|
protected function loop() |
151
|
|
|
{ |
152
|
|
|
return $this->loop; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|