@@ -12,6 +12,9 @@ final class Loop
12
12
*/
13
13
private static $ instance ;
14
14
15
+ /** @var bool */
16
+ private static $ stopped = false ;
17
+
15
18
/**
16
19
* Returns the event loop.
17
20
* When no loop is set it will it will call the factory to create one.
@@ -32,22 +35,23 @@ public static function get()
32
35
33
36
self ::$ instance = $ loop = Factory::create ();
34
37
35
- // Automatically run loop at end of program, unless already started explicitly.
38
+ // Automatically run loop at end of program, unless already started or stopped explicitly.
36
39
// This is tested using child processes, so coverage is actually 100%, see BinTest.
37
40
// @codeCoverageIgnoreStart
38
41
$ hasRun = false ;
39
42
$ loop ->futureTick (function () use (&$ hasRun ) {
40
43
$ hasRun = true ;
41
44
});
42
45
43
- register_shutdown_function (function () use ($ loop , &$ hasRun ) {
46
+ $ stopped =& self ::$ stopped ;
47
+ register_shutdown_function (function () use ($ loop , &$ hasRun , &$ stopped ) {
44
48
// Don't run if we're coming from a fatal error (uncaught exception).
45
49
$ error = error_get_last ();
46
50
if ((isset ($ error ['type ' ]) ? $ error ['type ' ] : 0 ) & (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR )) {
47
51
return ;
48
52
}
49
53
50
- if (!$ hasRun ) {
54
+ if (!$ hasRun && ! $ stopped ) {
51
55
$ loop ->run ();
52
56
}
53
57
});
@@ -215,6 +219,7 @@ public static function run()
215
219
*/
216
220
public static function stop ()
217
221
{
222
+ self ::$ stopped = true ;
218
223
self ::get ()->stop ();
219
224
}
220
225
}
0 commit comments