File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ final class Loop
43
43
public static function setFactory (DriverFactory $ factory = null )
44
44
{
45
45
if (self ::$ level > 0 ) {
46
- throw new \RuntimeException ("Setting a new factory while running isn't allowed! " );
46
+ throw new \RuntimeException ("Setting a new factory while running isn't allowed. " );
47
47
}
48
48
49
49
self ::$ factory = $ factory ;
@@ -82,6 +82,31 @@ public static function execute(callable $callback, Driver $driver = null)
82
82
}
83
83
}
84
84
85
+ /**
86
+ * Run an unscoped loop.
87
+ *
88
+ * When possible, execute() SHOULD be preferred over run() for more explicit scoping.
89
+ *
90
+ * @return void
91
+ *
92
+ * @see \AsyncInterop\Loop::run()
93
+ */
94
+ public static function run ()
95
+ {
96
+ if (self ::$ level > 0 ) {
97
+ throw new \RuntimeException ("The loop can only be run while not yet running. " );
98
+ }
99
+
100
+ $ driver = self ::$ driver ?: self ::get ();
101
+ self ::$ level ++;
102
+
103
+ try {
104
+ $ driver ->run ();
105
+ } finally {
106
+ self ::$ level --;
107
+ }
108
+ }
109
+
85
110
/**
86
111
* Create a new driver if a factory is present, otherwise throw.
87
112
*
@@ -430,3 +455,10 @@ private function __construct()
430
455
// intentionally left blank
431
456
}
432
457
}
458
+
459
+ // Reset the $level in order to be able to use ::run() inside a shutdown handler
460
+ $ f = function ()
461
+ {
462
+ self ::$ level = 0 ;
463
+ };
464
+ register_shutdown_function ($ f ->bindTo (null , Loop::class));
You can’t perform that action at this time.
0 commit comments