@@ -40,29 +40,22 @@ public static function setFactory(DriverFactory $factory = null)
40
40
}
41
41
42
42
self ::$ factory = $ factory ;
43
-
44
- if ($ factory === null ) {
45
- self ::$ driver = null ;
46
- } else {
47
- self ::$ driver = self ::createDriver ();
48
- }
43
+ self ::$ driver = null ; // reset it here, it will be actually instantiated inside execute() or get()
49
44
}
50
45
51
46
/**
52
47
* Execute a callback within the scope of an event loop driver.
53
48
*
54
49
* @param callable $callback The callback to execute
55
- * @param Driver $driver The event loop driver
50
+ * @param Driver $driver The event loop driver. If null a new one is created from the set factory.
56
51
*
57
52
* @return void
58
53
*/
59
54
public static function execute (callable $ callback , Driver $ driver = null )
60
55
{
61
56
$ previousDriver = self ::$ driver ;
62
57
63
- $ driver = $ driver ?: self ::createDriver ();
64
-
65
- self ::$ driver = $ driver ;
58
+ self ::$ driver = $ driver ?: self ::createDriver ();
66
59
self ::$ level ++;
67
60
68
61
try {
@@ -103,11 +96,10 @@ private static function createDriver()
103
96
*/
104
97
public static function get ()
105
98
{
106
- if (null === self ::$ driver ) {
107
- throw new \ RuntimeException ( ' Missing driver; Neither in Loop::execute nor factory set. ' ) ;
99
+ if (self ::$ driver ) {
100
+ return self :: $ driver ;
108
101
}
109
-
110
- return self ::$ driver ;
102
+ return self ::$ driver = self ::createDriver ();
111
103
}
112
104
113
105
/**
@@ -117,7 +109,8 @@ public static function get()
117
109
*/
118
110
public static function stop ()
119
111
{
120
- self ::get ()->stop ();
112
+ $ driver = self ::$ driver ?: self ::get ();
113
+ $ driver ->stop ();
121
114
}
122
115
123
116
/**
@@ -130,7 +123,8 @@ public static function stop()
130
123
*/
131
124
public static function defer (callable $ callback , $ data = null )
132
125
{
133
- return self ::get ()->defer ($ callback , $ data );
126
+ $ driver = self ::$ driver ?: self ::get ();
127
+ return $ driver ->defer ($ callback , $ data );
134
128
}
135
129
136
130
/**
@@ -146,7 +140,8 @@ public static function defer(callable $callback, $data = null)
146
140
*/
147
141
public static function delay ($ time , callable $ callback , $ data = null )
148
142
{
149
- return self ::get ()->delay ($ time , $ callback , $ data );
143
+ $ driver = self ::$ driver ?: self ::get ();
144
+ return $ driver ->delay ($ time , $ callback , $ data );
150
145
}
151
146
152
147
/**
@@ -163,7 +158,8 @@ public static function delay($time, callable $callback, $data = null)
163
158
*/
164
159
public static function repeat ($ interval , callable $ callback , $ data = null )
165
160
{
166
- return self ::get ()->repeat ($ interval , $ callback , $ data );
161
+ $ driver = self ::$ driver ?: self ::get ();
162
+ return $ driver ->repeat ($ interval , $ callback , $ data );
167
163
}
168
164
169
165
/**
@@ -177,7 +173,8 @@ public static function repeat($interval, callable $callback, $data = null)
177
173
*/
178
174
public static function onReadable ($ stream , callable $ callback , $ data = null )
179
175
{
180
- return self ::get ()->onReadable ($ stream , $ callback , $ data );
176
+ $ driver = self ::$ driver ?: self ::get ();
177
+ return $ driver ->onReadable ($ stream , $ callback , $ data );
181
178
}
182
179
183
180
/**
@@ -191,7 +188,8 @@ public static function onReadable($stream, callable $callback, $data = null)
191
188
*/
192
189
public static function onWritable ($ stream , callable $ callback , $ data = null )
193
190
{
194
- return self ::get ()->onWritable ($ stream , $ callback , $ data );
191
+ $ driver = self ::$ driver ?: self ::get ();
192
+ return $ driver ->onWritable ($ stream , $ callback , $ data );
195
193
}
196
194
197
195
/**
@@ -205,7 +203,8 @@ public static function onWritable($stream, callable $callback, $data = null)
205
203
*/
206
204
public static function onSignal ($ signo , callable $ callback , $ data = null )
207
205
{
208
- return self ::get ()->onSignal ($ signo , $ callback , $ data );
206
+ $ driver = self ::$ driver ?: self ::get ();
207
+ return $ driver ->onSignal ($ signo , $ callback , $ data );
209
208
}
210
209
211
210
/**
@@ -217,7 +216,8 @@ public static function onSignal($signo, callable $callback, $data = null)
217
216
*/
218
217
public static function enable ($ watcherId )
219
218
{
220
- self ::get ()->enable ($ watcherId );
219
+ $ driver = self ::$ driver ?: self ::get ();
220
+ $ driver ->enable ($ watcherId );
221
221
}
222
222
223
223
/**
@@ -229,7 +229,8 @@ public static function enable($watcherId)
229
229
*/
230
230
public static function disable ($ watcherId )
231
231
{
232
- self ::get ()->disable ($ watcherId );
232
+ $ driver = self ::$ driver ?: self ::get ();
233
+ $ driver ->disable ($ watcherId );
233
234
}
234
235
235
236
/**
@@ -241,7 +242,8 @@ public static function disable($watcherId)
241
242
*/
242
243
public static function cancel ($ watcherId )
243
244
{
244
- self ::get ()->cancel ($ watcherId );
245
+ $ driver = self ::$ driver ?: self ::get ();
246
+ $ driver ->cancel ($ watcherId );
245
247
}
246
248
247
249
/**
@@ -256,7 +258,8 @@ public static function cancel($watcherId)
256
258
*/
257
259
public static function reference ($ watcherId )
258
260
{
259
- self ::get ()->reference ($ watcherId );
261
+ $ driver = self ::$ driver ?: self ::get ();
262
+ $ driver ->reference ($ watcherId );
260
263
}
261
264
262
265
/**
@@ -271,7 +274,8 @@ public static function reference($watcherId)
271
274
*/
272
275
public static function unreference ($ watcherId )
273
276
{
274
- self ::get ()->unreference ($ watcherId );
277
+ $ driver = self ::$ driver ?: self ::get ();
278
+ $ driver ->unreference ($ watcherId );
275
279
}
276
280
277
281
/**
@@ -287,7 +291,8 @@ public static function unreference($watcherId)
287
291
*/
288
292
public static function storeState ($ key , $ value )
289
293
{
290
- self ::get ()->storeState ($ key , $ value );
294
+ $ driver = self ::$ driver ?: self ::get ();
295
+ $ driver ->storeState ($ key , $ value );
291
296
}
292
297
293
298
/**
@@ -302,7 +307,8 @@ public static function storeState($key, $value)
302
307
*/
303
308
public static function fetchState ($ key )
304
309
{
305
- return self ::get ()->fetchState ($ key );
310
+ $ driver = self ::$ driver ?: self ::get ();
311
+ return $ driver ->fetchState ($ key );
306
312
}
307
313
308
314
/**
@@ -316,7 +322,8 @@ public static function fetchState($key)
316
322
*/
317
323
public static function setErrorHandler (callable $ callback = null )
318
324
{
319
- self ::get ()->setErrorHandler ($ callback );
325
+ $ driver = self ::$ driver ?: self ::get ();
326
+ $ driver ->setErrorHandler ($ callback );
320
327
}
321
328
322
329
/**
@@ -342,7 +349,8 @@ public static function setErrorHandler(callable $callback = null)
342
349
*/
343
350
public static function info ()
344
351
{
345
- return self ::get ()->info ();
352
+ $ driver = self ::$ driver ?: self ::get ();
353
+ return $ driver ->info ();
346
354
}
347
355
348
356
/**
0 commit comments