Skip to content

Commit

Permalink
Update Application.php
Browse files Browse the repository at this point in the history
Mosaab4 authored Aug 27, 2021
1 parent d76beac commit b662b55
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion Application.php
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@

class Application
{
const EVENT_BEFORE_REQUEST = 'beforeRequest';
const EVENT_AFTER_REQUEST = 'afterRequest';

protected array $eventListeners = [];

public static string $ROOT_DIR;
public static Application $app;
public string $layout = 'main';
@@ -50,6 +55,8 @@ public static function isGuest(): bool

public function run()
{
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);

try {
echo $this->router->resolve();
} catch (Exception $e) {
@@ -77,4 +84,19 @@ public function logout()
$this->user = null;
$this->session->remove('user');
}
}

public function triggerEvent($eventName)
{
$callbacks = $this->eventListeners[$eventName] ?? [];

foreach ($callbacks as $callback){

call_user_func($callback);
}
}

public function on($eventName, $callback)
{
$this->eventListeners[$eventName][] = $callback;
}
}

0 comments on commit b662b55

Please sign in to comment.