1
+ <?php
2
+
3
+ namespace Playground ;
4
+
5
+ use Amp \Reactor ;
6
+ use Interop \Async \EventLoop \EventLoopDriver ;
7
+
8
+ class AmpDriver implements EventLoopDriver {
9
+ private $ loop ;
10
+
11
+ public function __construct (Reactor $ loop ) {
12
+ $ this ->loop = $ loop ;
13
+ }
14
+
15
+ /**
16
+ * Start the event loop.
17
+ *
18
+ * @return void
19
+ */
20
+ public function run () {
21
+ $ this ->loop ->run ();
22
+ }
23
+
24
+ /**
25
+ * Stop the event loop.
26
+ *
27
+ * @return void
28
+ */
29
+ public function stop () {
30
+ $ this ->loop ->stop ();
31
+ }
32
+
33
+ /**
34
+ * Defer the execution of a callback.
35
+ *
36
+ * @param callable $callback The callback to defer.
37
+ *
38
+ * @return string An identifier that can be used to cancel, enable or disable the event.
39
+ */
40
+ public function defer (callable $ callback ) {
41
+ return $ this ->loop ->immediately ($ callback );
42
+ }
43
+
44
+
45
+ /**
46
+ * Delay the execution of a callback. The time delay is approximate and accuracy is not guaranteed.
47
+ *
48
+ * @param callable $callback The callback to delay.
49
+ * @param int $delay The amount of time, in milliseconds, to delay the execution for.
50
+ *
51
+ * @return string An identifier that can be used to cancel, enable or disable the event.
52
+ */
53
+ public function delay (callable $ callback , $ delay ) {
54
+ return $ this ->loop ->once ($ callback , $ delay );
55
+ }
56
+
57
+ /**
58
+ * Repeatedly execute a callback. The interval between executions is approximate and accuracy is not guaranteed.
59
+ *
60
+ * @param callable $callback The callback to repeat.
61
+ * @param int $interval The time interval, in milliseconds, to wait between executions.
62
+ *
63
+ * @return string An identifier that can be used to cancel, enable or disable the event.
64
+ */
65
+ public function repeat (callable $ callback , $ interval ) {
66
+ return $ this ->loop ->repeat ($ callback , $ interval );
67
+ }
68
+
69
+ /**
70
+ * Execute a callback when a stream resource becomes readable.
71
+ *
72
+ * @param resource $stream The stream to monitor.
73
+ * @param callable $callback The callback to execute.
74
+ *
75
+ * @return string An identifier that can be used to cancel, enable or disable the event.
76
+ */
77
+ public function onReadable ($ stream , callable $ callback ) {
78
+ return $ this ->loop ->onReadable ($ stream , $ callback );
79
+ }
80
+
81
+ /**
82
+ * Execute a callback when a stream resource becomes writable.
83
+ *
84
+ * @param resource $stream The stream to monitor.
85
+ * @param callable $callback The callback to execute.
86
+ *
87
+ * @return string An identifier that can be used to cancel, enable or disable the event.
88
+ */
89
+ public function onWritable ($ stream , callable $ callback ) {
90
+ return $ this ->loop ->onWritable ($ stream , $ callback );
91
+ }
92
+
93
+ /**
94
+ * Execute a callback when a signal is received.
95
+ *
96
+ * @param int $signo The signal number to monitor.
97
+ * @param callable $callback The callback to execute.
98
+ *
99
+ * @return string An identifier that can be used to cancel, enable or disable the event.
100
+ */
101
+ public function onSignal ($ signo , callable $ callback ) {
102
+ return $ this ->loop ->onSignal ($ signo , $ callback );
103
+ }
104
+
105
+ /**
106
+ * Execute a callback when an error occurs.
107
+ *
108
+ * @param callable $callback The callback to execute.
109
+ *
110
+ * @return string An identifier that can be used to cancel, enable or disable the event.
111
+ */
112
+ public function onError (callable $ callback ) {
113
+ throw new BadMethodCallException ("Not implemented yet. " );
114
+ }
115
+
116
+ /**
117
+ * Enable an event.
118
+ *
119
+ * @param string $eventIdentifier The event identifier.
120
+ *
121
+ * @return void
122
+ */
123
+ public function enable ($ eventIdentifier ) {
124
+ $ this ->loop ->enable ($ eventIdentifier );
125
+ }
126
+
127
+ /**
128
+ * Disable an event.
129
+ *
130
+ * @param string $eventIdentifier The event identifier.
131
+ *
132
+ * @return void
133
+ */
134
+ public function disable ($ eventIdentifier ) {
135
+ $ this ->loop ->disable ($ eventIdentifier );
136
+ }
137
+
138
+ /**
139
+ * Cancel an event.
140
+ *
141
+ * @param string $eventIdentifier The event identifier.
142
+ *
143
+ * @return void
144
+ */
145
+ public function cancel ($ eventIdentifier ) {
146
+ $ this ->loop ->cancel ($ eventIdentifier );
147
+ }
148
+
149
+ /**
150
+ * Reference an event.
151
+ *
152
+ * This will keep the event loop alive whilst the event is still being monitored. Events have this state by default.
153
+ *
154
+ * @param string $eventIdentifier The event identifier.
155
+ *
156
+ * @return void
157
+ */
158
+ public function reference ($ eventIdentifier ) {
159
+ throw new BadMethodCallException ("Not implemented yet. " );
160
+ }
161
+
162
+ /**
163
+ * Unreference an event.
164
+ *
165
+ * The event loop should exit the run method when only unreferenced events are still being monitored. Events are all
166
+ * referenced by default.
167
+ *
168
+ * @param string $eventIdentifier The event identifier.
169
+ *
170
+ * @return void
171
+ */
172
+ public function unreference ($ eventIdentifier ) {
173
+ throw new BadMethodCallException ("Not implemented yet. " );
174
+ }
175
+ }
0 commit comments