Skip to content

Commit

Permalink
improve event
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Sep 11, 2024
1 parent 1122dbb commit 9ca3940
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/application/common/core/AppLifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@
class AppLifecycle
{

protected static $events = [];
public static function addEvent($event, $callback)
{
if (!isset(self::$events[$event])) {
self::$events[$event] = [];
}
self::$events[$event][] = $callback;
}

public static function triggerEvent($event, ...$args)
{
if (!empty(self::$events[$event])) {
foreach (self::$events[$event] as $callback) {
try {
if (empty($args)) $args = [];
call_user_func_array($callback, $args);
} catch (\Exception $e) {
Log::warning("Event call error: $event" . var_export($callback) . "\n $e");
}
}
}
}

public function appInit()
{
Log::record('app_init');
Expand All @@ -24,21 +47,25 @@ public function appInit()
public function appBegin()
{
Log::record('app_begin');
static::triggerEvent('app_begin');
}

public function moduleInit()
{
Log::record('module_init');
static::triggerEvent('module_init');
}

public function actionBegin()
{
Log::record('action_begin');
static::triggerEvent('action_begin');
}

public function viewFilter()
{
Log::record('view_filter');
static::triggerEvent('view_filter');
}
public function logWrite()
{
Expand All @@ -47,5 +74,6 @@ public function logWrite()
public function appEnd()
{
Log::record('app_end');
static::triggerEvent('app_end');
}
}

0 comments on commit 9ca3940

Please sign in to comment.