Skip to content

Commit

Permalink
Add events on Application.php
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodeholic committed Jul 26, 2020
1 parent e1c1239 commit dba81db
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
class Application
{
const EVENT_BEFORE_REQUEST = 'beforeRequest';
const EVENT_AFTER_REQUEST = 'afterRequest';

protected array $eventListeners = [];

public static Application $app;
public static string $ROOT_DIR;
public string $userClass;
Expand Down Expand Up @@ -73,6 +78,7 @@ public function logout()

public function run()
{
$this->triggerEvent(self::EVENT_BEFORE_REQUEST);
try {
echo $this->router->resolve();
} catch (\Exception $e) {
Expand All @@ -81,4 +87,17 @@ public function run()
]);
}
}

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 dba81db

Please sign in to comment.