Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ec370baa45 | |||
| aca9694723 | |||
| 23d7fc72a9 | |||
| 38a0af6b86 | |||
| 17c15b0860 | |||
| b7eda7a944 | |||
| 1123b75d9e | |||
| 6d1f1b79ba | |||
| 393f37577d | |||
| 584285b92c | |||
| ed2a41a139 | |||
| 89c6d0c814 | |||
| 67a2ea3e1d |
@@ -22,10 +22,15 @@
|
|||||||
"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",
|
||||||
|
"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": {
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -255,8 +258,23 @@ 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 = fn(): string => (string) $route->controller->{$route->method}(...($route->parameters + $route->variables + $request->parameters));
|
$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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
foreach ($route->middlewares as $middleware) {
|
foreach ($route->middlewares as $middleware) {
|
||||||
// Iterating over the route middlewares
|
// Iterating over the route middlewares
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -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)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user