Skip to content

Commit 6cde9f5

Browse files
committed
Merge Registry into Driver
1 parent 6b1da28 commit 6cde9f5

File tree

4 files changed

+39
-73
lines changed

4 files changed

+39
-73
lines changed

src/Loop/Driver.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
namespace Interop\Async\Loop;
44

5-
interface Driver
5+
abstract class Driver
66
{
7+
/**
8+
* @var array
9+
*/
10+
private $registry = [];
11+
712
/**
813
* Start the event loop.
914
*
@@ -13,7 +18,7 @@ interface Driver
1318
*
1419
* @return void
1520
*/
16-
public function run();
21+
public abstract function run();
1722

1823
/**
1924
* Stop the event loop.
@@ -23,7 +28,7 @@ public function run();
2328
*
2429
* @return void
2530
*/
26-
public function stop();
31+
public abstract function stop();
2732

2833
/**
2934
* Defer the execution of a callback.
@@ -37,7 +42,7 @@ public function stop();
3742
*
3843
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
3944
*/
40-
public function defer(callable $callback, $data = null);
45+
public abstract function defer(callable $callback, $data = null);
4146

4247
/**
4348
* Delay the execution of a callback.
@@ -52,7 +57,7 @@ public function defer(callable $callback, $data = null);
5257
*
5358
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
5459
*/
55-
public function delay($delay, callable $callback, $data = null);
60+
public abstract function delay($delay, callable $callback, $data = null);
5661

5762
/**
5863
* Repeatedly execute a callback.
@@ -67,7 +72,7 @@ public function delay($delay, callable $callback, $data = null);
6772
*
6873
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
6974
*/
70-
public function repeat($interval, callable $callback, $data = null);
75+
public abstract function repeat($interval, callable $callback, $data = null);
7176

7277
/**
7378
* Execute a callback when a stream resource becomes readable.
@@ -80,7 +85,7 @@ public function repeat($interval, callable $callback, $data = null);
8085
*
8186
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
8287
*/
83-
public function onReadable($stream, callable $callback, $data = null);
88+
public abstract function onReadable($stream, callable $callback, $data = null);
8489

8590
/**
8691
* Execute a callback when a stream resource becomes writable.
@@ -93,7 +98,7 @@ public function onReadable($stream, callable $callback, $data = null);
9398
*
9499
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
95100
*/
96-
public function onWritable($stream, callable $callback, $data = null);
101+
public abstract function onWritable($stream, callable $callback, $data = null);
97102

98103
/**
99104
* Execute a callback when a signal is received.
@@ -112,7 +117,7 @@ public function onWritable($stream, callable $callback, $data = null);
112117
*
113118
* @throws UnsupportedFeatureException If signal handling is not supported.
114119
*/
115-
public function onSignal($signo, callable $callback, $data = null);
120+
public abstract function onSignal($signo, callable $callback, $data = null);
116121

117122
/**
118123
* Enable a watcher.
@@ -126,7 +131,7 @@ public function onSignal($signo, callable $callback, $data = null);
126131
*
127132
* @throws InvalidWatcherException If the watcher identifier is invalid.
128133
*/
129-
public function enable($watcherId);
134+
public abstract function enable($watcherId);
130135

131136
/**
132137
* Disable a watcher. Disabling a watcher MUST NOT invalidate the watcher.
@@ -137,7 +142,7 @@ public function enable($watcherId);
137142
*
138143
* @throws InvalidWatcherException If the watcher identifier is invalid.
139144
*/
140-
public function disable($watcherId);
145+
public abstract function disable($watcherId);
141146

142147
/**
143148
* Cancel a watcher. This will detatch the event loop from all resources that are associated to the watcher. After
@@ -148,7 +153,7 @@ public function disable($watcherId);
148153
*
149154
* @return void
150155
*/
151-
public function cancel($watcherId);
156+
public abstract function cancel($watcherId);
152157

153158
/**
154159
* Reference a watcher.
@@ -162,7 +167,7 @@ public function cancel($watcherId);
162167
*
163168
* @throws InvalidWatcherException If the watcher identifier is invalid.
164169
*/
165-
public function reference($watcherId);
170+
public abstract function reference($watcherId);
166171

167172
/**
168173
* Unreference a watcher.
@@ -176,7 +181,7 @@ public function reference($watcherId);
176181
*
177182
* @throws InvalidWatcherException If the watcher identifier is invalid.
178183
*/
179-
public function unreference($watcherId);
184+
public abstract function unreference($watcherId);
180185

181186
/**
182187
* Stores information in the loop bound registry. This can be used to store loop bound information. Stored
@@ -189,19 +194,29 @@ public function unreference($watcherId);
189194
*
190195
* @return void
191196
*/
192-
public function storeState($key, $value);
197+
public final function storeState($key, $value)
198+
{
199+
if ($value === null) {
200+
unset($this->registry[$key]);
201+
} else {
202+
$this->registry[$key] = $value;
203+
}
204+
}
193205

194206
/**
195207
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
196208
* the stored state of other packages.
197209
*
198210
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
199211
*
200-
* @param string $key Namespaced storage key.
212+
* @param string $key namespaced storage key
201213
*
202214
* @return mixed previously stored value or null if it doesn't exist
203215
*/
204-
public function fetchState($key);
216+
public final function fetchState($key)
217+
{
218+
return isset($this->registry[$key]) ? $this->registry[$key] : null;
219+
}
205220

206221
/**
207222
* Set a callback to be executed when an error occurs.
@@ -213,7 +228,7 @@ public function fetchState($key);
213228
*
214229
* @return void
215230
*/
216-
public function setErrorHandler(callable $callback = null);
231+
public abstract function setErrorHandler(callable $callback = null);
217232

218233
/**
219234
* Retrieve an associative array of information about the event loop driver.
@@ -235,7 +250,7 @@ public function setErrorHandler(callable $callback = null);
235250
*
236251
* @return array
237252
*/
238-
public function info();
253+
public abstract function info();
239254

240255
/**
241256
* Get the underlying loop handle.
@@ -247,5 +262,5 @@ public function info();
247262
*
248263
* @return null|object|resource The loop handle the event loop operates on. Null if there is none.
249264
*/
250-
public function getHandle();
265+
public abstract function getHandle();
251266
}

src/Loop/Registry.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

test/DummyDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Interop\Async\Loop\Test;
44

5-
class DummyDriver implements \Interop\Async\Loop\Driver
6-
{
7-
use \Interop\Async\Loop\Registry;
5+
use Interop\Async\Loop\Driver;
86

7+
class DummyDriver extends Driver
8+
{
99
public $defers;
1010
public $handler;
1111
public static $id = "a";

test/RegistryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class RegistryTest extends \PHPUnit_Framework_TestCase
88

99
protected function setUp()
1010
{
11-
$this->registry = $this->getMockForTrait(Registry::class);
11+
$this->registry = $this->getMockForAbstractClass(Driver::class);
1212
}
1313

1414
/** @test */

0 commit comments

Comments
 (0)