2525 * and generating an HTTP response.
2626 *
2727 * @license MIT, https://docs.flightphp.com/license
28- * @copyright Copyright (c) 2011-2025, Mike Cao <mike@mikecao.com>, n0nag0n <n0nag0n@sky-9.com>
28+ * @copyright Copyright (c) 2011-2026,
29+ * Mike Cao <mike@mikecao.com>, n0nag0n <n0nag0n@sky-9.com>, fadrian06 <https://github.com/fadrian06>
2930 *
3031 * @method void start()
3132 * @method void stop(?int $code = null)
5657 * @method void etag(string $id, string $type = 'strong')
5758 * @method void lastModified(int $time)
5859 * @method void download(string $filePath)
59- *
60- * @phpstan-template EngineTemplate of object
61- * @phpstan-method void registerContainerHandler(ContainerInterface|callable(class-string<EngineTemplate> $id, array<int|string, mixed> $params): ?EngineTemplate $containerHandler)
62- * @phpstan-method Route route(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
63- * @phpstan-method void group(string $pattern, callable $callback, (class-string|callable|array{0: class-string, 1: string})[] $group_middlewares = [])
64- * @phpstan-method Route post(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
65- * @phpstan-method Route put(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
66- * @phpstan-method Route patch(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
67- * @phpstan-method Route delete(string $pattern, callable|string|array{0: class-string, 1: string} $callback, bool $pass_route = false, string $alias = '')
68- * @phpstan-method void resource(string $pattern, class-string $controllerClass, array<string, string|array<string>> $methods = [])
69- * @phpstan-method string getUrl(string $alias, array<string, mixed> $params = [])
70- * @phpstan-method void before(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
71- * @phpstan-method void after(string $name, Closure(array<int, mixed> &$params, string &$output): (void|false) $callback)
72- * @phpstan-method void set(string|iterable<string, mixed> $key, ?mixed $value = null)
73- * @phpstan-method mixed get(?string $key)
74- * @phpstan-method void render(string $file, ?array<string, mixed> $data = null, ?string $key = null)
75- * @phpstan-method void json(mixed $data, int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
76- * @phpstan-method void jsonHalt(mixed $data, int $code = 200, bool $encode = true, string $charset = 'utf-8', int $option = 0)
77- * @phpstan-method void jsonp(mixed $data, string $param = 'jsonp', int $code = 200, bool $encode = true, string $charset = "utf8", int $encodeOption = 0, int $encodeDepth = 512)
78- *
79- * Note: IDEs will use standard @method tags for autocompletion, while PHPStan will use @phpstan-* tags for advanced type checking.
80- *
81- * phpcs:disable PSR2.Methods.MethodDeclaration.Underscore
8260 */
8361class Engine
8462{
85- /**
86- * @var array<string> List of methods that can be extended in the Engine class.
87- */
63+ /** @var array<int, string> List of methods that can be extended in the Engine class */
8864 private const MAPPABLE_METHODS = [
8965 'start ' ,
9066 'stop ' ,
@@ -112,22 +88,17 @@ class Engine
11288 'triggerEvent '
11389 ];
11490
115- /** @var array<string, mixed> Stored variables. */
91+ /** @var array<string, mixed> */
11692 protected array $ vars = [];
11793
118- /** Class loader. */
11994 protected Loader $ loader ;
120-
121- /** @var Dispatcher<EngineTemplate> Method and class dispatcher. */
12295 protected Dispatcher $ dispatcher ;
123-
124- /** Event dispatcher. */
12596 protected EventDispatcher $ eventDispatcher ;
12697
127- /** If the framework has been initialized or not. */
98+ /** If the framework has been initialized or not */
12899 protected bool $ initialized = false ;
129100
130- /** If the request has been handled or not. */
101+ /** If the request has been handled or not */
131102 protected bool $ requestHandled = false ;
132103
133104 public function __construct ()
@@ -138,27 +109,24 @@ public function __construct()
138109 }
139110
140111 /**
141- * Handles calls to class methods.
142- *
143112 * @param string $name Method name
144- * @param array<int, mixed> $params Method parameters
145- *
146- * @throws Exception
147- * @return mixed Callback results
113+ * @param array<int, mixed> $arguments Method parameters
114+ * @throws Throwable
115+ * @return mixed
148116 */
149- public function __call (string $ name , array $ params )
117+ public function __call (string $ name , array $ arguments )
150118 {
151119 $ callback = $ this ->dispatcher ->get ($ name );
152120
153- if (\ is_callable ($ callback )) {
154- return $ this ->dispatcher ->run ($ name , $ params );
121+ if (is_callable ($ callback )) {
122+ return $ this ->dispatcher ->run ($ name , $ arguments );
155123 }
156124
157125 if (!$ this ->loader ->get ($ name )) {
158126 throw new Exception ("$ name must be a mapped method. " );
159127 }
160128
161- $ shared = empty ($ params ) || $ params [0 ];
129+ $ shared = empty ($ arguments ) || $ arguments [0 ];
162130
163131 return $ this ->loader ->load ($ name , $ shared );
164132 }
@@ -167,13 +135,10 @@ public function __call(string $name, array $params)
167135 // Core Methods //
168136 //////////////////
169137
170- /** Initializes the framework. */
138+ /** Initializes the framework */
171139 public function init (): void
172140 {
173- $ initialized = $ this ->initialized ;
174- $ self = $ this ;
175-
176- if ($ initialized ) {
141+ if ($ this ->initialized ) {
177142 $ this ->vars = [];
178143 $ this ->loader ->reset ();
179144 $ this ->dispatcher ->reset ();
@@ -183,16 +148,14 @@ public function init(): void
183148 $ this ->dispatcher ->setEngine ($ this );
184149
185150 // Register default components
186- $ this ->map ('eventDispatcher ' , function () {
187- return EventDispatcher::getInstance ();
188- });
151+ $ this ->map ('eventDispatcher ' , static fn (): EventDispatcher => EventDispatcher::getInstance ());
189152 $ this ->loader ->register ('request ' , Request::class);
190153 $ this ->loader ->register ('response ' , Response::class);
191154 $ this ->loader ->register ('router ' , Router::class);
192155
193- $ this ->loader ->register ('view ' , View::class, [], function (View $ view ) use ( $ self ) {
194- $ view ->path = $ self ->get ('flight.views.path ' );
195- $ view ->extension = $ self ->get ('flight.views.extension ' );
156+ $ this ->loader ->register ('view ' , View::class, [], function (View $ view ) {
157+ $ view ->path = $ this ->get ('flight.views.path ' );
158+ $ view ->extension = $ this ->get ('flight.views.extension ' );
196159 });
197160
198161 foreach (self ::MAPPABLE_METHODS as $ name ) {
@@ -210,21 +173,23 @@ public function init(): void
210173 $ this ->set ('flight.v2.output_buffering ' , false );
211174
212175 // Startup configuration
213- $ this ->before ('start ' , function () use ( $ self ) {
176+ $ this ->before ('start ' , function () {
214177 // Enable error handling
215- if ($ self ->get ('flight.handle_errors ' )) {
216- set_error_handler ([$ self , 'handleError ' ]);
217- set_exception_handler ([$ self , 'handleException ' ]);
178+ if ($ this ->get ('flight.handle_errors ' )) {
179+ set_error_handler ([$ this , 'handleError ' ]);
180+ set_exception_handler ([$ this , 'handleException ' ]);
218181 }
219182
220183 // Set case-sensitivity
221- $ self ->router ()->caseSensitive = $ self ->get ('flight.case_sensitive ' );
184+ $ this ->router ()->caseSensitive = $ this ->get ('flight.case_sensitive ' );
185+
222186 // Set Content-Length
223- $ self ->response ()->content_length = $ self ->get ('flight.content_length ' );
187+ $ this ->response ()->content_length = $ this ->get ('flight.content_length ' );
188+
224189 // This is to maintain legacy handling of output buffering
225190 // which causes a lot of problems. This will be removed
226191 // in v4
227- $ self ->response ()->v2_output_buffering = $ this ->get ('flight.v2.output_buffering ' );
192+ $ this ->response ()->v2_output_buffering = $ this ->get ('flight.v2.output_buffering ' );
228193 });
229194
230195 $ this ->initialized = true ;
0 commit comments