1 | <?php |
||
16 | class Event { |
||
17 | use |
||
18 | Singleton; |
||
19 | const INIT_STATE_METHOD = 'init'; |
||
20 | /** |
||
21 | * @var callable[][] |
||
22 | */ |
||
23 | protected $callbacks; |
||
24 | /** |
||
25 | * @var callable[][] |
||
26 | */ |
||
27 | protected $callbacks_cache; |
||
28 | protected function init () { |
||
31 | /** |
||
32 | * Add event handler |
||
33 | * |
||
34 | * @param string $event For example `admin/System/components/plugins/disable` |
||
35 | * @param callable $callback Callable, that will be called at event dispatching |
||
36 | * |
||
37 | * @return Event |
||
38 | */ |
||
39 | function on ($event, $callback) { |
||
46 | /** |
||
47 | * Remove event handler |
||
48 | * |
||
49 | * @param string $event |
||
50 | * @param callable|null $callback If not specified - all callbacks for this event will be removed |
||
51 | * |
||
52 | * @return Event |
||
53 | */ |
||
54 | function off ($event, $callback = null) { |
||
70 | /** |
||
71 | * Similar to `::on()`, but but removes handler after handling of first event |
||
72 | * |
||
73 | * @param string $event |
||
74 | * @param callable $callback |
||
75 | * |
||
76 | * @return Event |
||
77 | */ |
||
78 | function once ($event, $callback) { |
||
88 | /** |
||
89 | * Fire event |
||
90 | * |
||
91 | * After event name it is possible to specify as many arguments as needed |
||
92 | * |
||
93 | * @param string $event For example `admin/System/components/plugins/disable` |
||
94 | * @param mixed[] $arguments |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | function fire ($event, ...$arguments) { |
||
113 | /** |
||
114 | * Before firing events we need to ensure that events callbacks were registered |
||
115 | */ |
||
116 | protected function ensure_events_registered () { |
||
124 | /** |
||
125 | * Initialize all events handlers |
||
126 | */ |
||
127 | protected function register_events () { |
||
132 | /** |
||
133 | * @return string[] |
||
134 | */ |
||
135 | protected function events_files_paths () { |
||
154 | } |
||
155 |