11<?php
22
33declare (strict_types=1 );
4+
45/**
56 * Flight: An extensible micro-framework.
67 *
3233 * @method void start() Starts engine
3334 * @method void stop() Stops framework and outputs current response
3435 * @method void halt(int $code = 200, string $message = '') Stops processing and returns a given response.
35- *
36+ *
3637 * Routing
3738 * @method Route route(string $pattern, callable $callback, bool $pass_route = false, string $alias = '') Routes a URL to a callback function with all applicable methods
3839 * @method void group(string $pattern, callable $callback, array $group_middlewares = []) Groups a set of routes together under a common prefix.
@@ -78,12 +79,12 @@ class Engine
7879 */
7980 protected Dispatcher $ dispatcher ;
8081
81- /**
82- * If the framework has been initialized or not
83- *
84- * @var boolean
85- */
86- protected bool $ initialized = false ;
82+ /**
83+ * If the framework has been initialized or not
84+ *
85+ * @var boolean
86+ */
87+ protected bool $ initialized = false ;
8788
8889 /**
8990 * Constructor.
@@ -377,7 +378,7 @@ public function _start(): void
377378 ob_start ();
378379
379380 // Route the request
380- $ failed_middleware_check = false ;
381+ $ failed_middleware_check = false ;
381382 while ($ route = $ router ->route ($ request )) {
382383 $ params = array_values ($ route ->params );
383384
@@ -386,55 +387,52 @@ public function _start(): void
386387 $ params [] = $ route ;
387388 }
388389
389- // Run any before middlewares
390- if (count ($ route ->middleware ) > 0 ) {
391- foreach ($ route ->middleware as $ middleware ) {
392-
393- $ middleware_object = (is_callable ($ middleware ) === true ? $ middleware : (method_exists ($ middleware , 'before ' ) === true ? [ $ middleware , 'before ' ]: false ));
390+ // Run any before middlewares
391+ if (count ($ route ->middleware ) > 0 ) {
392+ foreach ($ route ->middleware as $ middleware ) {
393+ $ middleware_object = (is_callable ($ middleware ) === true ? $ middleware : (method_exists ($ middleware , 'before ' ) === true ? [ $ middleware , 'before ' ] : false ));
394394
395- if ($ middleware_object === false ) {
396- continue ;
397- }
395+ if ($ middleware_object === false ) {
396+ continue ;
397+ }
398398
399- // It's assumed if you don't declare before, that it will be assumed as the before method
400- $ middleware_result = $ middleware_object ($ route ->params );
399+ // It's assumed if you don't declare before, that it will be assumed as the before method
400+ $ middleware_result = $ middleware_object ($ route ->params );
401401
402- if ($ middleware_result === false ) {
403- $ failed_middleware_check = true ;
404- break 2 ;
405- }
406- }
407- }
402+ if ($ middleware_result === false ) {
403+ $ failed_middleware_check = true ;
404+ break 2 ;
405+ }
406+ }
407+ }
408408
409409 // Call route handler
410410 $ continue = $ this ->dispatcher ->execute (
411411 $ route ->callback ,
412412 $ params
413413 );
414-
415414
416- // Run any before middlewares
417- if (count ($ route ->middleware ) > 0 ) {
418-
419- // process the middleware in reverse order now
420- foreach (array_reverse ($ route ->middleware ) as $ middleware ) {
421415
422- // must be an object. No functions allowed here
423- $ middleware_object = is_object ($ middleware ) === true && !($ middleware instanceof Closure) && method_exists ($ middleware , 'after ' ) === true ? [ $ middleware , 'after ' ] : false ;
416+ // Run any before middlewares
417+ if (count ($ route ->middleware ) > 0 ) {
418+ // process the middleware in reverse order now
419+ foreach (array_reverse ($ route ->middleware ) as $ middleware ) {
420+ // must be an object. No functions allowed here
421+ $ middleware_object = is_object ($ middleware ) === true && !($ middleware instanceof Closure) && method_exists ($ middleware , 'after ' ) === true ? [ $ middleware , 'after ' ] : false ;
424422
425- // has to have the after method, otherwise just skip it
426- if ($ middleware_object === false ) {
427- continue ;
428- }
423+ // has to have the after method, otherwise just skip it
424+ if ($ middleware_object === false ) {
425+ continue ;
426+ }
429427
430- $ middleware_result = $ middleware_object ($ route ->params );
428+ $ middleware_result = $ middleware_object ($ route ->params );
431429
432- if ($ middleware_result === false ) {
433- $ failed_middleware_check = true ;
434- break 2 ;
435- }
436- }
437- }
430+ if ($ middleware_result === false ) {
431+ $ failed_middleware_check = true ;
432+ break 2 ;
433+ }
434+ }
435+ }
438436
439437 $ dispatched = true ;
440438
@@ -447,9 +445,9 @@ public function _start(): void
447445 $ dispatched = false ;
448446 }
449447
450- if ($ failed_middleware_check === true ) {
451- $ this ->halt (403 , 'Forbidden ' );
452- } else if ($ dispatched === false ) {
448+ if ($ failed_middleware_check === true ) {
449+ $ this ->halt (403 , 'Forbidden ' );
450+ } elseif ($ dispatched === false ) {
453451 $ this ->notFound ();
454452 }
455453 }
@@ -476,11 +474,11 @@ public function _error($e): void
476474 ->status (500 )
477475 ->write ($ msg )
478476 ->send ();
479- // @codeCoverageIgnoreStart
477+ // @codeCoverageIgnoreStart
480478 } catch (Throwable $ t ) {
481479 exit ($ msg );
482480 }
483- // @codeCoverageIgnoreEnd
481+ // @codeCoverageIgnoreEnd
484482 }
485483
486484 /**
@@ -512,20 +510,20 @@ public function _stop(?int $code = null): void
512510 * @param string $pattern URL pattern to match
513511 * @param callable $callback Callback function
514512 * @param bool $pass_route Pass the matching route object to the callback
515- * @param string $alias the alias for the route
516- * @return Route
513+ * @param string $alias the alias for the route
514+ * @return Route
517515 */
518516 public function _route (string $ pattern , callable $ callback , bool $ pass_route = false , string $ alias = '' ): Route
519517 {
520518 return $ this ->router ()->map ($ pattern , $ callback , $ pass_route , $ alias );
521519 }
522520
523- /**
521+ /**
524522 * Routes a URL to a callback function.
525523 *
526- * @param string $pattern URL pattern to match
527- * @param callable $callback Callback function that includes the Router class as first parameter
528- * @param array<callable> $group_middlewares The middleware to be applied to the route
524+ * @param string $pattern URL pattern to match
525+ * @param callable $callback Callback function that includes the Router class as first parameter
526+ * @param array<callable> $group_middlewares The middleware to be applied to the route
529527 */
530528 public function _group (string $ pattern , callable $ callback , array $ group_middlewares = []): void
531529 {
@@ -585,7 +583,7 @@ public function _delete(string $pattern, callable $callback, bool $pass_route =
585583 *
586584 * @param int $code HTTP status code
587585 * @param string $message Response message
588- *
586+ *
589587 */
590588 public function _halt (int $ code = 200 , string $ message = '' ): void
591589 {
@@ -594,10 +592,10 @@ public function _halt(int $code = 200, string $message = ''): void
594592 ->status ($ code )
595593 ->write ($ message )
596594 ->send ();
597- // apologies for the crappy hack here...
598- if ($ message !== 'skip---exit ' ) {
599- exit (); // @codeCoverageIgnore
600- }
595+ // apologies for the crappy hack here...
596+ if ($ message !== 'skip---exit ' ) {
597+ exit (); // @codeCoverageIgnore
598+ }
601599 }
602600
603601 /**
@@ -728,7 +726,7 @@ public function _etag(string $id, string $type = 'strong'): void
728726 {
729727 $ id = (('weak ' === $ type ) ? 'W/ ' : '' ) . $ id ;
730728
731- $ this ->response ()->header ('ETag ' , '" ' . str_replace ('" ' , '\" ' , $ id ). '" ' );
729+ $ this ->response ()->header ('ETag ' , '" ' . str_replace ('" ' , '\" ' , $ id ) . '" ' );
732730
733731 if (
734732 isset ($ _SERVER ['HTTP_IF_NONE_MATCH ' ]) &&
@@ -755,14 +753,14 @@ public function _lastModified(int $time): void
755753 }
756754 }
757755
758- /**
759- * Gets a url from an alias that's supplied.
760- *
761- * @param string $alias the route alias.
762- * @param array<string, mixed> $params The params for the route if applicable.
763- */
764- public function _getUrl (string $ alias , array $ params = []): string
765- {
766- return $ this ->router ()->getUrlByAlias ($ alias , $ params );
767- }
756+ /**
757+ * Gets a url from an alias that's supplied.
758+ *
759+ * @param string $alias the route alias.
760+ * @param array<string, mixed> $params The params for the route if applicable.
761+ */
762+ public function _getUrl (string $ alias , array $ params = []): string
763+ {
764+ return $ this ->router ()->getUrlByAlias ($ alias , $ params );
765+ }
768766}
0 commit comments