1 | <?php |
||
43 | class Events implements SingletonInterface |
||
44 | { |
||
45 | /** @var array Holds all the events and their callbacks */ |
||
46 | private $events = []; |
||
47 | |||
48 | /** @var string|null Name of the event in progress, null if there are no active events */ |
||
49 | private $inProgress = null; |
||
50 | |||
51 | /** @var Context The current context */ |
||
52 | private $context; |
||
53 | |||
54 | /** |
||
55 | * Events constructor. |
||
56 | * |
||
57 | * @param Context $context |
||
58 | */ |
||
59 | public function __construct(Context $context) |
||
63 | |||
64 | /** |
||
65 | * Load every PHP Events files under the directory |
||
66 | * |
||
67 | * @param string $path Path to the module |
||
68 | */ |
||
69 | public function loadPath(string $path) : void |
||
88 | |||
89 | /** |
||
90 | * Register a new event. |
||
91 | * |
||
92 | * @param string $name The name of the event |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function register(string $name) : bool |
||
106 | |||
107 | /** |
||
108 | * Check if the event exists by it's name. |
||
109 | * |
||
110 | * @param string $name The name of the event |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isRegistered(string $name) : bool |
||
118 | |||
119 | /** |
||
120 | * Remove an event and un-hook everything related to it. |
||
121 | * |
||
122 | * @param string $name The name of the event |
||
123 | */ |
||
124 | public function unregister(string $name) : void |
||
128 | |||
129 | /** |
||
130 | * Hook a callable to an event. |
||
131 | * If the event is not registered, register it. |
||
132 | * |
||
133 | * @param string $name The name of the event to hook on |
||
134 | * @param callable $callback The callback to call once the event is fired |
||
135 | * @param int $priority The priority order of the callback |
||
136 | * |
||
137 | * @return bool |
||
138 | */ |
||
139 | public function hook(string $name, callable $callback, int $priority = 0) : bool |
||
152 | |||
153 | /** |
||
154 | * Fire an event and call all the hooked callables. |
||
155 | * |
||
156 | * @param string $name The name of the event to fire |
||
157 | * @param array $params The parameters to pass down on the callbacks |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function fire(string $name, array $params = []) : bool |
||
181 | |||
182 | /** |
||
183 | * Returns the name of the event being fired, null otherwise. |
||
184 | * |
||
185 | * @return string|null |
||
186 | */ |
||
187 | public function getInProgress() : ?string |
||
191 | |||
192 | /** |
||
193 | * Returns true if an event is being fired. |
||
194 | * |
||
195 | * @return bool |
||
196 | */ |
||
197 | public function isInProgress() : bool |
||
201 | } |
||
202 |