@@ -32,13 +32,27 @@ public static function execute(callable $callback, EventLoopDriver $driver)
32
32
}
33
33
}
34
34
35
+ /**
36
+ * Retrieve the event loop driver that is in scope.
37
+ *
38
+ * @return EventLoopDriver
39
+ */
40
+ public static function get ()
41
+ {
42
+ self ::inScope ();
43
+
44
+ return self ::$ driver ;
45
+ }
46
+
35
47
/**
36
48
* Stop the event loop.
37
49
*
38
50
* @return void
39
51
*/
40
52
public static function stop ()
41
53
{
54
+ self ::inScope ();
55
+
42
56
self ::$ driver ->stop ();
43
57
}
44
58
@@ -51,6 +65,8 @@ public static function stop()
51
65
*/
52
66
public static function defer (callable $ callback )
53
67
{
68
+ self ::inScope ();
69
+
54
70
return self ::$ driver ->defer ($ callback );
55
71
}
56
72
@@ -64,6 +80,8 @@ public static function defer(callable $callback)
64
80
*/
65
81
public static function delay (callable $ callback , float $ time )
66
82
{
83
+ self ::inScope ();
84
+
67
85
return self ::$ driver ->delay ($ callback , $ time );
68
86
}
69
87
@@ -77,6 +95,8 @@ public static function delay(callable $callback, float $time)
77
95
*/
78
96
public static function repeat (callable $ callback , float $ interval )
79
97
{
98
+ self ::inScope ();
99
+
80
100
return self ::$ driver ->repeat ($ callback , $ interval );
81
101
}
82
102
@@ -90,6 +110,8 @@ public static function repeat(callable $callback, float $interval)
90
110
*/
91
111
public static function onReadable ($ stream , callable $ callback )
92
112
{
113
+ self ::inScope ();
114
+
93
115
return self ::$ driver ->onReadable ($ stream , $ callback );
94
116
}
95
117
@@ -103,6 +125,8 @@ public static function onReadable($stream, callable $callback)
103
125
*/
104
126
public function onWritable ($ stream , callable $ callback )
105
127
{
128
+ self ::inScope ();
129
+
106
130
return self ::$ driver ->onWritable ($ stream , $ callback );
107
131
}
108
132
@@ -116,6 +140,8 @@ public function onWritable($stream, callable $callback)
116
140
*/
117
141
public function onSignal (int $ signo , callable $ callback )
118
142
{
143
+ self ::inScope ();
144
+
119
145
return self ::$ driver ->onSignal ($ signo , $ callback );
120
146
}
121
147
@@ -128,6 +154,8 @@ public function onSignal(int $signo, callable $callback)
128
154
*/
129
155
public function onError (callable $ callback )
130
156
{
157
+ self ::inScope ();
158
+
131
159
return self ::$ driver ->onError ($ callback );
132
160
}
133
161
@@ -140,6 +168,8 @@ public function onError(callable $callback)
140
168
*/
141
169
public function enable (string $ eventIdentifier )
142
170
{
171
+ self ::inScope ();
172
+
143
173
self ::$ driver ->enable ($ eventIdentifier );
144
174
}
145
175
@@ -152,6 +182,8 @@ public function enable(string $eventIdentifier)
152
182
*/
153
183
public function disable (string $ eventIdentifier )
154
184
{
185
+ self ::inScope ();
186
+
155
187
self ::$ driver ->disable ($ eventIdentifier );
156
188
}
157
189
@@ -164,6 +196,8 @@ public function disable(string $eventIdentifier)
164
196
*/
165
197
public function cancel (string $ eventIdentifier )
166
198
{
199
+ self ::inScope ();
200
+
167
201
self ::$ driver ->cancel ($ eventIdentifier );
168
202
}
169
203
@@ -178,6 +212,8 @@ public function cancel(string $eventIdentifier)
178
212
*/
179
213
public function reference (string $ eventIdentifier )
180
214
{
215
+ self ::inScope ();
216
+
181
217
self ::$ driver ->reference ($ eventIdentifier );
182
218
}
183
219
@@ -193,9 +229,23 @@ public function reference(string $eventIdentifier)
193
229
*/
194
230
public function unreference (string $ eventIdentifier )
195
231
{
232
+ self ::inScope ();
233
+
196
234
self ::$ driver ->unreference ($ eventIdentifier );
197
235
}
198
236
237
+ /**
238
+ * Validate that the event loop is currently within the scope of a driver.
239
+ *
240
+ * @return void
241
+ */
242
+ private static function inScope ()
243
+ {
244
+ if (null === self ::$ driver ) {
245
+ throw new \RuntimeException ('Not within the scope of an event loop driver ' );
246
+ }
247
+ }
248
+
199
249
/**
200
250
* Disable construction as this is a static class.
201
251
*/
0 commit comments