forked from mirzaev/minimal
This commit is contained in:
@@ -8,14 +8,18 @@ namespace mirzaev\minimal;
|
||||
use mirzaev\minimal\router,
|
||||
mirzaev\minimal\route,
|
||||
mirzaev\minimal\controller,
|
||||
mirzaev\minimal\model;
|
||||
mirzaev\minimal\model,
|
||||
mirzaev\minimal\http\request,
|
||||
mirzaev\minimal\http\response,
|
||||
mirzaev\minimal\http\enumerations\status;
|
||||
|
||||
// Built-in libraries
|
||||
use exception,
|
||||
BadMethodCallException as exception_method,
|
||||
BadMethodCallException as exception_method,
|
||||
DomainException as exception_domain,
|
||||
InvalidArgumentException as exception_argument,
|
||||
UnexpectedValueException as exception_value,
|
||||
LogicException as exception_logic,
|
||||
ReflectionClass as reflection;
|
||||
|
||||
/**
|
||||
@@ -26,10 +30,11 @@ use exception,
|
||||
* @param model $model An instance of the model
|
||||
* @param router $router An instance of the router
|
||||
*
|
||||
* @mathod self __construct(?string $namespace) Constructor
|
||||
* @mathod void __construct(?string $namespace) Constructor
|
||||
* @method void __destruct() Destructor
|
||||
* @method string|null request(?string $uri, ?string $method, array $variabls) Handle the request
|
||||
* @method string|null route(route $route, string $method) Handle the route
|
||||
* @method string|null start() Initialize request by environment and handle it
|
||||
* @method string|null request(request $request, array $parameters = []) Handle request
|
||||
* @method string|null route(route $route, string $method) Handle route
|
||||
*
|
||||
* @package mirzaev\minimal
|
||||
*
|
||||
@@ -84,7 +89,7 @@ final class core
|
||||
*
|
||||
* @param ?string $namespace Пространство имён системного ядра
|
||||
*
|
||||
* @return self The instance of the core
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(
|
||||
?string $namespace = null
|
||||
@@ -93,30 +98,41 @@ final class core
|
||||
$this->namespace = $namespace ?? (new reflection(self::class))->getNamespaceName();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
*/
|
||||
public function __destruct() {}
|
||||
|
||||
/**
|
||||
* Request
|
||||
* Start
|
||||
*
|
||||
* Handle the request
|
||||
*
|
||||
* @param string|null $uri URI of the request (value by default: $_SERVER['REQUEST_URI'])
|
||||
* @param string|null $method Method of the request (GET, POST, PUT...) (value by default: $_SERVER["REQUEST_METHOD"])
|
||||
* @paam array $parameters parameters for merging with route parameters
|
||||
* Initialize request by environment and handle it
|
||||
*
|
||||
* @return string|null Response
|
||||
*/
|
||||
public function request(?string $uri = null, ?string $method = null, array $parameters = []): ?string
|
||||
public function start(): ?string
|
||||
{
|
||||
// Handle request and exit (success)
|
||||
return $this->request(new request(environment: true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Request
|
||||
*
|
||||
* Handle request
|
||||
*
|
||||
* @param request $request The request
|
||||
* @paam array $parameters parameters for merging with route parameters
|
||||
*
|
||||
* @return string|null Response
|
||||
*/
|
||||
public function request(request $request, array $parameters = []): ?string
|
||||
{
|
||||
// Matching a route
|
||||
$route = $this->router->match($uri ??= $_SERVER['REQUEST_URI'] ?? '/', $method ??= $_SERVER["REQUEST_METHOD"]);
|
||||
$route = $this->router->match($request);
|
||||
|
||||
if ($route) {
|
||||
// Initialized the route
|
||||
// Initialized a route
|
||||
|
||||
if (!empty($parameters)) {
|
||||
// Recaived parameters
|
||||
@@ -125,8 +141,11 @@ final class core
|
||||
$route->parameters = $parameters + $route->parameters;
|
||||
}
|
||||
|
||||
// Handling the route and exit (success)
|
||||
return $this->route($route, $method);
|
||||
// Writing request options from route options
|
||||
$request->options = $route->options;
|
||||
|
||||
// Handling a route and exit (success)
|
||||
return $this->route($route, $request);
|
||||
}
|
||||
|
||||
// Exit (fail)
|
||||
@@ -136,14 +155,18 @@ final class core
|
||||
/**
|
||||
* Route
|
||||
*
|
||||
* Handle the route
|
||||
* Handle route
|
||||
*
|
||||
* @param route $route The route
|
||||
* @param string $method Method of requests (GET, POST, PUT, DELETE, COOKIE...)
|
||||
* @param request $request The request
|
||||
*
|
||||
* @throws exception_domain if failed to find the controller or the model
|
||||
* @throws exception_logic if not received the controller
|
||||
* @throws exception_method if failed to find the method of the controller
|
||||
*
|
||||
* @return string|null Response, if generated
|
||||
*/
|
||||
public function route(route $route, string $method = 'GET'): ?string
|
||||
public function route(route $route, request $request): ?string
|
||||
{
|
||||
// Initializing name of the controller class
|
||||
$controller = $route->controller;
|
||||
@@ -159,12 +182,12 @@ final class core
|
||||
// Not found the controller and $route->controller has a value
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_domain('Failed to found the controller: ' . $route->controller);
|
||||
throw new exception_domain("Failed to find the controller: $controller", status::not_implemented->value);
|
||||
} else {
|
||||
// Not found the controller and $route->controller is empty
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_argument('Failed to initialize the controller: ' . $route->controller);
|
||||
throw new exception_logic('Not received the controller', status::internal_server_error->value);
|
||||
}
|
||||
|
||||
// Deinitializing name of the controller class
|
||||
@@ -184,7 +207,7 @@ final class core
|
||||
// Not found the model and $route->model has a value
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_domain('Failed to initialize the model: ' . ($route->model ?? $route->controller));
|
||||
throw new exception_domain("Failed to find the model: $model", status::not_implemented->value);
|
||||
}
|
||||
|
||||
// Deinitializing name of the model class
|
||||
@@ -197,165 +220,19 @@ final class core
|
||||
$route->controller->model = $route->model;
|
||||
}
|
||||
|
||||
if ($method === 'POST') {
|
||||
// POST
|
||||
// Writing the request to the controller
|
||||
$route->controller->request = $request;
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters + $_POST, $_FILES);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'GET') {
|
||||
// GET
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
if ($_SERVER["CONTENT_TYPE"] === 'multipart/form-data' || $_SERVER["CONTENT_TYPE"] === 'application/x-www-form-urlencoded') {
|
||||
// The requeset content type is the "multipart/form-data" or "application/x-www-form-urlencoded"
|
||||
|
||||
// Analysis of the request
|
||||
[$_GET, $_FILES] = request_parse_body($route->options);
|
||||
}
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters + $_GET, $_FILES);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'PUT') {
|
||||
// PUT
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
if ($_SERVER["CONTENT_TYPE"] === 'multipart/form-data' || $_SERVER["CONTENT_TYPE"] === 'application/x-www-form-urlencoded') {
|
||||
// The requeset content type is the "multipart/form-data" or "application/x-www-form-urlencoded"
|
||||
|
||||
// Analysis of the request
|
||||
[$_PUT, $_FILES] = request_parse_body($route->options);
|
||||
}
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters + $_PUT, $_FILES);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'DELETE') {
|
||||
// DELETE
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
if ($_SERVER["CONTENT_TYPE"] === 'multipart/form-data' || $_SERVER["CONTENT_TYPE"] === 'application/x-www-form-urlencoded') {
|
||||
// The requeset content type is the "multipart/form-data" or "application/x-www-form-urlencoded"
|
||||
|
||||
// Analysis of the request
|
||||
[$_DELETE, $_FILES] = request_parse_body($route->options);
|
||||
}
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters + $_DELETE, $_FILES);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'PATCH') {
|
||||
// PATCH
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
if ($_SERVER["CONTENT_TYPE"] === 'multipart/form-data' || $_SERVER["CONTENT_TYPE"] === 'application/x-www-form-urlencoded') {
|
||||
// The requeset content type is the "multipart/form-data" or "application/x-www-form-urlencoded"
|
||||
|
||||
// Analysis of the request
|
||||
[$_PATCH, $_FILES] = request_parse_body($route->options);
|
||||
}
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters + $_PATCH, $_FILES);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'HEAD') {
|
||||
// HEAD
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'OPTIONS') {
|
||||
// OPTIONS
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'CONNECT') {
|
||||
// CONNECT
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
} else if ($method === 'TRACE') {
|
||||
// TRACE
|
||||
|
||||
if (method_exists($route->controller, $route->method)) {
|
||||
// Found the method of the controller
|
||||
|
||||
// Executing method of the controller that handles the route and exit (success)
|
||||
return $route->controller->{$route->method}($route->parameters);
|
||||
} else {
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception('Failed to find the method of the controller: ' . $route->method);
|
||||
}
|
||||
// Executing method of the controller and exit (success)
|
||||
return $route->controller->{$route->method}(...($route->parameters + $request->parameters));
|
||||
} else {
|
||||
// Not recognized method of the request
|
||||
|
||||
// Not found the method of the controller
|
||||
|
||||
// Exit (fail)
|
||||
throw new exception_value('Failed to recognize the method: ' . $method);
|
||||
throw new exception_method('Failed to find method of the controller: ' . $route->controller::class . "->$route->method()", status::not_implemented->value);
|
||||
}
|
||||
|
||||
// Exit (fail)
|
||||
|
||||
Reference in New Issue
Block a user