Skip to content

Commit f890d37

Browse files
committed
Use default registry to support sync wait, fix error messages
1 parent 665cc7f commit f890d37

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/Loop.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ final class Loop
1616
*/
1717
private static $driver = null;
1818

19+
/**
20+
* @var bool
21+
*/
22+
private static $running = false;
23+
1924
/**
2025
* Set the factory to be used to create a driver if none is passed to
21-
* self::execute. A default driver will be created if none exists yet
26+
* self::execute. A default driver will be created if none is running
2227
* to support synchronous waits in traditional applications.
2328
*/
24-
public static function setFactory(LoopDriverFactory $factory = null) {
29+
public static function setFactory(LoopDriverFactory $factory = null)
30+
{
2531
self::$factory = $factory;
26-
self::$driver = self::$driver ?: self::createDriver();
32+
33+
if (!self::$running) {
34+
self::$driver = self::createDriver();
35+
self::$registry = [];
36+
}
2737
}
2838

2939
/**
@@ -36,12 +46,14 @@ public static function setFactory(LoopDriverFactory $factory = null) {
3646
*/
3747
public static function execute(callable $callback, LoopDriver $driver = null)
3848
{
39-
$driver = $driver ?: self::createDriver();
4049
$previousRegistry = self::$registry;
41-
4250
$previousDriver = self::$driver;
51+
52+
$driver = $driver ?: self::createDriver();
53+
4354
self::$driver = $driver;
4455
self::$registry = [];
56+
self::$running = true;
4557

4658
try {
4759
$callback();
@@ -50,6 +62,7 @@ public static function execute(callable $callback, LoopDriver $driver = null)
5062
} finally {
5163
self::$driver = $previousDriver;
5264
self::$registry = $previousRegistry;
65+
self::$running = false;
5366
}
5467
}
5568

@@ -67,7 +80,8 @@ private static function createDriver()
6780
$driver = self::$factory->create();
6881

6982
if (!$driver instanceof LoopDriver) {
70-
throw new \LogicException("LoopDriverFactory didn't return a LoopDriver.");
83+
$type = is_object($driver) ? "an instance of " . get_class($driver) : gettype($driver);
84+
throw new \LogicException("Factory returned {$type}, but must return an instance of LoopDriver.");
7185
}
7286

7387
return $driver;
@@ -81,7 +95,7 @@ private static function createDriver()
8195
public static function get()
8296
{
8397
if (null === self::$driver) {
84-
throw new \RuntimeException('Not within the scope of an event loop driver');
98+
throw new \RuntimeException('Missing driver; Neither in Loop::execute nor factory set.');
8599
}
86100

87101
return self::$driver;
@@ -268,12 +282,16 @@ public static function setErrorHandler(callable $callback = null)
268282
*
269283
* @return bool
270284
*/
271-
public static function supports($feature) {
285+
public static function supports($feature)
286+
{
272287
return self::get()->supports($feature);
273288
}
274289

275290
/**
276291
* Disable construction as this is a static class.
277292
*/
278-
private function __construct() {}
293+
private function __construct()
294+
{
295+
// intentionally left blank
296+
}
279297
}

0 commit comments

Comments
 (0)