File tree Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Expand file tree Collapse file tree 1 file changed +19
-0
lines changed Original file line number Diff line number Diff line change 17
17
*/
18
18
class Application
19
19
{
20
+ const EVENT_BEFORE_REQUEST = 'beforeRequest ' ;
21
+ const EVENT_AFTER_REQUEST = 'afterRequest ' ;
22
+
23
+ protected array $ eventListeners = [];
24
+
20
25
public static Application $ app ;
21
26
public static string $ ROOT_DIR ;
22
27
public string $ userClass ;
@@ -73,6 +78,7 @@ public function logout()
73
78
74
79
public function run ()
75
80
{
81
+ $ this ->triggerEvent (self ::EVENT_BEFORE_REQUEST );
76
82
try {
77
83
echo $ this ->router ->resolve ();
78
84
} catch (\Exception $ e ) {
@@ -81,4 +87,17 @@ public function run()
81
87
]);
82
88
}
83
89
}
90
+
91
+ public function triggerEvent ($ eventName )
92
+ {
93
+ $ callbacks = $ this ->eventListeners [$ eventName ] ?? [];
94
+ foreach ($ callbacks as $ callback ) {
95
+ call_user_func ($ callback );
96
+ }
97
+ }
98
+
99
+ public function on ($ eventName , $ callback )
100
+ {
101
+ $ this ->eventListeners [$ eventName ][] = $ callback ;
102
+ }
84
103
}
You can’t perform that action at this time.
0 commit comments