Completed
Push — class-eventloop ( 1db9ec )
by Vasily
05:03
created

EventLoop::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 5
rs 9.4285
1
<?php
2
namespace PHPDaemon\Core;
3
use PHPDaemon\Structures\StackCallbacks;
4
5
class EventLoop
6
{
7
    protected $base;
8
    protected $dnsBase;
9
    protected $callbacks;
10
    protected $stopped = true;
11
12
    public function __construct() {
13
        $this->base = new \EventBase;
14
        $this->callbacks = new StackCallbacks;
15
        $this->dnsBase = new \EventDnsBase($this->base, false); // @TODO: test with true
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
16
    }
17
18
    public function getBase() {
19
        return $this->base;
20
    }
21
22
    public function getDnsBase() {
23
        return $this->dnsBase;
24
    }
25
26
    public function reinit() {
27
        $this->base->reinit();
28
        return $this;
29
    }
30
31
    public function signal(...$args) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
32
        return \Event::signal($this->base, ...$args);
33
    }
34
35
    public function interrupt($cb = null) {
36
        if ($cb !== null) {
37
            $this->callbacks->push($cb);
38
        }
39
        $this->base->exit();
40
    }
41
42
    public function stop() {
43
        $this->stopped = true;
44
        $this->interrupt();
45
    }
46
47
    public function run() {
48
        $this->stopped = false;
49
        while (!$this->stopped) {
50
            $this->callbacks->executeAll($this);
51
            if (!$this->base->dispatch()) {
52
                break;
53
            }
54
        }
55
    }
56
}