Passed
Push — master ( 4e4f7a...1e85e8 )
by 世昌
02:24
created

Event::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace suda\framework;
3
4
use function array_key_exists;
5
use ReflectionException;
6
use suda\framework\event\EventFilter;
0 ignored issues
show
Bug introduced by
The type suda\framework\event\EventFilter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use suda\framework\runnable\Runnable;
8
9
class Event
10
{
11
    protected $queue = [];
12
13
    /**
14
     * 加载事件处理
15
     *
16
     * @param array $arrays
17
     * @return void
18
     */
19
    public function load(array $arrays)
20
    {
21
        $this->queue = array_merge_recursive($this->queue, $arrays);
22
    }
23
24
    /**
25
     * 注册一条命令
26
     *
27
     * @param string $name
28
     * @param mixed $command
29
     * @return void
30
     */
31
    public function listen(string $name, $command)
32
    {
33
        $this->add($name, $command);
34
    }
35
36
    /**
37
     * 注册一条命令
38
     *
39
     * @param string $name
40
     * @param mixed $command
41
     * @return void
42
     */
43
    public function register(string $name, $command)
44
    {
45
        $this->add($name, $command);
46
    }
47
48
    /**
49
     * 添加命令到底部
50
     *
51
     * @param string $name
52
     * @param mixed $command
53
     * @return void
54
     */
55
    public function add(string $name, $command)
56
    {
57
        $this->queue[$name][] = $command;
58
    }
59
60
    /**
61
     * 添加命令到顶部
62
     *
63
     * @param string $name
64
     * @param mixed $command
65
     * @return void
66
     */
67
    public function addTop(string $name, $command)
68
    {
69
        if (array_key_exists($name, $this->queue) && is_array($this->queue[$name])) {
70
            array_unshift($this->queue[$name], $command);
71
        } else {
72
            $this->add($name, $command);
73
        }
74
    }
75
76
    /**
77
     * 移除一条命令
78
     *
79
     * @param string $name
80
     * @param mixed $remove
81
     * @return void
82
     */
83
    public function remove(string $name, $remove)
84
    {
85
        if (array_key_exists($name, $this->queue) && is_array($this->queue[$name])) {
86
            foreach ($this->queue[$name] as $key => $command) {
87
                if ($command === $remove) {
88
                    unset($this->queue[$name][$key]);
89
                }
90
            }
91
        }
92
    }
93
94
    /**
95
     * 运行所有命令
96
     *
97
     * @param string $name
98
     * @param array $args
99
     * @return void
100
     * @throws ReflectionException
101
     */
102
    public function exec(string $name, array $args = [])
103
    {
104
        if ($this->hasListenEvent($name)) {
105
            foreach ($this->queue[$name] as $command) {
106
                $this->call($command, $args);
107
            }
108
        }
109
    }
110
111
    /**
112
     * 继续执行
113
     *
114
     * @param string $name
115
     * @param mixed $value
116
     * @param array $args
117
     * @return mixed
118
     * @throws ReflectionException
119
     */
120
    public function process(string $name, $value, array $args = [])
121
    {
122
        if ($this->hasListenEvent($name)) {
123
            array_unshift($args, $value);
124
            foreach ($this->queue[$name] as $command) {
125
                $args[0] = $value;
126
                $value = $this->call($command, $args);
127
            }
128
        }
129
        return $value;
130
    }
131
132
    /**
133
     * 继续执行
134
     *
135
     * @param string $name
136
     * @param array $args
137
     * @param mixed $condition
138
     * @return boolean
139
     * @throws ReflectionException
140
     */
141
    public function next(string $name, array $args = [], $condition = true):bool
142
    {
143
        if ($this->hasListenEvent($name)) {
144
            foreach ($this->queue[$name] as $command) {
145
                if ($this->call($command, $args) === $condition) {
146
                    continue;
147
                } else {
148
                    return false;
149
                }
150
            }
151
        }
152
        return true;
153
    }
154
155
    /**
156
     * 运行最先注入的命令
157
     *
158
     * @param string $name
159
     * @param array $args
160
     * @return mixed
161
     * @throws ReflectionException
162
     */
163
    public function execFirst(string $name, array $args = [])
164
    {
165
        if ($this->hasListenEvent($name)) {
166
            return  $this->call(array_shift($this->queue[$name]), $args);
167
        }
168
        return null;
169
    }
170
171
    /**
172
     * 运行最后一个注入的命令
173
     *
174
     * @param string $name
175
     * @param array $args
176
     * @return mixed
177
     * @throws ReflectionException
178
     */
179
    public function execLast(string $name, array $args = [])
180
    {
181
        if ($this->hasListenEvent($name)) {
182
            return $this->call(array_pop($this->queue[$name]), $args);
183
        }
184
        return null;
185
    }
186
187
    /**
188
     * 调用对象
189
     *
190
     * @param mixed $command
191
     * @param array $args
192
     * @return mixed
193
     * @throws ReflectionException
194
     */
195
    protected function call($command, array &$args)
196
    {
197
        return (new Runnable($command))->apply($args);
198
    }
199
200
    /**
201
     * 判断是监控事件
202
     *
203
     * @param string $name
204
     * @return boolean
205
     */
206
    public function hasListenEvent(string $name):bool
207
    {
208
        return array_key_exists($name, $this->queue) && is_array($this->queue[$name]);
209
    }
210
}
211