Skip to content

Commit c6184ed

Browse files
Scope validation and driver retrieval
1 parent 7e4ce36 commit c6184ed

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/EventLoop.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ public static function execute(callable $callback, EventLoopDriver $driver)
3232
}
3333
}
3434

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+
3547
/**
3648
* Stop the event loop.
3749
*
3850
* @return void
3951
*/
4052
public static function stop()
4153
{
54+
self::inScope();
55+
4256
self::$driver->stop();
4357
}
4458

@@ -51,6 +65,8 @@ public static function stop()
5165
*/
5266
public static function defer(callable $callback)
5367
{
68+
self::inScope();
69+
5470
return self::$driver->defer($callback);
5571
}
5672

@@ -64,6 +80,8 @@ public static function defer(callable $callback)
6480
*/
6581
public static function delay(callable $callback, float $time)
6682
{
83+
self::inScope();
84+
6785
return self::$driver->delay($callback, $time);
6886
}
6987

@@ -77,6 +95,8 @@ public static function delay(callable $callback, float $time)
7795
*/
7896
public static function repeat(callable $callback, float $interval)
7997
{
98+
self::inScope();
99+
80100
return self::$driver->repeat($callback, $interval);
81101
}
82102

@@ -90,6 +110,8 @@ public static function repeat(callable $callback, float $interval)
90110
*/
91111
public static function onReadable($stream, callable $callback)
92112
{
113+
self::inScope();
114+
93115
return self::$driver->onReadable($stream, $callback);
94116
}
95117

@@ -103,6 +125,8 @@ public static function onReadable($stream, callable $callback)
103125
*/
104126
public function onWritable($stream, callable $callback)
105127
{
128+
self::inScope();
129+
106130
return self::$driver->onWritable($stream, $callback);
107131
}
108132

@@ -116,6 +140,8 @@ public function onWritable($stream, callable $callback)
116140
*/
117141
public function onSignal(int $signo, callable $callback)
118142
{
143+
self::inScope();
144+
119145
return self::$driver->onSignal($signo, $callback);
120146
}
121147

@@ -128,6 +154,8 @@ public function onSignal(int $signo, callable $callback)
128154
*/
129155
public function onError(callable $callback)
130156
{
157+
self::inScope();
158+
131159
return self::$driver->onError($callback);
132160
}
133161

@@ -140,6 +168,8 @@ public function onError(callable $callback)
140168
*/
141169
public function enable(string $eventIdentifier)
142170
{
171+
self::inScope();
172+
143173
self::$driver->enable($eventIdentifier);
144174
}
145175

@@ -152,6 +182,8 @@ public function enable(string $eventIdentifier)
152182
*/
153183
public function disable(string $eventIdentifier)
154184
{
185+
self::inScope();
186+
155187
self::$driver->disable($eventIdentifier);
156188
}
157189

@@ -164,6 +196,8 @@ public function disable(string $eventIdentifier)
164196
*/
165197
public function cancel(string $eventIdentifier)
166198
{
199+
self::inScope();
200+
167201
self::$driver->cancel($eventIdentifier);
168202
}
169203

@@ -178,6 +212,8 @@ public function cancel(string $eventIdentifier)
178212
*/
179213
public function reference(string $eventIdentifier)
180214
{
215+
self::inScope();
216+
181217
self::$driver->reference($eventIdentifier);
182218
}
183219

@@ -193,9 +229,23 @@ public function reference(string $eventIdentifier)
193229
*/
194230
public function unreference(string $eventIdentifier)
195231
{
232+
self::inScope();
233+
196234
self::$driver->unreference($eventIdentifier);
197235
}
198236

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+
199249
/**
200250
* Disable construction as this is a static class.
201251
*/

0 commit comments

Comments
 (0)