Skip to content

Commit 1c30c34

Browse files
committed
Merge pull request #68 from async-interop/driver-registry
Driver registry
2 parents 104b761 + 237a9c4 commit 1c30c34

File tree

4 files changed

+75
-64
lines changed

4 files changed

+75
-64
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
}

test/RegistryTest.php

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,17 @@
44

55
class RegistryTest extends \PHPUnit_Framework_TestCase
66
{
7-
use Registry;
7+
private $registry;
88

99
protected function setUp()
1010
{
11-
self::$registry = null;
12-
}
13-
14-
/**
15-
* @test
16-
* @expectedException \RuntimeException
17-
*/
18-
public function fetchfailsOutsideOfLoop()
19-
{
20-
self::fetchState("foobar");
21-
}
22-
23-
/**
24-
* @test
25-
* @expectedException \RuntimeException
26-
*/
27-
public function storefailsOutsideOfLoop()
28-
{
29-
self::fetchState("store");
11+
$this->registry = $this->getMockForTrait(Registry::class);
3012
}
3113

3214
/** @test */
3315
public function defaultsToNull()
3416
{
35-
// emulate we're in an event loop…
36-
self::$registry = [];
37-
$this->assertNull(self::fetchState("foobar"));
17+
$this->assertNull($this->registry->fetchState("foobar"));
3818
}
3919

4020
/**
@@ -43,13 +23,10 @@ public function defaultsToNull()
4323
*/
4424
public function fetchesStoredValue($value)
4525
{
46-
// emulate we're in an event loop…
47-
self::$registry = [];
48-
49-
$this->assertNull(self::fetchState("foobar"));
50-
self::storeState("foobar", $value);
26+
$this->assertNull($this->registry->fetchState("foobar"));
27+
$this->registry->storeState("foobar", $value);
5128

52-
$this->assertSame($value, self::fetchState("foobar"));
29+
$this->assertSame($value, $this->registry->fetchState("foobar"));
5330
}
5431

5532
public function provideValues()

0 commit comments

Comments
 (0)