Skip to content

Commit 4908813

Browse files
committed
Refactor Registry for Drivers
1 parent 104b761 commit 4908813

File tree

3 files changed

+69
-35
lines changed

3 files changed

+69
-35
lines changed

src/Loop.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
final class Loop
99
{
10-
use Loop\Registry;
11-
1210
/**
1311
* @var DriverFactory
1412
*/
@@ -45,10 +43,8 @@ public static function setFactory(DriverFactory $factory = null)
4543

4644
if ($factory === null) {
4745
self::$driver = null;
48-
self::$registry = null;
4946
} else {
5047
self::$driver = self::createDriver();
51-
self::$registry = [];
5248
}
5349
}
5450

@@ -62,13 +58,11 @@ public static function setFactory(DriverFactory $factory = null)
6258
*/
6359
public static function execute(callable $callback, Driver $driver = null)
6460
{
65-
$previousRegistry = self::$registry;
6661
$previousDriver = self::$driver;
6762

6863
$driver = $driver ?: self::createDriver();
6964

7065
self::$driver = $driver;
71-
self::$registry = [];
7266
self::$level++;
7367

7468
try {
@@ -77,7 +71,6 @@ public static function execute(callable $callback, Driver $driver = null)
7771
self::$driver->run();
7872
} finally {
7973
self::$driver = $previousDriver;
80-
self::$registry = $previousRegistry;
8174
self::$level--;
8275
}
8376
}
@@ -281,6 +274,37 @@ public static function unreference($watcherId)
281274
self::get()->unreference($watcherId);
282275
}
283276

277+
/**
278+
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
279+
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
280+
*
281+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
282+
*
283+
* @param string $key namespaced storage key
284+
* @param mixed $value the value to be stored
285+
*
286+
* @return void
287+
*/
288+
public static function storeState($key, $value)
289+
{
290+
self::get()->storeState($key, $value);
291+
}
292+
293+
/**
294+
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
295+
* the stored state of other packages.
296+
*
297+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
298+
*
299+
* @param string $key namespaced storage key
300+
*
301+
* @return mixed previously stored value or null if it doesn't exist
302+
*/
303+
public static function fetchState($key)
304+
{
305+
return self::get()->fetchState($key);
306+
}
307+
284308
/**
285309
* Set a callback to be executed when an error occurs.
286310
*

src/Loop/Driver.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,31 @@ public function reference($watcherId);
149149
* @return void
150150
*/
151151
public function unreference($watcherId);
152+
153+
/**
154+
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
155+
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
156+
*
157+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
158+
*
159+
* @param string $key namespaced storage key
160+
* @param mixed $value the value to be stored
161+
*
162+
* @return void
163+
*/
164+
public function storeState($key, $value);
165+
166+
/**
167+
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
168+
* the stored state of other packages.
169+
*
170+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
171+
*
172+
* @param string $key namespaced storage key
173+
*
174+
* @return mixed previously stored value or null if it doesn't exist
175+
*/
176+
public function fetchState($key);
152177

153178
/**
154179
* Set a callback to be executed when an error occurs.

src/Loop/Registry.php

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,47 @@
33
namespace Interop\Async\Loop;
44

55
/**
6-
* State registry to be used in Interop\Async\Loop.
7-
*
8-
* THIS TRAIT SHOULD NOT BE USED BY LOOP DRIVERS. It's the responsibility of the
9-
* loop accessor to manage this state.
6+
* State registry to be used by classes implementing the Driver interface.
107
*/
118
trait Registry
129
{
1310
/**
1411
* @var array
1512
*/
16-
private static $registry = null;
13+
private $registry = [];
1714

1815
/**
19-
* Stores information in the loop bound registry. This can be used to store
20-
* loop bound information. Stored information is package private.
21-
* Packages MUST NOT retrieve the stored state of other packages.
16+
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
17+
* information is package private. Packages MUST NOT retrieve the stored state of other packages.
2218
*
23-
* Therefore packages SHOULD use the following prefix to keys:
24-
* `vendor.package.`
19+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
2520
*
2621
* @param string $key namespaced storage key
2722
* @param mixed $value the value to be stored
2823
*
2924
* @return void
3025
*/
31-
public static function storeState($key, $value)
26+
public function storeState($key, $value)
3227
{
33-
if (self::$registry === null) {
34-
throw new \RuntimeException('Not within the scope of an event loop driver');
35-
}
36-
3728
if ($value === null) {
38-
unset(self::$registry[$key]);
29+
unset($this->registry[$key]);
3930
} else {
40-
self::$registry[$key] = $value;
31+
$this->registry[$key] = $value;
4132
}
4233
}
4334

4435
/**
45-
* Fetches information stored bound to the loop. Stored information is
46-
* package private. Packages MUST NOT retrieve the stored state of
47-
* other packages.
36+
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
37+
* the stored state of other packages.
4838
*
49-
* Therefore packages SHOULD use the following prefix to keys:
50-
* `vendor.package.`
39+
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
5140
*
5241
* @param string $key namespaced storage key
5342
*
5443
* @return mixed previously stored value or null if it doesn't exist
5544
*/
56-
public static function fetchState($key)
45+
public function fetchState($key)
5746
{
58-
if (self::$registry === null) {
59-
throw new \RuntimeException('Not within the scope of an event loop driver');
60-
}
61-
62-
return isset(self::$registry[$key]) ? self::$registry[$key] : null;
47+
return isset($this->registry[$key]) ? $this->registry[$key] : null;
6348
}
6449
}

0 commit comments

Comments
 (0)