File tree Expand file tree Collapse file tree 6 files changed +154
-1
lines changed Expand file tree Collapse file tree 6 files changed +154
-1
lines changed Original file line number Diff line number Diff line change 6
6
"require" : {
7
7
"php" : " >=5.5.0"
8
8
},
9
+ "require-dev" : {
10
+ "phpunit/phpunit" : " ^4|^5"
11
+ },
9
12
"autoload" : {
10
13
"psr-4" : {
11
14
"Interop\\ Async\\ " : " src"
12
15
}
16
+ },
17
+ "autoload-dev" : {
18
+ "psr-4" : {
19
+ "Interop\\ Async\\ " : " test"
20
+ }
13
21
}
14
22
}
Original file line number Diff line number Diff line change
1
+ <phpunit bootstrap =" ./vendor/autoload.php" colors =" true" >
2
+ <testsuites >
3
+ <testsuite name =" Tests" >
4
+ <directory >./test</directory >
5
+ </testsuite >
6
+ </testsuites >
7
+ <filter >
8
+ <whitelist addUncoveredFilesFromWhitelist =" true" >
9
+ <directory >./src</directory >
10
+ </whitelist >
11
+ </filter >
12
+ </phpunit >
Original file line number Diff line number Diff line change 4
4
5
5
final class Loop
6
6
{
7
+ use Registry;
8
+
7
9
/**
8
10
* @var LoopDriver
9
11
*/
@@ -20,15 +22,18 @@ final class Loop
20
22
public static function execute (callable $ callback , LoopDriver $ driver )
21
23
{
22
24
$ previousDriver = self ::$ driver ;
25
+ $ previousRegistry = self ::$ registry ;
23
26
24
27
self ::$ driver = $ driver ;
28
+ self ::$ registry = [];
25
29
26
30
try {
27
31
$ callback ();
28
32
29
33
self ::$ driver ->run ();
30
34
} finally {
31
35
self ::$ driver = $ previousDriver ;
36
+ self ::$ registry = $ previousRegistry ;
32
37
}
33
38
}
34
39
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Interop \Async ;
4
+
5
+ trait Registry
6
+ {
7
+ /**
8
+ * @var array
9
+ */
10
+ private static $ registry = null ;
11
+
12
+ /**
13
+ * Stores information in the loop bound registry. This can be used to store
14
+ * loop bound information. Stored information is package private.
15
+ * Packages MUST NOT retrieve the stored state of other packages.
16
+ *
17
+ * Therefore packages SHOULD use the following prefix to keys:
18
+ * `vendor.package.`
19
+ *
20
+ * @param string $key namespaced storage key
21
+ * @param mixed $value the value to be stored
22
+ *
23
+ * @return void
24
+ */
25
+ public static function storeState ($ key , $ value )
26
+ {
27
+ if (self ::$ registry === null ) {
28
+ throw new \RuntimeException ('Not within the scope of an event loop driver ' );
29
+ }
30
+
31
+ if ($ value === null ) {
32
+ unset(self ::$ registry [$ key ]);
33
+ } else {
34
+ self ::$ registry [$ key ] = $ value ;
35
+ }
36
+ }
37
+
38
+ /**
39
+ * Fetches information stored bound to the loop. Stored information is
40
+ * package private. Packages MUST NOT retrieve the stored state of
41
+ * other packages.
42
+ *
43
+ * Therefore packages SHOULD use the following prefix to keys:
44
+ * `vendor.package.`
45
+ *
46
+ * @param string $key namespaced storage key
47
+ *
48
+ * @return mixed previously stored value or null if it doesn't exist
49
+ */
50
+ public static function fetchState ($ key )
51
+ {
52
+ if (self ::$ registry === null ) {
53
+ throw new \RuntimeException ('Not within the scope of an event loop driver ' );
54
+ }
55
+
56
+ return isset (self ::$ registry [$ key ]) ? self ::$ registry [$ key ] : null ;
57
+ }
58
+ }
Original file line number Diff line number Diff line change 6
6
* Must be thrown if an optional feature is not supported by the current driver
7
7
* or system.
8
8
*/
9
- class UnsupportedFeatureException extends \RuntimeException { }
9
+ class UnsupportedFeatureException extends \RuntimeException
10
+ {
11
+
12
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Interop \Async ;
4
+
5
+ class RegistryTest extends \PHPUnit_Framework_TestCase
6
+ {
7
+ use Registry;
8
+
9
+ protected function setUp ()
10
+ {
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 " );
30
+ }
31
+
32
+ /** @test */
33
+ public function defaultsToNull ()
34
+ {
35
+ // emulate we're in an event loop…
36
+ self ::$ registry = [];
37
+ $ this ->assertNull (self ::fetchState ("foobar " ));
38
+ }
39
+
40
+ /**
41
+ * @test
42
+ * @dataProvider provideValues
43
+ */
44
+ public function fetchesStoredValue ($ value )
45
+ {
46
+ // emulate we're in an event loop…
47
+ self ::$ registry = [];
48
+
49
+ $ this ->assertNull (self ::fetchState ("foobar " ));
50
+ self ::storeState ("foobar " , $ value );
51
+
52
+ $ this ->assertSame ($ value , self ::fetchState ("foobar " ));
53
+ }
54
+
55
+ public function provideValues ()
56
+ {
57
+ return [
58
+ ["string " ],
59
+ [42 ],
60
+ [1.001 ],
61
+ [true ],
62
+ [false ],
63
+ [null ],
64
+ [new \StdClass ],
65
+ ];
66
+ }
67
+ }
You can’t perform that action at this time.
0 commit comments