From b61599aac927f6ccfb5f441d2f035578e889357d Mon Sep 17 00:00:00 2001 From: Arsen Mirzaev Tatyano-Muradovich Date: Mon, 3 Nov 2025 15:01:26 +0300 Subject: [PATCH] gang banged middlewares --- mirzaev/minimal/system/middleware.php | 44 +++++++++++++++++++-------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/mirzaev/minimal/system/middleware.php b/mirzaev/minimal/system/middleware.php index 23a338c..b907057 100755 --- a/mirzaev/minimal/system/middleware.php +++ b/mirzaev/minimal/system/middleware.php @@ -10,7 +10,9 @@ use mirzaev\minimal\http\request, mirzaev\minimal\route; // Built-in libraries -use Closure as closure; +use Closure as closure, + LogicException as exception_logic + ; /** * Middleware @@ -26,14 +28,14 @@ use Closure as closure; * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @author Arsen Mirzaev Tatyano-Muradovich */ -final class middleware +class middleware { /** * Function * - * @var closure $function Function + * @var closure|array $function Function */ - public readonly closure $function; + public readonly closure|array $function; /** * Constructor @@ -42,10 +44,28 @@ final class middleware * * @return void */ - public function __construct(closure $function) + public function __construct(?closure $function = null) { - // Writing the function - $this->function = $function; + if (static::class === self::class) { + // The middleware class itself + + // Writing the function + $this->function = $function; + } else { + // The middleware inheriting class + + if (method_exists($this, 'middleware')) { + // Found the method + + // Writing the function + $this->function = [$this, 'middleware']; + } else { + // Not found the method + + // Exit (fail) + throw new exception_logic('The middleware method is not initialized', 500); + } + } } /** @@ -56,9 +76,9 @@ final class middleware * * @return string Output */ - public function __invoke(callable $next, controller $controller): string - { - // Processing the middleware (entering into recursion) - return (string) ($this->function)(next: $next, controller: $controller); - } + public function __invoke(callable $next, controller $controller): string + { + // Processing the middleware (entering into recursion) + return (string) ($this->function)(next: $next, controller: $controller); + } }