19 Commits

Author SHA1 Message Date
ebd793a2d3 core arguments null, router 404 error controller 2026-04-27 11:21:22 +05:00
ec370baa45 fixed virtual property error 2026-04-17 11:08:04 +05:00
aca9694723 get hook fixed dolboyoub 2026-04-17 11:03:30 +05:00
23d7fc72a9 smartphone detection 2026-04-17 10:58:13 +05:00
38a0af6b86 a litle bit reflection in the router, sorry 2026-04-12 18:02:02 +05:00
17c15b0860 fixed controller arguments fixing 2026-04-09 07:33:44 +05:00
b7eda7a944 controller arguments fixed 2026-04-08 15:03:49 +05:00
1123b75d9e fix magix static::class 2026-03-17 17:43:36 +05:00
6d1f1b79ba magic methods fixed 2026-03-09 09:10:11 +05:00
393f37577d json POST $variables fix 2025-12-02 18:58:04 +03:00
584285b92c fix #b61599aac9 merge 2025-11-04 12:27:54 +03:00
ed2a41a139 suggest mirzaev/files 2025-11-04 02:42:27 +07:00
89c6d0c814 suggest mirzaev/languages and mirzaev/currencies 2025-11-04 02:24:05 +07:00
67a2ea3e1d suggest mirzaev/pot 2025-11-04 02:16:40 +07:00
cd4a7912d0 suggest mirzaev/baza 2025-11-04 02:14:39 +07:00
42774c8830 merge with 3.7.3 2025-11-03 15:05:03 +03:00
b61599aac9 gang banged middlewares 2025-11-03 15:01:26 +03:00
234691f011 mego dibil 2025-10-09 23:03:36 +07:00
69702888d1 middlewares fixed govno 2025-10-09 22:45:06 +07:00
9 changed files with 229 additions and 2414 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
vendor vendor
composer.lock

View File

@@ -14,7 +14,7 @@
"name": "Arsen Mirzaev Tatyano-Muradovich", "name": "Arsen Mirzaev Tatyano-Muradovich",
"email": "arsen@mirzaev.sexy", "email": "arsen@mirzaev.sexy",
"homepage": "https://mirzaev.sexy", "homepage": "https://mirzaev.sexy",
"role": "Programmer" "role": "Creator"
} }
], ],
"support": { "support": {
@@ -22,8 +22,16 @@
"issues": "https://git.svoboda.works/mirzaev/minimal/issues" "issues": "https://git.svoboda.works/mirzaev/minimal/issues"
}, },
"require": { "require": {
"php": "~8.4" "php": "~8.4",
"mobiledetect/mobiledetectlib": "^4.8"
}, },
"suggest": {
"mirzaev/baza": "Baza database",
"mirzaev/pot": "Template for projects",
"mirzaev/files": "Easy working with files",
"mirzaev/languages": "Easy languages integration",
"mirzaev/currencies": "Easy currencies integration"
},
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"mirzaev\\minimal\\": "mirzaev/minimal/system" "mirzaev\\minimal\\": "mirzaev/minimal/system"

2320
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -22,7 +22,10 @@ use Closure as closure,
InvalidArgumentException as exception_argument, InvalidArgumentException as exception_argument,
UnexpectedValueException as exception_value, UnexpectedValueException as exception_value,
LogicException as exception_logic, LogicException as exception_logic,
ReflectionClass as reflection; Error as error,
ArgumentCountError as error_argument_count,
ReflectionClass as reflection,
ReflectionMethod as reflection_method;
/** /**
* Core * Core
@@ -34,7 +37,7 @@ use Closure as closure,
* @param model $model An instance of the model * @param model $model An instance of the model
* @param router $router An instance of the router * @param router $router An instance of the router
* *
* @mathod void __construct(?string $namespace) Constructor * @method void __construct(?string $namespace) Constructor
* @method void __destruct() Destructor * @method void __destruct() Destructor
* @method string|null start() Initialize request by environment and handle it * @method string|null start() Initialize request by environment and handle it
* @method string|null request(request $request, array $parameters = []) Handle request * @method string|null request(request $request, array $parameters = []) Handle request
@@ -128,27 +131,14 @@ 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';
} }
// Preparing the route function // Processing the request and exit (success)
$action = fn(): string => (string) $this->request(new request(environment: true)); return $this->request(new request(environment: true));
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)
return $response;
} }
/** /**
@@ -170,30 +160,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;
} }
// 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) // Exit (success)
return $response; return $this->route($route, $request);
} }
// Exit (fail) // Exit (fail)
@@ -279,8 +253,55 @@ 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;
// Initializing the controller method arguments
$arguments = $route->parameters + $route->variables + $request->parameters;
// Processing the method of the controller and exit (success)
$action = function () use ($route, $request, $arguments): string {
if (array_keys($arguments) === array_column((new reflection_method($route->controller, $route->method))->getParameters(), 'name')) {
// Arguments match the controller method arguments
// Exit (success)
return (string) $route->controller->{$route->method}(...$arguments);
} else {
// Arguments not match the controller method arguments
// Exit (success)
return (string) $route->controller->{$route->method}($arguments ? $arguments : null);
}
};
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

@@ -11,6 +11,9 @@ use mirzaev\minimal\http\enumerations\method,
mirzaev\minimal\http\enumerations\content, mirzaev\minimal\http\enumerations\content,
mirzaev\minimal\http\response; mirzaev\minimal\http\response;
// The smartphones detection library
use Detection\MobileDetect as mobile;
// Built-in libraries // Built-in libraries
use DomainException as exception_domain, use DomainException as exception_domain,
InvalidArgumentException as exception_argument, InvalidArgumentException as exception_argument,
@@ -297,6 +300,50 @@ final class request
get => $this->options ?? []; get => $this->options ?? [];
} }
/**
* Smartphone
*
* @see https://docs.mobiledetect.net/home/usage-composer Documentation
*
* @var bool $smartphone The request was sent from a smartphone?
*/
public bool $smartphone {
// Read
get {
if (!isset($this->smartphone)) {
// The property is not initialized
// Writing into the property
$this->smartphone = new mobile()->isMobile();
}
// Exit (success)
return $this->smartphone;
}
}
/**
* Tablet
*
* @see https://docs.mobiledetect.net/home/usage-composer Documentation
*
* @var bool $tablet The request was sent from a tablet?
*/
public bool $tablet {
// Read
get {
if (!isset($this->tablet)) {
// The property is not initialized
// Writing into the property
$this->tablet = new mobile()->isTablet();
}
// Exit (success)
return $this->tablet;
}
}
/** /**
* Constructor * Constructor
* *
@@ -408,9 +455,6 @@ final class request
// Exit (false) // Exit (false)
throw new exception_argument('Failed to validate JSON from the input buffer', status::unprocessable_content->value); throw new exception_argument('Failed to validate JSON from the input buffer', status::unprocessable_content->value);
} }
// Writing parameters from environment into the property
$this->parameters = $_POST ?? [];
} else if ($this->method === method::post) { } else if ($this->method === method::post) {
// POST method // POST method

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); }
}
} }

View File

@@ -114,6 +114,18 @@ final class route
get => $this->options ?? []; get => $this->options ?? [];
} }
/**
* Parameters
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var array $parameters Arguments for the $this->method (will be concatenated together with generated request parameters)
*/
public array $variables = [] {
// Read
&get => $this->variables;
}
/** /**
* Constructor * Constructor
* *

View File

@@ -129,6 +129,7 @@ final class router
// Skipping unmatched routes based on results of previous iterations // Skipping unmatched routes based on results of previous iterations
if (isset($matches[$route]) && $matches[$route] === false) continue; if (isset($matches[$route]) && $matches[$route] === false) continue;
// Initializing of route directory // Initializing of route directory
$route_directory = $routes[$route][$i] ?? null; $route_directory = $routes[$route][$i] ?? null;
@@ -161,56 +162,70 @@ final class router
} }
} }
// Finding a priority route from match results if (array_all($matches, fn($value) => $value === false)) {
foreach ($matches as $route => $match) if ($match !== false) break; // Not found any route
if ($route && !empty($data = $this->routes[$route])) { if (false) {
// Route found // Initialized the errors controller
// Universalization of route // Processing the `404` error method
$route = self::universalize($route);
/** }
* Initialization of route variables } else {
*/ // Found At least one route
foreach ($routes[$route] as $i => $route_directory) { // Finding a priority route from all match results (setting the $route variable)
// Iteration over directories of route foreach ($matches as $route => $match) if ($match !== false) break;
unset($match);
if (preg_match('/^\$([a-zA-Z_\x80-\xff]+)$/', $route_directory) === 1) { if ($route && !empty($data = $this->routes[$route])) {
// The directory is a variable ($variable) // The route found
// Запись в реестр переменных и перещапись директории в маршруте // Universalization of the route
$data[$request->method->value]->variables[trim($route_directory, '$')] = $directories[$i]; $route = self::universalize($route);
} else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1) {
// The directory of route is a collector ($variable...)
// Инициализаия ссылки на массив сборщика /**
$collector = &$data[$request->method->value]->variables[trim($route_directory, '$.')]; * Initialization of the route variables
*/
// Инициализаия массива сборщика foreach ($routes[$route] as $i => $route_directory) {
$collector ??= []; // Iteration over directories of route
// Запись в реестр переменных if (preg_match('/^\$([a-zA-Z_\x80-\xff]+)$/', $route_directory) === 1) {
$collector[] = $directories[$i]; // The directory is a variable ($variable)
foreach (array_slice($directories, ++$i, preserve_keys: true) as &$urn_directory) { // Запись в реестр переменных и перезапись директории в маршруте
// Перебор директорий URN $data[$request->method->value]->variables[trim($route_directory, '$')] = $directories[$i];
} else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1) {
// The directory of route is a collector ($variable...)
// Инициализаия ссылки на массив сборщика
$collector = &$data[$request->method->value]->variables[trim($route_directory, '$.')];
// Инициализаия массива сборщика
$collector ??= [];
// Запись в реестр переменных // Запись в реестр переменных
$collector[] = $urn_directory; $collector[] = $directories[$i];
foreach (array_slice($directories, ++$i, preserve_keys: true) as &$urn_directory) {
// Перебор директорий URN
// Запись в реестр переменных
$collector[] = $urn_directory;
}
break;
} }
break;
} }
}
// Exit (success or fail) // Exit (success or fail)
return $data[$request->method->value] ?? null; return $data[$request->method->value] ?? null;
}
} }
} }
// Exit (fail) // Exit (success/fail)
return null; return null;
} }

View File

@@ -8,7 +8,8 @@ namespace mirzaev\minimal\traits;
use mirzaev\minimal\http\enumerations\status; use mirzaev\minimal\http\enumerations\status;
// Built-in libraries // Built-in libraries
use exception; use Exception as exception,
DomainException as exception_domain;
/** /**
* Trait of magical methods * Trait of magical methods
@@ -28,6 +29,8 @@ trait magic
/** /**
* Write property * Write property
* *
* @throws exception_domain Not found the proprty
*
* @param string $name Name of the property * @param string $name Name of the property
* @param mixed $value Value of the property * @param mixed $value Value of the property
* *
@@ -35,14 +38,24 @@ trait magic
*/ */
public function __set(string $name, mixed $value = null): void public function __set(string $name, mixed $value = null): void
{ {
match ($name) { if (property_exists(static::static, $name)) {
default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value) // Exist the property
};
// Writing the property
$this->{$name} = $value;
} else {
// Not exist the property
// Exit (fail)
throw new exception_domain('Not found the property: ' . static::class . "::\$$name", status::not_found->value);
}
} }
/** /**
* Read property * Read property
* *
* @throws exception_domain Not found the property
*
* @param string $name Name of the property * @param string $name Name of the property
* *
* @return mixed Value of the property * @return mixed Value of the property
@@ -50,7 +63,7 @@ trait magic
public function __get(string $name): mixed public function __get(string $name): mixed
{ {
return match ($name) { return match ($name) {
default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value) default => throw new exception_domain('Not found the property: ' . static::class . "::\$$name", status::not_found->value)
}; };
} }