Compare commits

..

5 Commits

16 changed files with 1983 additions and 338 deletions

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

View File

@ -1,7 +1,7 @@
{ {
"name": "mirzaev/minimal", "name": "mirzaev/minimal",
"type": "framework", "type": "framework",
"description": "Lightweight MVC framework that manages only the basic mechanisms, leaving the development of the programmer and not overloading the project", "description": "My vision of a good framework",
"keywords": [ "keywords": [
"mvc", "mvc",
"framework", "framework",
@ -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": "Developer" "role": "Programmer"
} }
], ],
"support": { "support": {
@ -22,16 +22,11 @@
"issues": "https://git.mirzaev.sexy/mirzaev/minimal/issues" "issues": "https://git.mirzaev.sexy/mirzaev/minimal/issues"
}, },
"require": { "require": {
"php": "~8.2" "php": "~8.4"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"mirzaev\\minimal\\": "mirzaev/minimal/system" "mirzaev\\minimal\\": "mirzaev/minimal/system"
} }
},
"autoload-dev": {
"psr-4": {
"mirzaev\\minimal\\tests\\": "mirzaev/minimal/tests"
}
} }
} }

View File

@ -6,16 +6,27 @@ namespace mirzaev\minimal;
// Files of the project // Files of the project
use mirzaev\minimal\model, use mirzaev\minimal\model,
mirzaev\minimal\traits\magic; mirzaev\minimal\core,
mirzaev\minimal\traits\magic,
mirzaev\minimal\http\request,
mirzaev\minimal\http\enumerations\status;
// Встроенные библиотеки // Built-in libraries
use exception; use exception,
RuntimeException as exception_runtime;
/** /**
* Controller (base) * Controller
* *
* @package mirzaev\minimal * @package mirzaev\minimal
* *
* @var core $core An instance of the core
* @var request $request Request
* @var model $model An instance of the model connected in the core
* @var view $view View template engine instance (twig)
*
* @method void __construct(core $core) Constructor
*
* @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>
*/ */
@ -24,71 +35,84 @@ class controller
use magic; use magic;
/** /**
* Postfix of file names * Core
*
* @var core $core An instance of the core
*/ */
public const string POSTFIX = '_controller'; public core $core {
// Read
get => $this->core;
}
/** /**
* Instance of the model connected in the router * Request
*
* @var request $request Request
*/ */
protected model $model; public request $request {
// Read
get => $this->request;
}
/** /**
* View template engine instance (twig) * Model
*
* @throws exception_runtime if reinitialize the property
* @throws exception_runtime if an attempt to write null
*
* @var model $model An instance of the model connected in the core
*/ */
protected object $view; public ?model $model = null {
// Write
set (model|null $model) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
if ($model instanceof model) {
// Validated model
// Writing
$this->model = $model;
} else {
// Not validated model
// Exit (fail)
throw new exception_runtime('The property must be an instance of model', status::internal_server_error->value);
}
}
// Read
get => $this->model;
}
/**
* View
*
* @var view $view View template engine instance (twig)
*/
public object $view {
// Write
set (object $view) {
$this->view ??= $view;
}
// Read
get => $this->view;
}
/** /**
* Constructor * Constructor
*/
public function __construct() {}
/**
* Write property
* *
* @param string $name Name of the property * @param core $core The instance of the core
* @param mixed $value Value of the property
* *
* @return void * @return void
*/ */
public function __set(string $name, mixed $value = null): void public function __construct(core $core) {
{ // Writing the core into the property
match ($name) { $this->core = $core;
'model' => (function () use ($value) {
if ($this->__isset('model')) throw new exception('Can not reinitialize property: ' . static::class . '::$model', 500);
else {
// Property not initialized
if (is_object($value)) $this->model = $value;
else throw new exception('Property "' . static::class . '::view" should store an instance of a model', 500);
}
})(),
'view' => (function () use ($value) {
if ($this->__isset('view')) throw new exception('Can not reinitialize property: ' . static::class . '::$view', 500);
else {
// Property not initialized
if (is_object($value)) $this->view = $value;
else throw new exception('Property "' . static::class . '::view" should store an instance of a view template engine', 500);
}
})(),
default => throw new exception('Property "' . static::class . "::\$$name\" not found", 404)
};
}
/**
* Read property
*
* @param string $name Name of the property
*
* @return mixed Value of the property
*/
public function __get(string $name): mixed
{
return match ($name) {
'model' => $this->model ?? throw new exception('Property "' . static::class . '::$model" is not initialized', 500),
'view' => $this->view ?? throw new exception('Property "' . static::class . '::$view" is not initialized', 500),
default => throw new exception('Property "' . static::class . "::\$$name\" not found", 404)
};
} }
} }

View File

@ -4,13 +4,23 @@ declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
// Файлы проекта // Files of the project
use mirzaev\minimal\router, use mirzaev\minimal\router,
mirzaev\minimal\route,
mirzaev\minimal\controller, 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, use exception,
RuntimeException as exception_runtime,
BadMethodCallException as exception_method,
DomainException as exception_domain,
InvalidArgumentException as exception_argument,
UnexpectedValueException as exception_value,
LogicException as exception_logic,
ReflectionClass as reflection; ReflectionClass as reflection;
/** /**
@ -18,210 +28,227 @@ use exception,
* *
* @package mirzaev\minimal * @package mirzaev\minimal
* *
* @param string $namespace Namespace where the core was initialized from
* @param controller $controller An instance of the controller
* @param model $model An instance of the model
* @param router $router An instance of the router
*
* @mathod void __construct(?string $namespace) Constructor
* @method void __destruct() Destructor
* @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
*
* @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 core final class core
{ {
/** /**
* Инстанция соединения с базой данных * Namespace
*/
private object $db;
/**
* Инстанция маршрутизатора
*/
private readonly router $router;
/**
* Инстанция ядра контроллера
*/
private readonly controller $controller;
/**
* Инстанция ядра модели
*/
private readonly model $model;
/**
* Путь пространства имён (системное)
* *
* Используется для поиска файлов по спецификации PSR-4 * @var string $namespace Namespace where the core was initialized from
*/
private readonly string $namespace;
/**
* Конструктор
* *
* @param ?object $db Инстанция соединения с базой данных * @see https://www.php-fig.org/psr/psr-4/
* @param ?router $router Маршрутизатор
* @param ?controller $controller Инстанция ядра контроллера
* @param ?model $model Инстанция ядра модели
* @param ?string $namespace Пространство имён системного ядра
*
* @return self Инстанция ядра
*/ */
public function __construct( public string $namespace {
?object $db = null, // Read
?router $router = null, get => $this->namespace;
?controller $controller = null,
?model $model = null,
?string $namespace = null
) {
// Инициализация свойств
if (isset($db)) $this->__set('db', $db);
if (isset($router)) $this->__set('router', $router);
if (isset($controller)) $this->__set('controller', $controller);
if (isset($model)) $this->__set('model', $model);
$this->__set('namespace', $namespace ?? (new reflection(self::class))->getNamespaceName());
} }
/** /**
* Деструктор * Controller
* *
* @var controller $controller An instance of the controller
*/
private controller $controller {
// Read
get => $this->controller ??= new controller;
}
/**
* Model
*
* @var model $model An instance of the model
*/
private model $model {
// Read
get => $this->model ??= new model;
}
/**
* Router
*
* @var router $router An instance of the router
*/
public router $router {
get => $this->router ??= router::initialize();
}
/**
* Constrictor
*
* @param ?string $namespace Пространство имён системного ядра
*
* @return void
*/
public function __construct(
?string $namespace = null
) {
// Writing a namespace to the property
$this->namespace = $namespace ?? (new reflection(self::class))->getNamespaceName();
}
/**
* Destructor
*/ */
public function __destruct() {} public function __destruct() {}
/** /**
* Запуск * Start
* *
* @param ?string $uri Маршрут * Initialize request by environment and handle it
* *
* @return string|int|null Ответ * @return string|null Response
*/ */
public function start(string $uri = null): string|int|null public function start(): ?string
{ {
// Обработка запроса // Handle request and exit (success)
return $this->__get('router')->handle($uri, core: $this); return $this->request(new request(environment: true));
} }
/** /**
* Записать свойство * Request
* *
* @param string $name Название * Handle request
* @param mixed $value Содержимое
* *
* @return void * @param request $request The request
* @paam array $parameters parameters for merging with route parameters
*
* @return string|null Response
*/ */
public function __set(string $name, mixed $value = null): void public function request(request $request, array $parameters = []): ?string
{ {
match ($name) { // Matching a route
'db', 'database' => (function () use ($value) { $route = $this->router->match($request);
if ($this->__isset('db')) throw new exception('Запрещено реинициализировать инстанцию соединения с базой данных ($this->db)', 500);
else {
// Свойство ещё не было инициализировано
if (is_object($value)) $this->db = $value; if ($route) {
else throw new exception('Свойство $this->db должно хранить инстанцию соединения с базой данных', 500); // Initialized a route
}
})(),
'router' => (function () use ($value) {
if ($this->__isset('router')) throw new exception('Запрещено реинициализировать инстанцию маршрутизатора ($this->router)', 500);
else {
// Свойство ещё не было инициализировано
if ($value instanceof router) $this->router = $value; if (!empty($parameters)) {
else throw new exception('Свойство $this->router должно хранить инстанцию маршрутизатора (mirzaev\minimal\router)"', 500); // Recaived parameters
}
})(),
'controller' => (function () use ($value) {
if ($this->__isset('controller')) throw new exception('Запрещено реинициализировать инстанцию ядра контроллеров ($this->controller)', 500);
else {
// Свойство не инициализировано
if ($value instanceof controller) $this->controller = $value; // Merging parameters with route parameters
else throw new exception('Свойство $this->controller должно хранить инстанцию ядра контроллеров (mirzaev\minimal\controller)', 500); $route->parameters = $parameters + $route->parameters;
} }
})(),
'model' => (function () use ($value) {
if ($this->__isset('model')) throw new exception('Запрещено реинициализировать инстанцию ядра моделей ($this->model)', 500);
else {
// Свойство не инициализировано
if ($value instanceof model) $this->model = $value; // Writing request options from route options
else throw new exception('Свойство $this->model должно хранить инстанцию ядра моделей (mirzaev\minimal\model)', 500); $request->options = $route->options;
}
})(),
'namespace' => (function () use ($value) {
if ($this->__isset('namespace')) throw new exception('Запрещено реинициализировать путь пространства имён ($this->namespace)', 500);
else {
// Свойство не инициализировано
if (is_string($value)) $this->namespace = $value; // Handling a route and exit (success)
else throw new exception('Свойство $this->namespace должно хранить строку с путём пространства имён', 500); return $this->route($route, $request);
} }
})(),
default => throw new exception("Свойство \"\$$name\" не найдено", 404) // Exit (fail)
}; return null;
} }
/** /**
* Прочитать свойство * Route
* *
* Записывает значение по умолчанию, если свойство не инициализировано * Handle route
* *
* @param string $name Название * @param route $route The route
* @param request $request The request
* *
* @return mixed Содержимое * @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 __get(string $name): mixed public function route(route $route, request $request): ?string
{ {
return match ($name) { // Initializing name of the controller class
'db', 'database' => $this->db ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500), $controller = $route->controller;
'router' => (function () {
// Инициализация со значением по умолчанию
if (!$this->__isset('router')) $this->__set('router', new router);
// Возврат (успех) if ($route->controller instanceof controller) {
return $this->router; // Initialized the controller
})(), } else if (class_exists($controller = "$this->namespace\\controllers\\$controller")) {
'controller' => (function () { // Found the controller by its name
// Инициализация со значением по умолчанию
if (!$this->__isset('controller')) $this->__set('controller', new controller);
// Возврат (успех) // Initializing the controller
return $this->controller; $route->controller = new $controller(core: $this);
})(), } else if (!empty($route->controller)) {
'model' => (function () { // Not found the controller and $route->controller has a value
// Инициализация со значением по умолчанию
if (!$this->__isset('model')) $this->__set('model', new model);
// Возврат (успех) // Exit (fail)
return $this->model; throw new exception_domain("Failed to find the controller: $controller", status::not_implemented->value);
})(), } else {
'namespace' => $this->namespace ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500), // Not found the controller and $route->controller is empty
default => throw new exception("Свойство \"\$$name\" не найдено", 404)
}; // Exit (fail)
throw new exception_logic('Not received the controller', status::internal_server_error->value);
} }
/** // Deinitializing name of the controller class
* Проверить свойство на инициализированность unset($controller);
*
* @param string $name Название if (!isset($route->controller->model)) {
* // Not initialized the model in the controller
* @return bool Инициализировано свойство?
*/ // Initializing name if the model class
public function __isset(string $name): bool $model = $route->model;
{
return match ($name) { if ($route->model instanceof model) {
default => isset($this->{$name}) // Initialized the model
}; } else if (class_exists($model = "$this->namespace\\models\\$model")) {
// Found the model by its name
// Initializing the model
$route->model = new $model;
} else if (!empty($route->model)) {
// Not found the model and $route->model has a value
// Exit (fail)
throw new exception_domain("Failed to find the model: $model", status::not_implemented->value);
} }
/** // Deinitializing name of the model class
* Удалить свойство unset($model);
*
* @param string $name Название if ($route->model instanceof model) {
* // Initialized the model
* @return void
*/ // Writing the model to the controller
public function __unset(string $name): void $route->controller->model = $route->model;
{ }
match ($name) { }
default => (function () use ($name) {
// Удаление // Writing the request to the controller
unset($this->{$name}); $route->controller->request = $request;
})()
}; if (method_exists($route->controller, $route->method)) {
// Found the method of the controller
try {
// Executing method of the controller and exit (success)
return $route->controller->{$route->method}(...($route->parameters + $request->parameters));
} catch (exception $exception) {
// Catched an exception
// Exit (fail)
throw new exception_runtime('Caught an error while processing the route', status::internal_server_error->value, $exception);
}
} else {
// Not found the method of the controller
// Exit (fail)
throw new exception_method('Failed to find method of the controller: ' . $route->controller::class . "->$route->method()", status::not_implemented->value);
}
// Exit (fail)
return null;
} }
} }

View File

@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http\enumerations;
// Files of the project
use mirzaev\minimal\http\enumerations\status;
// Built-in libraries
use InvalidArgumentException as exception_argument,
DomainException as exception_domain;
/**
* Content
*
* Implementation of "Content-Type" header
*
* @package mirzaev\minimal\http\enumerations
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
enum content: string
{
case any = '*/*';
// Text
case txt = 'text/plain';
case css = 'text/css';
case csv = 'text/csv';
case html = 'text/html';
case js = 'text/javascript'; // js + mjs (https://www.rfc-editor.org/rfc/rfc9239#name-text-javascript)
// Applications
case binary = 'application/octet-stream';
case encoded = 'application/x-www-form-urlencoded';
case json = 'application/json';
case rdf = 'application/ld+json';
case xml = 'application/xml';
case ogx = 'application/ogg';
case pdf = 'application/pdf';
case xls = 'application/vnd.ms-excel';
case xlsx = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
case tar = 'application/x-tar';
case zip = 'application/zip';
case zip7 = 'application/x-7z-compressed';
case rar = 'application/vnd.rar';
case jar = 'application/java-archive';
case odp = 'application/vnd.oasis.opendocument.presentation';
case ods = 'application/vnd.oasis.opendocument.spreadsheet';
case odt = 'application/vnd.oasis.opendocument.text';
case php = 'application/x-httpd-php';
case sh = 'application/x-sh';
case xhtml = 'application/xhtml+xml';
// Audio
case aac = 'audio/aac';
case mp3 = 'audio/mpeg';
case wav = 'audio/wav';
case oga = 'audio/ogg';
case weba = 'audio/webm';
// Images
case gif = 'image/gif';
case jpeg = 'image/jpeg';
case png = 'image/png';
case apng = 'image/apng';
case tiff = 'image/tiff';
case svg = ' image/svg+xml';
case webp = 'image/webp';
case avif = 'image/avif';
case bmp = 'image/bmp';
case ico = 'image/vnd.microsoft.icon';
// Videos
case avi = 'video/x-msvideo';
case mp4 = 'video/mp4';
case mpeg = 'video/mpeg';
case ogv = 'video/ogg';
case ts = 'video/mp2t';
// Fonts
case otf = 'font/otf';
case ttf = 'font/ttf';
case woff = 'font/woff';
case woff2 = 'font/woff2';
// Multipart
case form = 'multipart/form-data';
case mixed = 'multipart/mixed';
case alternative = 'multipart/alternative';
case related = 'multipart/related';
/**
* Extension
*
* Returns the file extension without a dot
*
* @throws exception_argument if content can not have file extension
* @throws exception_domain if failed to recognize content
*
* @return string File extension
*/
public function extension(): string
{
// Exit (success)
return match ($this) {
self::jpeg => 'jpg',
self::png => 'png',
self::form, self::mixed, self::alternative, self::related => throw new exception_argument('Content can not have file extension', status::internal_server_error->value),
default => throw new exception_domain('Failed to recognize content: ' . $this->value, status::not_found->value)
};
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http\enumerations;
/**
* Method
*
* Methods of HTTP request
*
* @package mirzaev\minimal\http\enumerations
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
enum method: string
{
case post = 'POST';
case get = 'GET';
case put = 'PUT';
case delete = 'DELETE';
case patch = 'PATCH';
case head = 'HEAD';
case options = 'OPTIONS';
case connect = 'CONNECT';
case trace = 'TRACE';
/**
* Body
*
* @return bool Request with this method may has body?
*/
public function body(): bool
{
// Exit (success)
return match ($this) {
self::post, self::put, self::delete, self::patch => true,
default => false
};
}
}

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http\enumerations;
/**
* Protocol
*
* Versions of HTTP
*
* @package mirzaev\minimal\http\enumerations
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
enum protocol: string
{
case http_3 = 'HTTP/3.0';
case http_2 = 'HTTP/2.0';
case http_1_1 = 'HTTP/1.1';
case http_1 = 'hTTP/1.0';
case http_0_9 = 'HTTP/0.9';
}

View File

@ -0,0 +1,241 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http\enumerations;
// Built-in libraries
use DomainException as exception_domain;
/**
* Status
*
* Status codes and status texts of HTTP response
*
* @package mirzaev\minimal\http\enumerations
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
enum status: int
{
// 1XX
case continue = 100;
case switching_protocols = 101;
case processing = 102;
case early_hints = 103;
case among_us = 112;
// 2XX
case ok = 200; // ok
case created = 201;
case accepted = 202;
case non_authoritative_information = 203;
case no_content = 204;
case reset_content = 205;
case partial_content = 206;
case multi_status = 207;
case already_reported = 208;
case im_used = 226; // bruh
// 3XX
case multiple_choises = 300;
case moved_permanently = 301;
case found = 302; // Previously "Moved temporarily"
case see_other = 303;
case not_modified = 304;
case use_proxy = 305;
case switch_proxy = 306;
case temporary_redirect = 307;
case permanent_redirect = 308;
// 4XX
case bad_request = 400;
case unauthorized = 401;
case payment_required = 402; // Are you fucking commerce?
case forbidden = 403;
case not_found = 404; // our celebrity
case method_not_allowed = 405;
case not_acceptable = 406;
case proxy_authentication_required = 407;
case request_timeout = 408;
case conflict = 409;
case gone = 410;
case length_required = 411;
case precondition_failed = 412;
case payload_too_large = 413;
case uri_too_long = 414;
case unsupported_media_type = 415;
case range_not_satisfiable = 416;
case expectation_failed = 417;
case i_am_a_teapot = 418;
case misdirected_request = 421;
case unprocessable_content = 422;
case locked = 423;
case failed_dependency = 424;
case too_early = 425;
case upgrade_required = 426;
case precondition_required = 428;
case too_many_requests = 429;
case request_header_fields_too_large = 431;
case unavailable_for_legal_reasons = 451; // @see self::failed_state
case bruh = 441;
// 5XX
case internal_server_error = 500;
case not_implemented = 501;
case bad_gateway = 502;
case service_unawaiable = 503;
case gateway_timeout = 504;
case http_version_not_supported = 505;
case variant_also_negotiates = 506;
case insufficient_storage = 507;
case loop_detected = 508;
case not_extended = 510;
case network_authentication_required = 511;
// 9XX
case failed_state = 911;
/**
* Label
*
* The result will be in ucwords() format - first character uppercase, rest lowercase
*
* You might want to do strtoupper() - convert all characters to uppercase
* "HTTP/2 200 Ok" after strtoupper() will be "HTTP/2 200 OK"
*
* It is common for "OK" to have both characters uppercase,
* and for the other status texts only the first letter of each word uppercase.
* This is universalized here, so the result will be "Ok" instead of "OK".
*
* If you want to get "OK" without using strtoupper(),
* just use the literal "OK" instead of self::ok->label()
*
* The uppercase letter on each word makes it easier and faster to read the status text,
* even if it violates the grammar rules we are accustomed to
*
* I also indignantly conducted a test and tried to use "Early hints" instead of "Early Hints",
* as well as "Already reported" instead of "Already Reported".
* The result of my tests was that the readability of such status texts is greatly reduced.
*
* Also note the following:
* 1. "Non-Authoritative Information" -> "Non Authoritative Information"
* 2. "Multi-Status" -> "Multi Status"
* 3. "IM Used" -> "I Am Used"
* 4. "I`m a teapot" -> "I Am A Teapot" (Initially, i wanted to leave it as in honor of tradition,
* but i decided that this is, first of all, one of our current and working status codes to this day,
* so it should also be universalized.
* 5. "URI" retains its case because it is an abbreviation.
* 6. "HTTP" retaints its case because it is an abbreviation.
*
* If you do not like my changes, just fork MINIMAL and edit this file.
* You will be able to get updates without any problems, which probably will not touch this file.
*
* Or you can write a BASH/FISH script with the creation of a link to your version of the file,
* which will be executed after `composer install`. Of course, you should not do this,
* but this is a completely working solution that is unlikely to break anything
* and is completely automatic and portable.
*
* @throws exception_domain if failed to recognize status
*
* @return string Label
*/
public function label(): string
{
// Exit (success)
return match ($this) {
// 1XX
self::continue => 'Continue',
self::switching_protocols => 'Switching Protocols',
self::processing => 'Processing',
self::early_hints => 'Early Hints',
self::among_us => 'Among Us',
// 2XX
self::ok => 'Ok',
self::created => 'Created',
self::accepted => 'Accepted',
self::non_authoritative_information => 'Non Authoritative Information',
self::no_content => 'No Content',
self::reset_content => 'Reset Content',
self::partial_content => 'Partial Content',
self::multi_status => 'Multi Status',
self::already_reported => 'Already Reported',
self::im_used => 'I Am Used',
// 3XX
self::multiple_choises => 'Multiple Choices',
self::moved_permanently => 'Moved Permanently',
self::found => 'Found', // Previously "Moved Temporarily"
self::see_other => 'See Other',
self::not_modified => 'Not Modified',
self::use_proxy => 'Use Proxy',
self::switch_proxy => 'Switch Proxy',
self::temporary_redirect => 'Temporary Redirect',
self::permanent_redirect => 'Permanent Redirect',
// 4XX
self::bad_request => 'Bad Request',
self::unauthorized => 'Unauthorized',
self::payment_required => 'Payment Required', // do not make me angry
self::forbidden => 'Forbidden',
self::not_found => 'Not Found',
self::method_not_allowed => 'Method Not Allowed',
self::not_acceptable => 'Not Acceeptable',
self::proxy_authentication_required => 'Proxy Authentication Required',
self::request_timeout => 'Request Timeout',
self::conflict => 'Conflict',
self::gone => 'Gone',
self::length_required => 'Length Reuired',
self::precondition_failed => 'Precondition Failed',
self::payload_too_large => 'Payload Too Large',
self::uri_too_long => 'URI Too Long',
self::unsupported_media_type => 'Unsupported Media Type',
self::range_not_satisfiable => 'Range Not Satisfiable',
self::expectation_failed => 'Exception Failed',
self::i_am_a_teapot => 'I Am A Teapot',
self::misdirected_request => 'Misdirected Request',
self::unprocessable_content => 'Unprocessable Content',
self::locked => 'Locked',
self::failed_dependency => 'Failed Dependency',
self::too_early => 'Too Early',
self::upgrade_required => 'Upgrade Required',
self::precondition_required => 'Precondition Required',
self::too_many_requests => 'Too Many Requests',
self::request_header_fields_too_large => 'Request Header Fields Too Large',
self::unavailable_for_legal_reasons => 'Unavaiable For Legal Reasons', // Fucking disgrease.
self::bruh => 'Bruh',
// 5XX
self::internal_server_error => 'Internal Server Error',
self::not_implemented => 'Not Implemented',
self::bad_gateway => 'Bad Gateway',
self::service_unawaiable => 'Service Unawaiable',
self::gateway_timeout => 'Gateway Timeout',
self::http_version_not_supported => 'HTTP Version Not Supported',
self::variant_also_negotiates => 'Variant Also Negotiates',
self::insufficient_storage => 'Insufficient Storage',
self::loop_detected => 'Loop Detected',
self::not_extended => 'Not Extended',
self::network_authentication_required => 'Network Authentication Required',
// 9XX
self::failed_state => 'Failed State',
default => throw new exception_domain('Failed to recognize status', self::not_found->value)
};
}
}

View File

@ -0,0 +1,504 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http;
// Files of the project
use mirzaev\minimal\http\enumerations\method,
mirzaev\minimal\http\enumerations\protocol,
mirzaev\minimal\http\enumerations\status,
mirzaev\minimal\http\enumerations\content,
mirzaev\minimal\http\response;
// Built-in libraries
use DomainException as exception_domain,
InvalidArgumentException as exception_argument,
RuntimeException as exception_runtime,
LogicException as exception_logic;
/**
* Request
*
* @package mirzaev\minimal\http
*
* @param method $method Method
* @param string $uri URI
* @param protocol $protocol Version of HTTP protocol
* @param array $headers Headers
* @param array $parameters Deserialized parameters from URI and body
* @param array $files Deserialized files from body
* @param array $options Options for `request_parse_body($options)`
*
* @method void __construct(method|string|null $method, ?string $uri, protocol|string|null $protocol, array $headers, array $parameters, array $files, bool $environment) Constructor
* @method response response() Generate response for request
* @method self header(string $name, string $value) Write a header to the headers property
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class request
{
/**
* Method
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
* @throws exception_domain if failed to recognize method
*
* @var method $method Method
*/
public method $method {
// Write
set (method|string $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
if ($value instanceof method) {
// Received implementation of the method
// Writing
$this->method = $value;
} else {
// Received a string literal (excected name of the method)
// Initializing implementator of the method
$method = method::{strtolower($value)};
if ($method instanceof method) {
// Initialized implementator of the method
// Writing
$this->method = $method;
} else {
// Not initialized implementator of the method
// Exit (fail)
throw new exception_domain('Failed to recognize method: ' . $value, status::not_implemented->value);
}
}
}
}
/**
* URI
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
*
* @var string $uri URI
*/
public string $uri {
// Write
set (string $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
// Writing
$this->uri = $value;
}
}
/**
* Protocol
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
* @throws exception_domain if failed to recognize HTTP version
*
* @var protocol $protocol Version of HTTP protocol
*/
public protocol $protocol {
// Write
set (protocol|string $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
if ($value instanceof protocol) {
// Received implementation of HTTP version
// Writing
$this->protocol = $value;
} else {
// Received a string literal (excected name of HTTP version)
// Initializing implementator of HTTP version
$protocol = protocol::tryFrom($value);
if ($protocol instanceof protocol) {
// Initialized implementator of HTTP version
// Writing
$this->protocol = $protocol;
} else {
// Not initialized implementator of HTTP version
// Exit (fail)
throw new exception_domain('Failed to recognize HTTP version: ' . $value, status::http_version_not_supported->value);
}
}
}
}
/**
* Headers
*
* @see https://www.rfc-editor.org/rfc/rfc7540
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var array $headers Headers
*/
public array $headers = [] {
// Read
&get => $this->headers;
}
/**
* Parameters
*
* For "application/json" will store json_decode(file_get_contents('php://input'))
* For method::post will store the value from $_POST ?? []
* For other methods with $this->method->body() === true and "multipart/form-data" will store the result of executing request_parse_body($this->options)[0] (>=PHP 8.4)
* For other methods with $this->method->body() === true and "application/x-www-form-urlencoded" will store the result of executing request_parse_body($this->options)[0] (>=PHP 8.4)
* For other methods with $this->method->body() === true and other Content-Type will store $GLOBALS['_' . $this->method->value] ?? []
* For other methods with $this->method->body() === false will store the value from $GLOBALS['_' . $this->method->value] ?? []
*
* @see https://wiki.php.net/rfc/rfc1867-non-post about request_parse_body()
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
*
* @var array $parameters Deserialized parameters from URI and body
*/
public array $parameters {
// Write
set (array $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
// Writing
$this->parameters = $value;
}
// Read
get => $this->parameters ?? [];
}
/**
* Files
*
* For method::post will store the value from $_FILES ?? []
* For other methods with $this->method->body() === true and "multipart/form-data" will store the result of executing request_parse_body($this->options)[1] (>=PHP 8.4)
* For other methods with $this->method->body() === true and "application/x-www-form-urlencoded" will store the result of executing request_parse_body($this->options)[1] (>=PHP 8.4)
* For other methods with $this->method->body() === true and other Content-Type will store $_FILES ?? []
*
* @see https://wiki.php.net/rfc/rfc1867-non-post about request_parse_body()
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
* @throws exception_runtime if $this->method is not initialized
* @throws exception_logic if request with that method can not has files
*
* @var array $files Deserialized files from body
*/
public array $files {
// Write
set (array $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
if (isset($this->method)) {
// Initialized method
if ($this->method->body()) {
// Request with this method can has body
// Writing
$this->files = $value;
} else {
// Request with this method can not has body
// Exit (fail)
throw new exception_logic('Request with ' . $this->method->value . ' method can not has body therefore can not has files', status::internal_server_error->value);
}
} else {
// Not initialized method
// Exit (fail)
throw new exception_runtime('Method of the request is not initialized', status::internal_server_error->value);
}
}
// Read
get => $this->files ?? [];
}
/**
* Options
*
* Required if $this->method !== method::post
*
* @see https://wiki.php.net/rfc/rfc1867-non-post about request_parse_body()
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
*
* @var array $options Options for `request_parse_body($options)`
*/
public array $options {
// Write
set (array $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
// Writing
$this->options = array_filter(
$value,
fn(string $key) => match ($key) {
'post_max_size',
'max_input_vars',
'max_multipart_body_parts',
'max_file_uploads',
'upload_max_filesize' => true,
default => throw new exception_domain("Failed to recognize option: $key", status::internal_server_error->value)
},
ARRAY_FILTER_USE_KEY
);
}
// Read
get => $this->options ?? [];
}
/**
* Constructor
*
* @param method|string|null $method Name of the method
* @param string|null $uri URI
* @param protocol|string|null $protocol Version of HTTP protocol
* @param array|null $headers Headers
* @param array|null $parameters Deserialized parameters from URI and body
* @param array|null $files Deserialized files from body
* @param bool $environment Write values from environment to properties?
*
* @throws exception_domain if failed to normalize name of header
* @throws exception_argument if failed to initialize JSON
* @throws exception_argument if failed to initialize a required property
*
* @return void
*/
public function __construct(
method|string|null $method = null,
?string $uri = null,
protocol|string|null $protocol = null,
?array $headers = null,
?array $parameters = null,
?array $files = null,
bool $environment = false
) {
// Writing method from argument into the property
if (isset($method)) $this->method = $method;
// Writing URI from argument into the property
if (isset($uri)) $this->uri = $uri;
// Writing verstion of HTTP protocol from argument into the property
if (isset($protocol)) $this->protocol = $protocol;
if (isset($headers)) {
// Received headers
// Declaring the buffer of headers
$buffer = [];
foreach ($headers ?? [] as $name => $value) {
// Iterating over headers
// Normalizing name of header (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$name = mb_strtolower($name, 'UTF-8');
if (empty($name)) {
// Not normalized name of header
// Exit (fail)
throw new exception_domain('Failed to normalize name of header', status::internal_server_error->value);
}
// Writing into the buffer of headers
$buffer[$name] = $value;
}
// Writing headers from argument into the property
$this->headers = $buffer;
// Deinitializing the buffer of headers
unset($buffer);
}
// Writing parameters from argument into the property
if (isset($parameters)) $this->parameters = $parameters;
// Writing files from argument into the property
if (isset($files)) $this->files = $files;
if ($environment) {
// Requested to write values from environment
// Writing method from environment into the property
$this->method ??= $_SERVER["REQUEST_METHOD"];
// Writing URI from environment into the property
$this->uri ??= $_SERVER['REQUEST_URI'];
// Writing verstion of HTTP protocol from environment into the property
$this->protocol ??= $_SERVER['SERVER_PROTOCOL'];
if (!isset($headers)) {
// Received headers
// Declaring the buffer of headers
$buffer = [];
foreach (getallheaders() ?? [] as $name => $value) {
// Iterating over headers
// Normalizing name of header (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$name = mb_strtolower($name, 'UTF-8');
if (empty($name)) {
// Not normalized name of header
// Exit (fail)
throw new exception_domain('Failed to normalize name of header', status::internal_server_error->value);
}
// Writing into the buffer of headers
$buffer[$name] = $value;
}
// Writing headers from environment into the property
$this->headers = $buffer;
// Deinitializing the buffer of headers
unset($buffer);
}
if ($this->headers['content-type'] === content::json->value) {
// The body contains "application/json"
// Initializing data from the input buffer
$input = file_get_contents('php://input');
if (json_validate($input, 512)) {
// Validated JSON
// Decoding JSON and writing parameters into the property (array type for universalization)
$this->parameters = json_decode($input, true, 512);
} else {
// Not validated JSON
// Exit (false)
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) {
// POST method
// Writing parameters from environment into the property
$this->parameters = $_POST ?? [];
// Writing files from environment into the property
$this->files = $_FILES ?? [];
} else if ($this->method->body()) {
// Non POST method and can has body
if (match($this->headers['content-type']) { content::form->value, content::encoded->value => true, default => false }) {
// Non POST method and the body content type is "multipart/form-data" or "application/x-www-form-urlencoded"
// Writing parameters and files from environment into the properties
[$this->parameters, $this->files] = request_parse_body($this->options);
} else {
// Non POST method and the body content type is not "multipart/form-data" or "application/x-www-form-urlencoded"
// Writing parameters from environment into the property
$this->parameters = $GLOBALS['_' . $this->method->value] ?? [];
// Writing files from environment into the property
$this->files = $_FILES ?? [];
}
} else {
// Non POST method and can not has body
// Writing parameters from environment into the property
$this->parameters = $GLOBALS['_' . $this->method->value] ?? [];
}
}
// Validating of required properties
if (empty($this->method)) throw new exception_argument('Failed to initialize method of the request', status::internal_server_error->value);
if (empty($this->uri)) throw new exception_argument('Failed to initialize URI of the request', status::internal_server_error->value);
}
/**
* Response
*
* Generate response for request
*
* @return response Reponse for request
*/
public function response(): response
{
// Exit (success)
return new response(protocol: $this->protocol, status: status::ok);
}
/**
* Header
*
* Write a header to the headers property
*
* @see https://www.rfc-editor.org/rfc/rfc7540
*
* @param string $name Name
* @param string $value Value
*
* @return self The instance from which the method was called (fluent interface)
*/
public function header(string $name, string $value): self
{
// Normalizing name of header and writing to the headers property (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$this->headers[mb_strtolower($name, 'UTF-8')] = $value;
// Exit (success)
return $this;
}
}

View File

@ -0,0 +1,464 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\http;
// Files of the project
use mirzaev\minimal\http\enumerations\method,
mirzaev\minimal\http\enumerations\protocol,
mirzaev\minimal\http\enumerations\content,
mirzaev\minimal\http\enumerations\status,
mirzaev\minimal\http\header;
// Built-in libraries
use DomainException as exception_domain,
InvalidArgumentException as exception_argument,
RuntimeException as exception_runtime,
LogicException as exception_logic;
/**
* Response
*
* @package mirzaev\minimal\http
*
* @param protocol $protocol Version of HTTP protocol
* @param status $status Status
* @param array $headers Headers
* @param string $body Body
*
* @method self __construct(protocol|string|null $protocol, ?status $status, ?array $headers, bool $environment) Constructor
* @method self sse() Writes headers for SSE implementation
* @method self json(mixed $content) Writes headers for JSON implementation
* @method self write(string $value) Concatenates with the response body property
* @method self body() Transfer the contents of the body property to the output buffer
* @method self|false validate(request $request) Validate response by request
* @method string status() Generates the status line (HTTP/2 200 OK)
* @method self header(string $name, string $value) Write a header to the headers property
* @method self start() Initializes response headers and output buffer
* @method self clean() Delete everything in the output buffer
* @method self end() Initializes response headers and flushes the output buffer
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class response
{
/**
* Protocol
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_domain if failed to recognize HTTP version
*
* @var protocol $protocol Version of HTTP protocol
*/
public protocol $protocol {
// Write
set (protocol|string $value) {
if ($value instanceof protocol) {
// Received implementation of HTTP version
// Writing
$this->protocol = $value;
} else {
// Received a string literal (excected name of HTTP version)
// Initializing implementator of HTTP version
$protocol = protocol::{$value};
if ($protocol instanceof protocol) {
// Initialized implementator of HTTP version
// Writing
$this->protocol = $protocol;
} else {
// Not initialized implementator of HTTP version
// Exit (fail)
throw new exception_domain("Failed to recognize HTTP version: $value", status::http_version_not_supported>value);
}
}
}
}
/**
* Status
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_domain if failed to recognize status
*
* @var status $status Status
*/
public status $status {
// Write
set (status|string $value) {
if ($value instanceof status) {
// Received implementation of status
// Writing
$this->status = $value;
} else {
// Received a string literal (excected name of status)
// Initializing implementator of status
$status = status::{$value};
if ($status instanceof status) {
// Initialized implementator of status
// Writing
$this->status = $status;
} else {
// Not initialized implementator of status
// Exit (fail)
throw new exception_domain("Failed to recognize status: $value", status::internal_server_error->value);
}
}
}
}
/**
* Headers
*
* @see https://www.rfc-editor.org/rfc/rfc7540
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var array $headers Headers
*/
public array $headers = [] {
// Read
&get => $this->headers;
}
/**
* Body
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var string $body Serialized content
*/
public string $body = '' {
// Write
set (string $value) {
// Writing
$this->body = $value;
}
// Read
get => $this->body;
}
/**
* Constructor
*
* @param protocol|string|null $protocol version of http protocol
* @param status|null $status Status
* @param array|null $headers Headers
* @param bool $environment Write values from environment to properties?
*
* @throws exception_domain if failed to normalize name of header
* @throws exception_argument if failed to initialize a required property
*
* @return void
*/
public function __construct(
protocol|string|null $protocol = null,
?status $status = null,
?array $headers = null,
bool $environment = false
) {
// Writing verstion of HTTP protocol from argument into the property
if (isset($protocol)) $this->protocol = $protocol;
// Writing status from argument into the property
if (isset($status)) $this->status = $status;
if (isset($headers)) {
// Received headers
// Declaring the buffer of headers
$buffer = [];
foreach ($headers ?? [] as $name => $value) {
// Iterating over headers
// Normalizing name of header (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$name = mb_strtolower($name, 'UTF-8');
if (empty($name)) {
// Not normalized name of header
// Exit (fail)
throw new exception_domain('Failed to normalize name of header', status::internal_server_error->value);
}
// Writing into the buffer of headers
$buffer[$name] = $value;
}
// Writing headers from argument into the property
$this->headers = $buffer;
// Deinitializing the buffer of headers
unset($buffer);
}
if ($environment) {
// Requested to write values from environment
// Writing verstion of HTTP protocol from environment into the property
$this->protocol ??= $_SERVER['SERVER_PROTOCOL'];
// Writing status from environment into the property
$this->status ??= status::ok;
}
// Validating of required properties
if (!isset($this->protocol)) throw new exception_argument('Failed to initialize HTTP version of the request', status::internal_server_error->value);
if (!isset($this->status)) throw new exception_argument('Failed to initialize status of the request', status::internal_server_error->value);
}
/**
* Server-Sent Events (SSE)
*
* Writes headers for SSE implementation
*
* @return self The instance from which the method was called (fluent interface)
*/
public function sse(): self
{
// Writing headers to the headers property (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$this->headers['x-accel-buffering'] = 'no';
$this->headers['content-encoding'] = 'none';
// Exit (success)
return $this;
}
/**
* JSON
*
* Writes headers for JSON implementation
* Concatenates with the response body property if $content argument was received
*
* @param mixed $content JSON or content that will be serialized via json_encode()
*
* @return self The instance from which the method was called (fluent interface)
*/
public function json(mixed $content): self
{
// Writing headers to the headers property (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$this->headers['content-type'] = content::json->value;
if (!empty($content)) {
// Received content
if (is_string($content) && json_validate($content, 512)) {
// Validated as JSON
// Writing to the response body property
$this->body .= $content;
} else {
// Not validated as JSON
// Writing to the response body property
$this->body .= json_encode($content, depth: 512);
}
}
// Exit (success)
return $this;
}
/**
* Write
*
* Concatenates with the response body property
*
* @param string $value Value that will be concatenated with the response body property
*
* @return self The instance from which the method was called (fluent interface)
*/
public function write(string $value): self
{
// Writing to the response body property
$this->body .= $value;
// Exit (success)
return $this;
}
/**
* Body
*
* Transfer the contents of the body property to the output buffer
*
* @return self The instance from which the method was called (fluent interface)
*/
public function body(): self
{
// Writing to the output buffer
echo $this->body;
// Exit (success)
return $this;
}
/**
* Validate
*
* Validate response by request
*
* @param request $request The request for validating with it
*
* @return self|false The instance from which the method was called if validated (fluent interface)
*/
public function validate(request $request): self|false
{
if (str_contains($request->headers['accept'], $this->headers['content-type'] ?? '')) {
// Validated with "accept" and "content-type"
// Exit (success)
return $this;
}
// Exit (fail)
return false;
// Exit (fail)
return false;
}
/**
* Status line
*
* Generates the status line (HTTP/2 200 OK)
*
* @param protocol|null $protocol Version of HTTP
* @param status|null $status Status code and status text
*
* @return string The status line
*/
public function status(?protocol $protocol = null, ?status $status = null): string
{
// Declaring buffer of status line
$buffer = '';
if ($protocol instanceof protocol) {
// Received version of HTTP
// Writing to buffer of status line
$buffer .= $protocol->value . ' ';
} else {
// Not received version of HTTP
// Writing to buffer of status line
$buffer .= $this->protocol->value . ' ';
}
if ($status instanceof status) {
// Received status
// Writing to buffer of status line
$buffer .= $status->value . ' ' . $status->label();
} else {
// Not received status
// Writing to buffer of status line
$buffer .= $this->status->value . ' ' . $this->status->label();
}
// Exit (success)
return $buffer;
}
/**
* Header
*
* Write a header to the headers property
*
* @see https://www.rfc-editor.org/rfc/rfc7540
*
* @param string $name Name
* @param string $value Value
*
* @return self The instance from which the method was called (fluent interface)
*/
public function header(string $name, string $value): self
{
// Normalizing name of header and writing to the headers property (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
$this->headers[mb_strtolower($name, 'UTF-8')] = $value;
// Exit (success)
return $this;
}
/**
* Start
*
* Initializes response headers and output buffer
*
* @return self The instance from which the method was called (fluent interface)
*/
public function start(): self
{
// Initializing the heaader string
header($this->status());
// Initializing headers
foreach ($this->headers ?? [] as $name => $value) header("$name: $value", replace: true);
// Initializing the output buffer
ob_start();
// Exit (success)
return $this;
}
/**
* Clean
*
* Delete everything in the output buffer
*
* @return self The instance from which the method was called (fluent interface)
*/
public function clean(): self
{
// Flushing the output buffer
ob_clean();
// Exit (success)
return $this;
}
/**
* End
*
* Initializes response headers and flushes the output buffer
*
* @return self The instance from which the method was called (fluent interface)
*/
public function end(): self
{
// Calculate length of the output buffer and write to the header (https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2)
header('content-length: ' . ob_get_length());
// Sending response and flushing the output buffer
ob_end_flush();
flush();
// Deinitializing headers property
unset($this->headers);
// Deinitializing headers
header_remove();
// Exit (success)
return $this;
}
}

View File

@ -7,14 +7,13 @@ namespace mirzaev\minimal;
// Files of the project // Files of the project
use mirzaev\minimal\traits\magic; use mirzaev\minimal\traits\magic;
// Built-in libraries
use exception;
/** /**
* Model (base) * Model
* *
* @package mirzaev\minimal * @package mirzaev\minimal
* *
* @method void __construct() Constructor
*
* @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>
*/ */
@ -22,13 +21,10 @@ class model
{ {
use magic; use magic;
/**
* Postfix of file names
*/
public const string POSTFIX = '_model';
/** /**
* Constructor * Constructor
*
* @return void
*/ */
public function __construct() {} public function __construct() {}
} }

144
mirzaev/minimal/system/route.php Executable file
View File

@ -0,0 +1,144 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal;
/**
* Route
*
* @package mirzaev\minimal
*
* @param string|controller $controller Name of the controller
* @param string $method Name of the method of the method of $this->controller
* @param string|model $model Name of the model
* @param array $parameters Arguments for the $this->method (will be concatenated together with generated request parameters)
* @param array $options Options for `request_parse_body($options)`
*
* @method void __construct(string|controller $controller, ?string $method, string|model|null $model, array $parameters, array $options) Constructor
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
final class route
{
/**
* Controller
*
* @var string|controller $controller Name of the controller or an instance of the controller
*/
public string|controller $controller {
// Read
get => $this->controller;
}
/**
* Method
*
* @var string $method Name of the method of the method of $this->controller
*/
public string $method{
// Read
get => $this->method;
}
/**
* Model
*
* @var string|model|null $model Name of the model of an instance of the model
*/
public string|model|null $model {
// Read
get => $this->model;
}
/**
* 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 $parameters = [] {
// Read
&get => $this->parameters;
}
/**
* Options
*
* Required if $this->method !== method::post
*
* @see https://wiki.php.net/rfc/rfc1867-non-post about request_parse_body()
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @throws exception_runtime if reinitialize the property
*
* @var array $options Options for `request_parse_body($options)`
*/
public array $options {
// Write
set (array $value) {
if (isset($this->{__PROPERTY__})) {
// The property is already initialized
// Exit (fail)
throw new exception_runtime('The property is already initialized: ' . __PROPERTY__, status::internal_server_error->value);
}
// Writing
$this->options = array_filter(
$value,
fn(string $key) => match ($key) {
'post_max_size',
'max_input_vars',
'max_multipart_body_parts',
'max_file_uploads',
'upload_max_filesize' => true,
default => throw new exception_domain("Failed to recognize option: $key", status::internal_server_error->value)
},
ARRAY_FILTER_USE_KEY
);
}
// Read
get => $this->options ?? [];
}
/**
* Constructor
*
* @param string|controller $controller Name of the controller
* @param string|null $method Name of the method of the method of $controller
* @param string|model|null $model Name of the model
* @param array $parameters Arguments for the $method (will be concatenated together with generated request parameters)
* @param array $options Options for `request_parse_body` (Only for POST method)
*
* @return void
*/
public function __construct(
string|controller $controller,
?string $method = 'index',
string|model|null $model = null,
array $parameters = [],
array $options = []
) {
// Writing name of the controller
$this->controller = $controller;
// Writing name of the model
$this->model = $model;
// Writing name of the method of the controller
$this->method = $method;
// Writing parameters
$this->parameters = $parameters;
// Writing options
if (match ($method) {
'GET', 'PUT', 'PATCH', 'DELETE' => true,
default => false
}) $this->options = $options;
}
}

View File

@ -4,71 +4,85 @@ declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
// Файлы проекта // Files of the project
use mirzaev\minimal\core; use mirzaev\minimal\route,
mirzaev\minimal\http\request,
mirzaev\minimal\traits\singleton;
// Build-ing libraries
use InvalidArgumentException as exception_argument;
/** /**
* Маршрутизатор * Router
* *
* @package mirzaev\minimal * @package mirzaev\minimal
* *
* @param array $routes Registry of routes
*
* @method self write(string $urn, route $route, string|array $method) Write route to registry of routes (fluent interface)
* @method route|null match(request $request) Match request URI with registry of routes
* @method self sort() Sort routes (DEV)
* @method string universalize(string $urn) Universalize URN
*
* @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 router final class router
{ {
/** use singleton;
* @var array $router Реестр маршрутов
*/
/* protected array $routes = []; */
public array $routes = [];
/** /**
* Записать маршрут * Routes
* *
* @param string $route Маршрут * @var array $routes Registry of routes
* @param string $handler Обработчик - инстанции контроллера и модели (не обязательно), без постфиксов
* @param ?string $method Вызываемый метод в инстанции контроллера обработчика
* @param ?string $request HTTP-метод запроса (GET, POST, PUT...)
* @param ?string $model Инстанция модели (переопределение инстанции модели в $target)
* @param array $variables
*
* @return static The instance from which the method was called (fluent interface)
*/ */
public function write( protected array $routes = [] {
string $route, // Read
string $handler, &get => $this->routes;
?string $method = 'index', }
?string $request = 'GET',
?string $model = null, /**
array $variables = [] * Write route
): static { *
// Запись в реестр * Write route to registry of routes
$this->routes[$route][$request] = [ *
'controller' => $handler, * @param string $urn URN of the route ('/', '/page', '/page/$variable', '/page/$collector...'...)
'model' => $model ?? $handler, * @param route $route The route
'method' => $method, * @param string|array $method Method of requests
'variables' => $variables *
]; * @return self The instance from which the method was called (fluent interface)
*/
public function write(string $urn, route $route, string|array $method): self
{
foreach (is_array($method) ? $method : [$method] as $method) {
// Iterate over methods of requests
// Initializing the request
$request = new request(
uri: $urn,
method: $method,
environment: false
);
// Writing to the registry of routes
$this->routes[$request->uri][$request->method->value] = $route;
}
// Exit (success) (fluent interface) // Exit (success) (fluent interface)
return $this; return $this;
} }
/** /**
* Handle a request * Match
* *
* @param string|null $uri URI (protocol://domain/foo/bar) * Match request URI with registry of routes
* @param string|null $method Method (GET, POST, PUT...)
* @param core|null $core Instence of the system core
* *
* @return string|int|null Response * @param request $request The request
*
* @return route|null Route, if found
*/ */
public function handle( public function match(request $request): ?route
?string $uri = null, {
?string $method = null,
?core $core = new core
): string|int|null {
// Declaration of the registry of routes directoies // Declaration of the registry of routes directoies
$routes = []; $routes = [];
@ -83,15 +97,8 @@ final class router
if (count($routes) === count($this->routes)) { if (count($routes) === count($this->routes)) {
// Initialized the registry of routes directoies // Initialized the registry of routes directoies
// Initializing default values // Universalization of URN (/foo/bar)
$uri ??= $_SERVER['REQUEST_URI'] ?? '/'; $urn = self::universalize(parse_url(urldecode($request->uri), PHP_URL_PATH));
$method ??= $_SERVER["REQUEST_METHOD"];
// Initializing of URN (/foo/bar)
$urn = parse_url(urldecode($uri), PHP_URL_PATH);
// Universalization of URN
$urn = self::universalize($urn);
// Search directories of URN (explode() creates empty value in array) // Search directories of URN (explode() creates empty value in array)
preg_match_all('/(^\/$|[^\/]+)/', $urn, $directories); preg_match_all('/(^\/$|[^\/]+)/', $urn, $directories);
@ -110,7 +117,7 @@ final class router
foreach ($this->routes as $route => $data) { foreach ($this->routes as $route => $data) {
// Iteration over routes // Iteration over routes
if (isset($data[$method])) { if (isset($data[$request->method->value])) {
// The request method matches the route method // The request method matches the route method
// Universalization of route // Universalization of route
@ -171,12 +178,12 @@ final class router
// The directory is a variable ($variable) // The directory is a variable ($variable)
// Запись в реестр переменных и перещапись директории в маршруте // Запись в реестр переменных и перещапись директории в маршруте
$data[$method]['variables'][trim($route_directory, '$')] = $directories[$i]; $data[$request->method->value]->variables[trim($route_directory, '$')] = $directories[$i];
} else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1) { } else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1) {
// The directory of route is a collector ($variable...) // The directory of route is a collector ($variable...)
// Инициализаия ссылки на массив сборщика // Инициализаия ссылки на массив сборщика
$collector = &$data[$method]['variables'][trim($route_directory, '$.')]; $collector = &$data[$request->method->value]->variables[trim($route_directory, '$.')];
// Инициализаия массива сборщика // Инициализаия массива сборщика
$collector ??= []; $collector ??= [];
@ -195,31 +202,13 @@ final class router
} }
} }
/** // Exit (success or fail)
* Initialization of route handlers return $data[$request->method->value] ?? null;
*/
if (array_key_exists($method, $data)) {
// Идентифицирован метод маршрута (GET, POST, PUT...)
// Инициализация обработчиков (controller, model, method)
$handlers = $data[$method];
if (class_exists($controller = $core->namespace . '\\controllers\\' . $handlers['controller'] . $core->controller::POSTFIX)) {
// Найден контроллер
// Инициализация инстанции ядра контроллера
$controller = new $controller;
// Вызов связанного с маршрутом метода и возврат (успех)
return $controller->{$handlers['method']}($handlers['variables'] + $_REQUEST, $_FILES, file_get_contents('php://input'));
}
}
} }
} }
// Возврат (провал) // Exit (fail)
return $this->error($core); return null;
} }
/** /**
@ -236,11 +225,11 @@ final class router
* *
* Добавить чтобы сначала текст потом переменная затем после переменной первыми тексты и в конце перменные опять и так рекурсивно * Добавить чтобы сначала текст потом переменная затем после переменной первыми тексты и в конце перменные опять и так рекурсивно
* *
* @return static The instance from which the method was called (fluent interface) * @return self The instance from which the method was called (fluent interface)
* *
* @todo ПЕРЕДЕЛАТЬ ПОЛНОСТЬЮ * @todo ПЕРЕДЕЛАТЬ ПОЛНОСТЬЮ
*/ */
public function sort(): static public function sort(): self
{ {
uksort($this->routes, function (string $a, string $b) { uksort($this->routes, function (string $a, string $b) {
// Sorting routes // Sorting routes
@ -284,35 +273,18 @@ final class router
return $this; return $this;
} }
/**
* Сгенерировать ответ с ошибкой
*
* Вызывает метод error404 в инстанции контроллера ошибок
*
* @param ?core $core Инстанция системного ядра
*
* @return ?string HTML-документ
*/
private function error(core $core = new core): ?string
{
return class_exists($class = '\\' . $core->namespace . '\\controllers\\errors' . $core->controller::POSTFIX)
&& method_exists($class, $method = 'error404')
? (new $class)->$method()
: null;
}
/** /**
* Universalize URN * Universalize URN
* *
* Always "/" at the beginning and never "/" at the end * Always "/" at the beginning and never "/" at the end
* *
* @param string &$urn URN (/foo/bar) * @param string $urn URN (/foo/bar)
* *
* @return string Universalized URN * @return string Universalized URN
*/ */
private function universalize(string $urn): string private function universalize(string $urn): string
{ {
// Universalization of URN and exit (success) // Universalization of URN and exit (success)
return (string) preg_replace('/(.+)\/$/', '$1', preg_replace('/^([^\/])/', '/$1', $urn)); return (string) '/' . mb_trim($urn, '/');
} }
} }

View File

@ -4,18 +4,23 @@ declare(strict_types=1);
namespace mirzaev\minimal\traits; namespace mirzaev\minimal\traits;
// Files of the project
use mirzaev\minimal\http\enumerations\status;
// Built-in libraries // Built-in libraries
use exception; use exception;
/** /**
* Trait of magical methods * Trait of magical methods
* *
* @method void __set(string $name, mixed $value = null) Write property * @package mirzaev\minimal\traits
*
* @method void __set(string $name, mixed $value) Write property
* @method mixed __get(string $name) Read property * @method mixed __get(string $name) Read property
* @method void __unset(string $name) Delete property * @method void __unset(string $name) Delete property
* @method bool __isset(string $name) Check property for initialization * @method bool __isset(string $name) Check property for initialization
* *
* @package mirzaev\minimal\traits * @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>
*/ */
trait magic trait magic
@ -31,7 +36,7 @@ trait magic
public function __set(string $name, mixed $value = null): void public function __set(string $name, mixed $value = null): void
{ {
match ($name) { match ($name) {
default => throw new exception('Property "' . static::class . "::\$$name\" not found", 404) default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value)
}; };
} }
@ -45,7 +50,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('Property "' . static::class . "::\$$name\" not found", 404) default => throw new exception('Failed to find property: ' . static::class . "::\$$name", status::not_found->value)
}; };
} }

View File

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\traits;
// Built-in libraries
use exception;
/**
* Trait of singleton
*
* @package mirzaev\minimal\traits
*
* @method static initialize() Initialize an instance
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
trait singleton
{
/**
* Constructor (blocked)
*
* @return void
*/
final private function __construct()
{
// Initializing the indicator that an instance of static::class has already been initialized
static $instance = false;
if ($instance) {
// An instance of static has already been initialized
// Exit (fail)
throw new exception('Has already been initialized an instance of the ' . static::class);
}
// Writing the indicator that the instance of static been initialized
$instance = true;
}
/**
* Initialize an instance
*
* Create an instance of static::class, or return an already created static::class instance
*
* @return static
*/
public static function initialize(): static
{
// Initialize the buffer of the instance of static::class
static $instance;
// Exit (success)
return $instance ??= new static;
}
/**
* Clone (blocked)
*/
private function __clone()
{
// Exit (fail)
throw new exception('Cloning is inadmissible');
}
/**
* Sleep (blocked)
*/
public function __sleep()
{
// Exit (fail)
throw new exception('Serialization is inadmissible');
}
/**
* Wake up (blocked)
*/
public function __wakeup()
{
// Exit (fail)
throw new exception('Deserialisation is inadmissible');
}
}