Completed
Push — class-eventloop ( 51113e...323d1b )
by Vasily
04:01
created

EventLoop::init()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 9.4285
1
<?php
2
namespace PHPDaemon\Core;
3
use PHPDaemon\Structures\StackCallbacks;
4
5
class EventLoop
6
{
7
    public static $instance;
8
    protected $base;
9
    protected $dnsBase;
10
    protected $callbacks;
11
    protected $stopped = true;
12
13
14
    public static function init() {
15
        if (self::$instance !== null) {
16
            self::$instance->reinit();
17
        } else {
18
            self::$instance = new static;
19
        }
20
    }
21
22
    public function __construct() {
23
        $this->base = new \EventBase;
24
        $this->callbacks = new StackCallbacks;
25
        $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...
26
    }
27
28
    public function getBase() {
29
        return $this->base;
30
    }
31
32
    public function getDnsBase() {
33
        return $this->dnsBase;
34
    }
35
36
    public function reinit() {
37
        $this->base->reinit();
38
    }
39
40
    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...
41
        return \Event::signal($this->base, ...$args);
42
    }
43
44
    public function timer(...$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...
45
        return \Event::timer($this->base, ...$args);
46
    }
47
48
    public function listener(...$args) {
49
        return new \EventListener($this->base, ...$args);
50
    }
51
52
    public function bufferEvent(...$args) {
53
        return new \EventBufferEvent($this->base, ...$args);
54
    }
55
56
    public function bufferEventSsl(...$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...
57
        return \EventBufferEvent::sslSocket($this->base, ...$args);
58
    }
59
60
    public function interrupt($cb = null) {
61
        if ($cb !== null) {
62
            $this->callbacks->push($cb);
63
        }
64
        $this->base->exit();
65
    }
66
67
    public function stop() {
68
        $this->stopped = true;
69
        $this->interrupt();
70
    }
71
72
    public function event(...$args) {
73
        return new \Event($this->base, ...$args);
74
    }
75
76
    public function run() {
77
        $this->stopped = false;
78
        while (!$this->stopped) {
79
            $this->callbacks->executeAll($this);
80
            if (!$this->base->dispatch()) {
81
                break;
82
            }
83
        }
84
    }
85
}