Skip to content

Commit b662b55

Browse files
authored
Update Application.php
1 parent d76beac commit b662b55

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Application.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class Application
99
{
10+
const EVENT_BEFORE_REQUEST = 'beforeRequest';
11+
const EVENT_AFTER_REQUEST = 'afterRequest';
12+
13+
protected array $eventListeners = [];
14+
1015
public static string $ROOT_DIR;
1116
public static Application $app;
1217
public string $layout = 'main';
@@ -50,6 +55,8 @@ public static function isGuest(): bool
5055

5156
public function run()
5257
{
58+
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);
59+
5360
try {
5461
echo $this->router->resolve();
5562
} catch (Exception $e) {
@@ -77,4 +84,19 @@ public function logout()
7784
$this->user = null;
7885
$this->session->remove('user');
7986
}
80-
}
87+
88+
public function triggerEvent($eventName)
89+
{
90+
$callbacks = $this->eventListeners[$eventName] ?? [];
91+
92+
foreach ($callbacks as $callback){
93+
94+
call_user_func($callback);
95+
}
96+
}
97+
98+
public function on($eventName, $callback)
99+
{
100+
$this->eventListeners[$eventName][] = $callback;
101+
}
102+
}

0 commit comments

Comments
 (0)