EventLoop::isSignalActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Thruster\Component\EventLoop;
4
5
use Ev;
6
use EvIo;
7
use EvLoop;
8
use EvTimer;
9
use EvChild;
10
use EvSignal;
11
use SplObjectStorage;
12
13
/**
14
 * Class EventLoop
15
 *
16
 * @package Thruster\Component\EventLoop
17
 * @author  Aurimas Niekis <[email protected]>
18
 */
19
class EventLoop implements EventLoopInterface
20
{
21
    /**
22
     * @var EvLoop
23
     */
24
    protected $loop;
25
26
    /**
27
     * @var SplObjectStorage|EvTimer[]
28
     */
29
    protected $timers;
30
31
    /**
32
     * @var SplObjectStorage|EvSignal[]
33
     */
34
    protected $signals;
35
36
    /**
37
     * @var SplObjectStorage|EvChild[]
38
     */
39
    protected $children;
40
41
    /**
42
     * @var bool
43
     */
44
    protected $running;
45
46
    /**
47
     * @var EvIo[]
48
     */
49
    private $readEvents;
50
51
    /**
52
     * @var EvIo[]
53
     */
54
    private $writeEvents;
55
56
    public function __construct()
57
    {
58
        $this->loop        = EvLoop::defaultLoop();
59
        $this->timers      = new SplObjectStorage();
60
        $this->signals     = new SplObjectStorage();
61
        $this->children    = new SplObjectStorage();
62
        $this->readEvents  = [];
63
        $this->writeEvents = [];
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function addReadStream($stream, callable $listener) : self
70
    {
71
        $this->addStream($stream, $listener, Ev::READ);
72
73
        return $this;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function addWriteStream($stream, callable $listener) : self
80
    {
81
        $this->addStream($stream, $listener, Ev::WRITE);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89 View Code Duplication
    public function removeReadStream($stream) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91
        $key = (int) $stream;
92
        if (isset($this->readEvents[$key])) {
93
            $this->readEvents[$key]->stop();
94
            unset($this->readEvents[$key]);
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103 View Code Duplication
    public function removeWriteStream($stream) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $key = (int) $stream;
106
        if (isset($this->writeEvents[$key])) {
107
            $this->writeEvents[$key]->stop();
108
            unset($this->writeEvents[$key]);
109
        }
110
111
        return $this;
112
    }
113
114
    /**
115
     * @inheritdoc
116
     */
117
    public function removeStream($stream) : self
118
    {
119
        $this->removeReadStream($stream);
120
        $this->removeWriteStream($stream);
121
122
        return $this;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function addStream($stream, callable $listener, $flags) : self
129
    {
130
        $listener = function ($event) use ($stream, $listener) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
            call_user_func($listener, $stream, $this);
132
        };
133
134
        $event = $this->loop->io($stream, $flags, $listener);
135
136
        if (($flags & \Ev::READ) === $flags) {
137
            $this->readEvents[(int)$stream] = $event;
138
        } elseif (($flags & \Ev::WRITE) === $flags) {
139
            $this->writeEvents[(int)$stream] = $event;
140
        }
141
142
        return $this;
143
    }
144
145
    /**
146
     * @inheritdoc
147
     */
148 View Code Duplication
    public function addTimer($interval, callable $callback, int $priority = 0) : Timer
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $timer = new Timer($this, $interval, $callback, false, $priority);
151
152
        $callback = function ($evTimer) use ($timer) {
153
            call_user_func($timer->getCallback(), $timer, $evTimer);
154
155
            if ($this->isTimerActive($timer)) {
156
                $this->cancelTimer($timer);
157
            }
158
        };
159
160
        $event = $this->loop->timer($interval, 0, $callback, null, $priority);
161
162
        $this->timers->attach($timer, $event);
163
164
        return $timer;
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170 View Code Duplication
    public function addPeriodicTimer($interval, callable $callback, int $priority = 0) : Timer
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        $timer = new Timer($this, $interval, $callback, true, $priority);
173
174
        $internalCallback = function ($evTimer) use ($timer) {
175
            call_user_func($timer->getCallback(), $timer, $evTimer);
176
        };
177
178
        $event = $this->loop->periodic(
179
            $timer->getInterval(),
180
            $timer->getInterval(),
181
            null,
182
            $internalCallback,
183
            null,
184
            $priority
185
        );
186
187
        $this->timers->attach($timer, $event);
188
189
        return $timer;
190
    }
191
192
    /**
193
     * @inheritdoc
194
     */
195
    public function cancelTimer(Timer $timer) : self
196
    {
197
        if (isset($this->timers[$timer])) {
198
            $this->timers[$timer]->stop();
199
            $this->timers->detach($timer);
200
        }
201
202
        return $this;
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function isTimerActive(Timer $timer) : bool
209
    {
210
        return $this->timers->contains($timer);
211
    }
212
213
    /**
214
     * @inheritdoc
215
     */
216 View Code Duplication
    public function addSignal(int $signalNo, callable $callback, int $priority = 0) : Signal
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
    {
218
        $signal = new Signal($this, $signalNo, $callback, $priority);
219
220
        $internalCallback = function ($evSignal) use ($signal) {
221
            call_user_func($signal->getCallback(), $signal, $evSignal);
222
        };
223
224
        $event = $this->loop->signal($signalNo, $internalCallback, null, $priority);
225
226
        $this->signals->attach($signal, $event);
227
228
        return $signal;
229
    }
230
231
    /**
232
     * @inheritdoc
233
     */
234
    public function cancelSignal(Signal $signal) : self
235
    {
236
        if (isset($this->signals[$signal])) {
237
            $this->signals[$signal]->stop();
238
            $this->signals->detach($signal);
239
        }
240
241
        return $this;
242
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247
    public function isSignalActive(Signal $signal) : bool
248
    {
249
        return $this->signals->contains($signal);
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255 View Code Duplication
    public function addChild(callable $callback, int $pid = 0, int $priority = 0) : Child
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
256
    {
257
        $child = new Child($this, $pid, $callback, $priority);
258
259
        $internalCallback = function ($evChild) use ($child) {
260
            call_user_func($child->getCallback(), $child, $evChild);
261
        };
262
263
        $event = $this->loop->child($pid, false, $internalCallback, null, $priority);
264
265
        $this->signals->attach($child, $event);
266
267
        return $child;
268
    }
269
270
    /**
271
     * @inheritdoc
272
     */
273
    public function cancelChild(Child $child) : self
274
    {
275
        if (isset($this->children[$child])) {
276
            $this->children[$child]->stop();
277
            $this->children->detach($child);
278
        }
279
280
        return $this;
281
    }
282
283
    /**
284
     * @inheritdoc
285
     */
286
    public function isChildActive(Child $child) : bool
287
    {
288
        return $this->children->contains($child);
289
    }
290
291
    /**
292
     * @inheritdoc
293
     */
294
    public function afterFork() : self
295
    {
296
        $this->loop->loopFork();
297
298
        return $this;
299
    }
300
301
    /**
302
     * {@inheritDoc}
303
     */
304
    public function tick() : self
305
    {
306
        $this->loop->run(Ev::RUN_ONCE | Ev::RUN_NOWAIT);
307
308
        return $this;
309
    }
310
311
    /**
312
     * @inheritdoc
313
     */
314
    public function run() : self
315
    {
316
        $this->running = true;
317
318
        while ($this->running) {
319
            $flags = Ev::RUN_ONCE;
320
321
            if ($this->timers->count() < 1 &&
322
                $this->signals->count() < 1 &&
323
                $this->children->count() < 1 &&
324
                count($this->readEvents) < 1 &&
325
                count($this->writeEvents) < 1
326
            ) {
327
                break;
328
            }
329
330
            $this->loop->run($flags);
331
        }
332
333
        return $this;
334
    }
335
336
    /**
337
     * @inheritdoc
338
     */
339
    public function stop() : self
340
    {
341
        $this->running = false;
342
343
        return $this;
344
    }
345
}
346