22
33namespace 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ abstract public 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+ final public 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+ final public 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+ abstract public 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+ abstract public 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+ abstract public function getHandle ();
251266}
0 commit comments