File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change 77
88class 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+ }
You can’t perform that action at this time.
0 commit comments