3 Commits

2 changed files with 71 additions and 50 deletions

View File

@@ -128,7 +128,7 @@ final class core
$_SERVER["REQUEST_METHOD"] = $options['method'] ?? 'GET'; $_SERVER["REQUEST_METHOD"] = $options['method'] ?? 'GET';
// Writing URI into the environment constant // Writing URI into the environment constant
$_SERVER['REQUEST_URI'] = $options['uri'] ?? '/'; $_SERVER['REQUEST_URI'] = $options['uri'] ?? '/';
// Writing verstion of HTTP protocol into the environment constant // Writing verstion of HTTP protocol into the environment constant
$_SERVER['SERVER_PROTOCOL'] = $options['protocol'] ?? 'CLI'; $_SERVER['SERVER_PROTOCOL'] = $options['protocol'] ?? 'CLI';
@@ -157,46 +157,14 @@ final class core
// Initialized the route // Initialized the route
if (!empty($parameters)) { if (!empty($parameters)) {
// Recaived parameters // Received parameters
// Merging parameters with the route parameters // Merging parameters with the route parameters
$route->parameters = $parameters + $route->parameters; $route->parameters = $parameters + $route->parameters;
} }
// Preparing the route function
$action = function() use ($request, $route): string {
// Writing the request options from the route options
$request->options = $route->options;
// Preparing the route function
$action = fn(): string => (string) $this->route($route, $request);
foreach ($route->middlewares as $middleware) {
// Iterating over the route middlewares
// Preparing the middleware function
$action = fn(): string => $middleware(next: $action);
}
// Processing middlewares and the route functions
$response = $action();
// Exit (success)
return $response;
};
foreach ($this->router->middlewares as $middleware) {
// Iterating over the router middlewares
// Preparing the middleware function
$action = fn(): string => $middleware(next: $action);
}
// Processing middlewares and the router request function
$response = $action();
// Exit (success) // Exit (success)
return $response; return $this->route($route, $request);
} }
// Exit (fail) // Exit (fail)
@@ -282,8 +250,40 @@ final class core
// Found the method of the controller // Found the method of the controller
try { try {
// Executing method of the controller and exit (success) // Preparing the route function
return $route->controller->{$route->method}(...($route->parameters + $request->parameters)); $action = function () use ($request, $route): string {
// Writing the request options from the route options
$request->options = $route->options;
// Processing the method of the controller and exit (success)
$action = fn(): string => (string) $route->controller->{$route->method}(...($route->parameters + $route->variables + $request->parameters));
foreach ($route->middlewares as $middleware) {
// Iterating over the route middlewares
// Preparing the middleware function
$action = fn(): string => $middleware(next: $action, controller: $route->controller);
}
// Processing middlewares and the route functions
$response = $action();
// Exit (success)
return $response;
};
foreach ($this->router->middlewares as $middleware) {
// Iterating over the router middlewares
// Preparing the middleware function
$action = fn(): string => $middleware(next: $action, controller: $route->controller);
}
// Processing middlewares and the router request function
$response = $action();
// Exit (success)
return $response;
} catch (exception $exception) { } catch (exception $exception) {
// Catched an exception // Catched an exception

View File

@@ -6,10 +6,13 @@ namespace mirzaev\minimal;
// Files of the project // Files of the project
use mirzaev\minimal\http\request, use mirzaev\minimal\http\request,
mirzaev\minimal\controller,
mirzaev\minimal\route; mirzaev\minimal\route;
// Built-in libraries // Built-in libraries
use Closure as closure; use Closure as closure,
LogicException as exception_logic
;
/** /**
* Middleware * Middleware
@@ -25,14 +28,14 @@ use Closure as closure;
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/ */
final class middleware class middleware
{ {
/** /**
* Function * Function
* *
* @var closure $function Function * @var closure|array $function Function
*/ */
public readonly closure $function; public readonly closure|array $function;
/** /**
* Constructor * Constructor
@@ -41,23 +44,41 @@ final class middleware
* *
* @return void * @return void
*/ */
public function __construct(closure $function) public function __construct(?closure $function = null)
{ {
// Writing the function if (static::class === self::class) {
$this->function = $function; // 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);
}
}
} }
/** /**
* Invoke * Invoke
* *
* @param callable $next * @param callable $next
* @param controller $controller
* *
* @return string Output * @return string Output
*/ */
public function __invoke(callable $next): string public function __invoke(callable $next, controller $controller): string
{ {
// Processing the middleware (entering into recursion) // Processing the middleware (entering into recursion)
return (string) ($this->function)(next: $next, controller: $controller);
return (string) ($this->function)(next: $next); }
}
} }