Skip to content

Commit dba81db

Browse files
committed
Add events on Application.php
1 parent e1c1239 commit dba81db

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Application.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
*/
1818
class Application
1919
{
20+
const EVENT_BEFORE_REQUEST = 'beforeRequest';
21+
const EVENT_AFTER_REQUEST = 'afterRequest';
22+
23+
protected array $eventListeners = [];
24+
2025
public static Application $app;
2126
public static string $ROOT_DIR;
2227
public string $userClass;
@@ -73,6 +78,7 @@ public function logout()
7378

7479
public function run()
7580
{
81+
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);
7682
try {
7783
echo $this->router->resolve();
7884
} catch (\Exception $e) {
@@ -81,4 +87,17 @@ public function run()
8187
]);
8288
}
8389
}
90+
91+
public function triggerEvent($eventName)
92+
{
93+
$callbacks = $this->eventListeners[$eventName] ?? [];
94+
foreach ($callbacks as $callback) {
95+
call_user_func($callback);
96+
}
97+
}
98+
99+
public function on($eventName, $callback)
100+
{
101+
$this->eventListeners[$eventName][] = $callback;
102+
}
84103
}

0 commit comments

Comments
 (0)