2
2
3
3
namespace Interop \Async \Loop ;
4
4
5
- interface Driver
5
+ abstract class Driver
6
6
{
7
+ /**
8
+ * @var array
9
+ */
10
+ private $ registry = [];
11
+
7
12
/**
8
13
* Start the event loop.
9
14
*
@@ -13,7 +18,7 @@ interface Driver
13
18
*
14
19
* @return void
15
20
*/
16
- public function run ();
21
+ public abstract function run ();
17
22
18
23
/**
19
24
* Stop the event loop.
@@ -23,7 +28,7 @@ public function run();
23
28
*
24
29
* @return void
25
30
*/
26
- public function stop ();
31
+ public abstract function stop ();
27
32
28
33
/**
29
34
* Defer the execution of a callback.
@@ -37,7 +42,7 @@ public function stop();
37
42
*
38
43
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
39
44
*/
40
- public function defer (callable $ callback , $ data = null );
45
+ public abstract function defer (callable $ callback , $ data = null );
41
46
42
47
/**
43
48
* Delay the execution of a callback.
@@ -52,7 +57,7 @@ public function defer(callable $callback, $data = null);
52
57
*
53
58
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
54
59
*/
55
- public function delay ($ delay , callable $ callback , $ data = null );
60
+ public abstract function delay ($ delay , callable $ callback , $ data = null );
56
61
57
62
/**
58
63
* Repeatedly execute a callback.
@@ -67,7 +72,7 @@ public function delay($delay, callable $callback, $data = null);
67
72
*
68
73
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
69
74
*/
70
- public function repeat ($ interval , callable $ callback , $ data = null );
75
+ public abstract function repeat ($ interval , callable $ callback , $ data = null );
71
76
72
77
/**
73
78
* Execute a callback when a stream resource becomes readable.
@@ -80,7 +85,7 @@ public function repeat($interval, callable $callback, $data = null);
80
85
*
81
86
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
82
87
*/
83
- public function onReadable ($ stream , callable $ callback , $ data = null );
88
+ public abstract function onReadable ($ stream , callable $ callback , $ data = null );
84
89
85
90
/**
86
91
* Execute a callback when a stream resource becomes writable.
@@ -93,7 +98,7 @@ public function onReadable($stream, callable $callback, $data = null);
93
98
*
94
99
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
95
100
*/
96
- public function onWritable ($ stream , callable $ callback , $ data = null );
101
+ public abstract function onWritable ($ stream , callable $ callback , $ data = null );
97
102
98
103
/**
99
104
* Execute a callback when a signal is received.
@@ -112,7 +117,7 @@ public function onWritable($stream, callable $callback, $data = null);
112
117
*
113
118
* @throws UnsupportedFeatureException If signal handling is not supported.
114
119
*/
115
- public function onSignal ($ signo , callable $ callback , $ data = null );
120
+ public abstract function onSignal ($ signo , callable $ callback , $ data = null );
116
121
117
122
/**
118
123
* Enable a watcher.
@@ -126,7 +131,7 @@ public function onSignal($signo, callable $callback, $data = null);
126
131
*
127
132
* @throws InvalidWatcherException If the watcher identifier is invalid.
128
133
*/
129
- public function enable ($ watcherId );
134
+ public abstract function enable ($ watcherId );
130
135
131
136
/**
132
137
* Disable a watcher. Disabling a watcher MUST NOT invalidate the watcher.
@@ -137,7 +142,7 @@ public function enable($watcherId);
137
142
*
138
143
* @throws InvalidWatcherException If the watcher identifier is invalid.
139
144
*/
140
- public function disable ($ watcherId );
145
+ public abstract function disable ($ watcherId );
141
146
142
147
/**
143
148
* 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);
148
153
*
149
154
* @return void
150
155
*/
151
- public function cancel ($ watcherId );
156
+ public abstract function cancel ($ watcherId );
152
157
153
158
/**
154
159
* Reference a watcher.
@@ -162,7 +167,7 @@ public function cancel($watcherId);
162
167
*
163
168
* @throws InvalidWatcherException If the watcher identifier is invalid.
164
169
*/
165
- public function reference ($ watcherId );
170
+ public abstract function reference ($ watcherId );
166
171
167
172
/**
168
173
* Unreference a watcher.
@@ -176,7 +181,7 @@ public function reference($watcherId);
176
181
*
177
182
* @throws InvalidWatcherException If the watcher identifier is invalid.
178
183
*/
179
- public function unreference ($ watcherId );
184
+ public abstract function unreference ($ watcherId );
180
185
181
186
/**
182
187
* 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);
189
194
*
190
195
* @return void
191
196
*/
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
+ }
193
205
194
206
/**
195
207
* Fetches information stored bound to the loop. Stored information is package private. Packages MUST NOT retrieve
196
208
* the stored state of other packages.
197
209
*
198
210
* Therefore packages SHOULD use the following prefix to keys: `vendor.package.`
199
211
*
200
- * @param string $key Namespaced storage key.
212
+ * @param string $key namespaced storage key
201
213
*
202
214
* @return mixed previously stored value or null if it doesn't exist
203
215
*/
204
- public function fetchState ($ key );
216
+ public final function fetchState ($ key )
217
+ {
218
+ return isset ($ this ->registry [$ key ]) ? $ this ->registry [$ key ] : null ;
219
+ }
205
220
206
221
/**
207
222
* Set a callback to be executed when an error occurs.
@@ -213,7 +228,7 @@ public function fetchState($key);
213
228
*
214
229
* @return void
215
230
*/
216
- public function setErrorHandler (callable $ callback = null );
231
+ public abstract function setErrorHandler (callable $ callback = null );
217
232
218
233
/**
219
234
* Retrieve an associative array of information about the event loop driver.
@@ -235,7 +250,7 @@ public function setErrorHandler(callable $callback = null);
235
250
*
236
251
* @return array
237
252
*/
238
- public function info ();
253
+ public abstract function info ();
239
254
240
255
/**
241
256
* Get the underlying loop handle.
@@ -247,5 +262,5 @@ public function info();
247
262
*
248
263
* @return null|object|resource The loop handle the event loop operates on. Null if there is none.
249
264
*/
250
- public function getHandle ();
265
+ public abstract function getHandle ();
251
266
}
0 commit comments