6 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
4 changed files with 114 additions and 39 deletions

View File

@@ -22,7 +22,8 @@
"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": { "suggest": {
"mirzaev/baza": "Baza database", "mirzaev/baza": "Baza database",

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
@@ -255,12 +258,21 @@ final class core
// Writing the request options from the route options // Writing the request options from the route options
$request->options = $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) // Processing the method of the controller and exit (success)
$action = function() use ($route, $request): string { $action = function () use ($route, $request, $arguments): string {
try { if (array_keys($arguments) === array_column((new reflection_method($route->controller, $route->method))->getParameters(), 'name')) {
return (string) $route->controller->{$route->method}(...($route->parameters + $route->variables + $request->parameters)); // Arguments match the controller method arguments
} catch (exception $exception) {
return (string) $route->controller->{$route->method}($route->parameters + $route->variables + $request->parameters) // 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);
} }
}; };

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
* *

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