16 Commits
0.1.0 ... 2.3.0

Author SHA1 Message Date
e1a6483556 collectors, new router, errors handlers, refactoring 2024-10-11 10:14:33 +03:00
95ddffba30 fix postfix public 2024-01-04 04:08:44 +07:00
41bf8ab56a обновил чёто забыл уже 2023-12-21 23:14:34 +07:00
3d31c92628 a little fixes and transit into PHP 8.2 2023-03-20 21:46:41 +10:00
1f5685a20d Косметические изменения кода 2022-11-03 16:09:32 +10:00
22ad7304f9 Перенос с git.hood.su на git.mirzaev.sexy 2022-11-03 08:27:45 +10:00
Arsen Mirzaev Tatyano-Muradovich
7777d7af17 Баг при запросе маршрута с неправильным типом запроса 2022-03-04 07:15:52 +10:00
Arsen Mirzaev Tatyano-Muradovich
483814b6a5 Теперь понимает передачу файлов 2022-03-04 04:35:21 +10:00
Arsen Mirzaev Tatyano-Muradovich
e7a6b9cebe Исправления для маршрута на главную страницу 2022-03-04 02:15:42 +10:00
Arsen Mirzaev Tatyano-Muradovich
a4949ebc52 Мелкое исправление по переменным в контроллер 2022-02-28 04:40:03 +10:00
Arsen Mirzaev Tatyano-Muradovich
aeed2d21cc Доработка передачи переменных в контроллер 2022-02-28 04:30:29 +10:00
Arsen Mirzaev Tatyano-Muradovich
81990de191 Маршрутизатор теперь умеет в переменные 2022-02-28 04:22:21 +10:00
Arsen Mirzaev Tatyano-Muradovich
b6f90b7001 Исправление composer.json 2021-11-12 23:20:13 +10:00
Arsen Mirzaev Tatyano-Muradovich
db36a0a27e Исправления первой версии 2021-10-01 03:41:24 +10:00
Arsen Mirzaev Tatyano-Muradovich
ebfa14d8d6 Первая версия 2021-10-01 03:40:44 +10:00
Arsen Mirzaev Tatyano-Muradovich
2d06e386f5 Поверхностные исправления 2021-09-26 02:35:59 +10:00
12 changed files with 3111 additions and 2865 deletions

0
.gitignore vendored Normal file → Executable file
View File

81
composer.json Normal file → Executable file
View File

@@ -1,44 +1,37 @@
{ {
"name": "mirzaev/minimal", "name": "mirzaev/minimal",
"type": "framework", "type": "framework",
"description": "Легковесный MVC фреймворк который следует твоим правилам, а не диктует свои", "description": "Lightweight MVC framework that manages only the basic mechanisms, leaving the development of the programmer and not overloading the project",
"keywords": [ "keywords": [
"mvc", "mvc",
"framework" "framework",
], "lightweight"
"homepage": "https://git.hood.su/mirzaev/minimal", ],
"license": "WTFPL", "license": "WTFPL",
"authors": [ "homepage": "https://git.mirzaev.sexy/mirzaev/minimal",
{ "authors": [
"name": "Arsen Mirzaev Tatyano-Muradovich", {
"email": "arsen@mirzaev.sexy", "name": "Arsen Mirzaev Tatyano-Muradovich",
"homepage": "https://mirzaev.sexy", "email": "arsen@mirzaev.sexy",
"role": "Developer" "homepage": "https://mirzaev.sexy",
} "role": "Developer"
], }
"support": { ],
"docs": "https://git.hood.su/mirzaev/minimal/manual", "support": {
"issues": "https://git.hood.su/mirzaev/minimal/issues" "docs": "https://git.mirzaev.sexy/mirzaev/minimal/wiki",
}, "issues": "https://git.mirzaev.sexy/mirzaev/minimal/issues"
"require": { },
"php": "~8.0", "require": {
"psr/log": "~3.0", "php": "~8.2"
"twig/twig": "^3.3" },
}, "autoload": {
"require-dev": { "psr-4": {
"phpunit/phpunit": "~9.5" "mirzaev\\minimal\\": "mirzaev/minimal/system"
}, }
"suggest": { },
"ext-PDO": "Для работы с базами данных на SQL (MySQL, PostreSQL...)" "autoload-dev": {
}, "psr-4": {
"autoload": { "mirzaev\\minimal\\tests\\": "mirzaev/minimal/tests"
"psr-4": { }
"mirzaev\\minimal\\": "mirzaev/minimal/system" }
} }
},
"autoload-dev": {
"psr-4": {
"mirzaev\\minimal\\tests\\": "mirzaev/minimal/tests"
}
}
}

4640
composer.lock generated Normal file → Executable file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal;
// Files of the project
use mirzaev\minimal\model,
mirzaev\minimal\traits\magic;
// Встроенные библиотеки
use exception;
/**
* Controller (base)
*
* @package mirzaev\minimal
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
class controller
{
use magic;
/**
* Postfix of file names
*/
public const string POSTFIX = '_controller';
/**
* Instance of the model connected in the router
*/
protected model $model;
/**
* View template engine instance (twig)
*/
protected object $view;
/**
* Constructor
*/
public function __construct() {}
/**
* Write property
*
* @param string $name Name of the property
* @param mixed $value Value of the property
*
* @return void
*/
public function __set(string $name, mixed $value = null): void
{
match ($name) {
'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

@@ -1,148 +0,0 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\controllers;
use mirzaev\minimal\core;
use mirzaev\minimal\models\model;
use Twig\Loader\FilesystemLoader;
use Twig\Environment as view;
use Exception;
/**
* Контроллер
*
* @package mirzaev\minimal\controllers
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
class controller
{
/**
* @var model $model Модель
*/
protected model $model;
/**
* @var view $view Шаблонизатор представления
*/
protected view $view;
/**
* Конструктор
*
* @return void
*/
public function __construct()
{
// Установка значения по умолчанию для модели (если будет найдена)
$this->__get('model');
// Установка значения по умолчанию для шаблонизатора представлений
$this->__get('view');
}
/**
* Отрисовка шаблона
*
* @param string $route Маршрут
*/
public function view(string $route)
{
// Чтение представления по шаблону пути: "/views/[controller]/index
// Никаких слоёв и шаблонизаторов
// Не стал в ядре записывать путь до шаблонов
if (file_exists($view = core::path() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $route . DIRECTORY_SEPARATOR . 'index.html')) {
include $view;
}
}
/**
* Записать свойство
*
* @param mixed $name Название
* @param mixed $value Значение
*
* @return void
*/
public function __set($name, $value): void
{
if ($name === 'model') {
if (!isset($this->model)) {
$this->model = $value;
return;
} else {
throw new Exception('Запрещено переопределять модель');
}
} else if ($name === 'view') {
if (!isset($this->view)) {
$this->view = $value;
return;
} else {
throw new Exception('Запрещено переопределять шаблонизатор представления');
}
}
throw new Exception('Свойство не найдено: ' . $name);
}
/**
* Прочитать свойство
*
* @param mixed $name Название
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'model') {
if (isset($this->model)) {
// Если модель найдена
return $this->model;
} else {
// Инициализация класса модели
$model = preg_replace('/' . core::controllerPostfix() . '$/i', '', basename(get_class($this))) . core::modelPostfix();
// Иначе
if (class_exists($model_class = core::namespace() . '\\models\\' . $model)) {
// Если найдена одноимённая с контроллером модель (без постфикса)
return $this->model = new $model_class;
}
return;
}
} else if ($name === 'view') {
if (isset($this->view)) {
// Если модель найдена
return $this->view;
} else {
$path = core::path() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'views';
$loader = new FilesystemLoader($path);
return $this->view = (new view($loader, [
// 'cache' => $path . DIRECTORY_SEPARATOR . 'cache',
]));
}
}
throw new Exception('Свойство не найдено: ' . $name);
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*
* @return mixed
*/
public function __isset(string $name)
{
if ($name === 'model') {
return isset($this->model);
} else if ($name === 'view') {
return isset($this->view);
}
throw new Exception('Свойство не найдено: ' . $name);
}
}

368
mirzaev/minimal/system/core.php Normal file → Executable file
View File

@@ -1,141 +1,227 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
use mirzaev\minimal\router; // Файлы проекта
use mirzaev\minimal\router,
use PDO; mirzaev\minimal\controller,
use PDOException; mirzaev\minimal\model;
use Exception; // Встроенные библиотеки
use exception,
/** ReflectionClass as reflection;
* Ядро
* /**
* @package mirzaev\minimal * Core
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> *
*/ * @package mirzaev\minimal
final class core *
{ * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
/** * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
* @var PDO $db Соединение с базой данных */
*/ final class core
private static PDO $db; {
/**
/** * Инстанция соединения с базой данных
* @var router $router Маршрутизатор */
*/ private object $db;
private static router $router;
/**
/** * Инстанция маршрутизатора
* @var string $path Корневая директория */
*/ private readonly router $router;
private static string $path;
/**
/** * Инстанция ядра контроллера
* @var string $namespace Пространство имён */
*/ private readonly controller $controller;
private static string $namespace;
/**
/** * Инстанция ядра модели
* @var string $postfix_controller Постфикс контроллеров */
*/ private readonly model $model;
private static string $postfix_controller = '_controller';
/**
/** * Путь пространства имён (системное)
* @var string $postfix_model Постфикс моделей *
*/ * Используется для поиска файлов по спецификации PSR-4
private static string $postfix_model = '_model'; */
private readonly string $namespace;
/**
* Конструктор /**
* * Конструктор
* @param string $db *
* @param string $login * @param ?object $db Инстанция соединения с базой данных
* @param string $password * @param ?router $router Маршрутизатор
* @param router $router Маршрутизатор * @param ?controller $controller Инстанция ядра контроллера
*/ * @param ?model $model Инстанция ядра модели
public function __construct(string $db = 'mysql:dbname=db;host=127.0.0.1', string $login = '', string $password = '', router $router = null) * @param ?string $namespace Пространство имён системного ядра
{ *
// Инициализация маршрутизатора * @return self Инстанция ядра
self::$router = $router ?? new router; */
public function __construct(
// Инициализация корневого пространства имён ?object $db = null,
self::$namespace = __NAMESPACE__; ?router $router = null,
?controller $controller = null,
try { ?model $model = null,
// Инициализация PDO ?string $namespace = null
self::$db = new PDO($db, $login, $password); ) {
} catch (PDOException $e) { // Инициализация свойств
throw new Exception('Проблемы при соединении с базой данных: ' . $e->getMessage(), $e->getCode()); 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);
self::$router::handle(); $this->__set('namespace', $namespace ?? (new reflection(self::class))->getNamespaceName());
} }
/** /**
* Деструктор * Деструктор
* *
*/ */
public function __destruct() public function __destruct() {}
{
// Закрытие соединения /**
} * Запуск
*
/** * @param ?string $uri Маршрут
* Прочитать/записать корневую директорию *
* * @return string|int|null Ответ
* @var string|null $path Путь */
* public function start(string $uri = null): string|int|null
* @return string {
*/ // Обработка запроса
public static function path(string $path = null): string return $this->__get('router')->handle($uri, core: $this);
{ }
return self::$path = (string) ($path ?? self::$path);
} /**
* Записать свойство
/** *
* Прочитать/записать соединение с базой данных * @param string $name Название
* * @param mixed $value Содержимое
* @var PDO|null $db Соединение с базой данных *
* * @return void
* @return PDO */
*/ public function __set(string $name, mixed $value = null): void
public static function db(PDO $db = null): PDO {
{ match ($name) {
return self::$db = $db ?? self::$db; 'db', 'database' => (function () use ($value) {
} if ($this->__isset('db')) throw new exception('Запрещено реинициализировать инстанцию соединения с базой данных ($this->db)', 500);
else {
/** // Свойство ещё не было инициализировано
* Прочитать постфикс контроллеров
* if (is_object($value)) $this->db = $value;
* @return string|null else throw new exception('Свойство $this->db должно хранить инстанцию соединения с базой данных', 500);
*/ }
public static function controllerPostfix(): ?string })(),
{ 'router' => (function () use ($value) {
return self::$postfix_controller; if ($this->__isset('router')) throw new exception('Запрещено реинициализировать инстанцию маршрутизатора ($this->router)', 500);
} else {
// Свойство ещё не было инициализировано
/**
* Прочитать постфикс моделей if ($value instanceof router) $this->router = $value;
* else throw new exception('Свойство $this->router должно хранить инстанцию маршрутизатора (mirzaev\minimal\router)"', 500);
* @return string|null }
*/ })(),
public static function modelPostfix(): ?string 'controller' => (function () use ($value) {
{ if ($this->__isset('controller')) throw new exception('Запрещено реинициализировать инстанцию ядра контроллеров ($this->controller)', 500);
return self::$postfix_model; else {
} // Свойство не инициализировано
/** if ($value instanceof controller) $this->controller = $value;
* Прочитать пространство имён else throw new exception('Свойство $this->controller должно хранить инстанцию ядра контроллеров (mirzaev\minimal\controller)', 500);
* }
* @return string|null })(),
*/ 'model' => (function () use ($value) {
public static function namespace(): ?string if ($this->__isset('model')) throw new exception('Запрещено реинициализировать инстанцию ядра моделей ($this->model)', 500);
{ else {
return self::$namespace; // Свойство не инициализировано
}
} if ($value instanceof model) $this->model = $value;
else throw new exception('Свойство $this->model должно хранить инстанцию ядра моделей (mirzaev\minimal\model)', 500);
}
})(),
'namespace' => (function () use ($value) {
if ($this->__isset('namespace')) throw new exception('Запрещено реинициализировать путь пространства имён ($this->namespace)', 500);
else {
// Свойство не инициализировано
if (is_string($value)) $this->namespace = $value;
else throw new exception('Свойство $this->namespace должно хранить строку с путём пространства имён', 500);
}
})(),
default => throw new exception("Свойство \"\$$name\" не найдено", 404)
};
}
/**
* Прочитать свойство
*
* Записывает значение по умолчанию, если свойство не инициализировано
*
* @param string $name Название
*
* @return mixed Содержимое
*/
public function __get(string $name): mixed
{
return match ($name) {
'db', 'database' => $this->db ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500),
'router' => (function () {
// Инициализация со значением по умолчанию
if (!$this->__isset('router')) $this->__set('router', new router);
// Возврат (успех)
return $this->router;
})(),
'controller' => (function () {
// Инициализация со значением по умолчанию
if (!$this->__isset('controller')) $this->__set('controller', new controller);
// Возврат (успех)
return $this->controller;
})(),
'model' => (function () {
// Инициализация со значением по умолчанию
if (!$this->__isset('model')) $this->__set('model', new model);
// Возврат (успех)
return $this->model;
})(),
'namespace' => $this->namespace ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500),
default => throw new exception("Свойство \"\$$name\" не найдено", 404)
};
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*
* @return bool Инициализировано свойство?
*/
public function __isset(string $name): bool
{
return match ($name) {
default => isset($this->{$name})
};
}
/**
* Удалить свойство
*
* @param string $name Название
*
* @return void
*/
public function __unset(string $name): void
{
match ($name) {
default => (function () use ($name) {
// Удаление
unset($this->{$name});
})()
};
}
}

View File

@@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal;
// Files of the project
use mirzaev\minimal\traits\magic;
// Built-in libraries
use exception;
/**
* Model (base)
*
* @package mirzaev\minimal
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
class model
{
use magic;
/**
* Postfix of file names
*/
public const string POSTFIX = '_model';
/**
* Constructor
*/
public function __construct() {}
}

View File

@@ -1,90 +0,0 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\models;
use mirzaev\minimal\core;
use PDO;
use Exception;
/**
* Модель
*
* @package mirzaev\minimal\models
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
class model
{
/**
* @var PDO $db Соединение с базой данных
*/
protected PDO $db;
/**
* Конструктор
*
* @param PDO|null $db Соединение с базой данных
*/
public function __construct(PDO $db = null)
{
$this->db = $db ?? core::db();
}
/**
* Записать свойство
*
* @param mixed $name Название
* @param mixed $value Значение
*
* @return void
*/
public function __set($name, $value): void
{
if ($name === 'db') {
if (!isset($this->db)) {
$this->db = $value;
return;
} else {
throw new Exception('Запрещено переопределять соединение с базой данных');
}
}
throw new Exception('Свойство не найдено: ' . $name);
}
/**
* Прочитать свойство
*
* @param mixed $name Название
*
* @return mixed
*/
public function __get($name)
{
if ($name === 'db') {
return $this->db;
}
throw new Exception('Свойство не найдено: ' . $name);
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*
* @return mixed
*/
public function __isset(string $name)
{
if ($name === 'db') {
return isset($this->db);
}
throw new Exception('Свойство не найдено: ' . $name);
}
}

440
mirzaev/minimal/system/router.php Normal file → Executable file
View File

@@ -1,122 +1,318 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
use mirzaev\shop\core; // Файлы проекта
use mirzaev\minimal\core;
/**
* Маршрутизатор /**
* * Маршрутизатор
* @package mirzaev\shop *
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> * @package mirzaev\minimal
*/ *
final class router * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
{ * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
/** */
* @var array $router Маршруты final class router
*/ {
public static array $routes = []; /**
* @var array $router Реестр маршрутов
/** */
* Новый маршрут /* protected array $routes = []; */
* public array $routes = [];
* @param string $route Маршрут
* @param string $controller Контроллер /**
* @param string|null $method Метод * Записать маршрут
* @param string|null $type Тип *
* @param string|null $model Модель * @param string $route Маршрут
* * @param string $handler Обработчик - инстанции контроллера и модели (не обязательно), без постфиксов
* @return void * @param ?string $method Вызываемый метод в инстанции контроллера обработчика
*/ * @param ?string $request HTTP-метод запроса (GET, POST, PUT...)
public static function create(string $route, string $controller, string $method = null, string $type = 'GET', string $model = null): void * @param ?string $model Инстанция модели (переопределение инстанции модели в $target)
{ * @param array $variables
if (is_null($model)) { *
$model = $controller; * @return static The instance from which the method was called (fluent interface)
} */
public function write(
self::$routes[$route][$type] = [ string $route,
// Инициализация контроллера с постфиксом string $handler,
'controller' => preg_match('/' . core::controllerPostfix() . '$/i', $controller) ? $controller : $controller . core::controllerPostfix(), ?string $method = 'index',
'model' => preg_match('/' . core::modelPostfix() . '$/i', $model) ? $model : $model . core::modelPostfix(), ?string $request = 'GET',
'method' => $method ?? '__construct' ?string $model = null,
]; array $variables = []
} ): static {
// Запись в реестр
/** $this->routes[$route][$request] = [
* Обработка маршрута 'controller' => $handler,
* 'model' => $model ?? $handler,
* @param string $route Маршрут 'method' => $method,
* @param string $controller Контроллер 'variables' => $variables
* ];
* @return void
*/ // Exit (success) (fluent interface)
public static function handle(string $uri = null): void return $this;
{ }
// Если не передан URI, то взять из данных веб-сервера
$uri = $uri ?? $_SERVER['REQUEST_URI'] ?? ''; /**
* Handle a request
// Инициализация URL *
$url = parse_url($uri, PHP_URL_PATH); * @param string|null $uri URI (protocol://domain/foo/bar)
* @param string|null $method Method (GET, POST, PUT...)
// Сортировка массива маршрутов от большего ключа к меньшему * @param core|null $core Instence of the system core
krsort(self::$routes); *
* @return string|int|null Response
foreach (self::$routes as $key => $value) { */
// Если не записан "/" в начале, то записать public function handle(
$route_name = preg_replace('/^([^\/])/', '/$1', $key); ?string $uri = null,
$url = preg_replace('/^([^\/])/', '/$1', $url); ?string $method = null,
?core $core = new core
// Если не записан "/" в конце, то записать ): string|int|null {
$route_name = preg_replace('/([^\/])$/', '$1/', $route_name); // Declaration of the registry of routes directoies
$url = preg_replace('/([^\/])$/', '$1/', $url); $routes = [];
if (mb_stripos($route_name, $url, 0, "UTF-8") === 0 && mb_strlen($route_name, 'UTF-8') <= mb_strlen($url, 'UTF-8')) { foreach ($this->routes as $route => $data) {
// Если найден маршрут, а так же его длина не меньше длины запрошенного URL // Iteration over routes
$route = $value[$_SERVER["REQUEST_METHOD"] ?? 'GET'];
break; // Search directories of route (explode() creates empty value in array)
} preg_match_all('/(^\/$|[^\/]+)/', $route, $data['directories']);
} $routes[$route] = $data['directories'][0];
}
if (!empty($route)) {
// Если маршрут найден if (count($routes) === count($this->routes)) {
if (class_exists($controller_class = core::namespace() . '\\controllers\\' . $route['controller'])) { // Initialized the registry of routes directoies
// Если найден класс-контроллер маршрута
// Initializing default values
$controller = new $controller_class; $uri ??= $_SERVER['REQUEST_URI'] ?? '/';
$method ??= $_SERVER["REQUEST_METHOD"];
if (empty($response = $controller->{$route['method']}($_REQUEST))) {
// Если не получен ответ после обработки контроллера // Initializing of URN (/foo/bar)
$urn = parse_url(urldecode($uri), PHP_URL_PATH);
// Удаление постфикса для поиска директории
$dir = preg_replace('/' . core::controllerPostfix() . '$/i', '', $route['controller']); // Universalization of URN
$urn = self::universalize($urn);
// Отрисовка шаблона по умолчанию
$response = $controller->view($dir); // Search directories of URN (explode() creates empty value in array)
} preg_match_all('/(^\/$|[^\/]+)/', $urn, $directories);
$directories = $directories[0];
echo $response;
return; /**
} * Initialization of the route
} */
echo self::error(); // Initializing the buffer of matches of route directories with URN directories
} $matches = [];
private static function error(): ?string foreach ($directories as $i => $urn_directory) {
{ // Iteration over directories of URN
if (
class_exists($class = core::namespace() . '\\controllers\\errors' . core::controllerPostfix()) && foreach ($this->routes as $route => $data) {
method_exists($class, $method = 'error404') // Iteration over routes
) {
// Если существует контроллер ошибок и метод-обработчик ответа 404, if (isset($data[$method])) {
// то вызвать обработку ответа 404 // The request method matches the route method
return (new $class(basename($class)))->$method();
} else { // Universalization of route
// Никаких исключений не вызывать, отдать пустую страницу $route = self::universalize($route);
// Либо можно, но отображать в зависимости от включенного дебаг режима
return null; // Skipping unmatched routes based on results of previous iterations
} if (isset($matches[$route]) && $matches[$route] === false) continue;
}
} // Initializing of route directory
$route_directory = $routes[$route][$i] ?? null;
if (isset($route_directory)) {
// Initialized of route directory
if ($urn_directory === $route_directory) {
// The directory of URN is identical to the directory of route
// Writing: end of URN directories XNOR end of route directories
$matches[$route] = !(isset($directories[$_i = $i + 1]) xor isset($routes[$route][$_i]));
} else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+)$/', $route_directory) === 1) {
// The directory of route is a variable ($variable)
// Writing: end of URN directories XNOR end of route directories
$matches[$route] = !(isset($directories[$_i = $i + 1]) xor isset($routes[$route][$_i]));
} else if (
!isset($routes[$route][$i + 1])
&& preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1
) {
// The directory of route is a collector ($variable...)
// AND this is the end of route directories
// Writing
$matches[$route] = 'collector';
} else $matches[$route] = false;
} else if ($matches[$route] === 'collector') {
} else $matches[$route] = false;
}
}
}
// Finding a priority route from match results
foreach ($matches as $route => $match) if ($match !== false) break;
if ($route && !empty($data = $this->routes[$route])) {
// Route found
// Universalization of route
$route = self::universalize($route);
/**
* Initialization of route variables
*/
foreach ($routes[$route] as $i => $route_directory) {
// Iteration over directories of route
if (preg_match('/^\$([a-zA-Z_\x80-\xff]+)$/', $route_directory) === 1) {
// The directory is a variable ($variable)
// Запись в реестр переменных и перещапись директории в маршруте
$data[$method]['variables'][trim($route_directory, '$')] = $directories[$i];
} else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+\.\.\.)$/', $route_directory) === 1) {
// The directory of route is a collector ($variable...)
// Инициализаия ссылки на массив сборщика
$collector = &$data[$method]['variables'][trim($route_directory, '$.')];
// Инициализаия массива сборщика
$collector ??= [];
// Запись в реестр переменных
$collector[] = $directories[$i];
foreach (array_slice($directories, ++$i, preserve_keys: true) as &$urn_directory) {
// Перебор директорий URN
// Запись в реестр переменных
$collector[] = $urn_directory;
}
break;
}
}
/**
* Initialization of route handlers
*/
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'));
}
}
}
}
// Возврат (провал)
return $this->error($core);
}
/**
* Sorting routes
*
* 1. Short routes
* 2. Long routes
* 3. Short routes with variables (position of variables from "right" to "left")
* 4. Long routes with variables (position of variables from "right" to "left")
* 5. Short routes with collector
* 6. Long routes with collector
* 7. Short routes with variables and collector (position of variables from "right" to "left" then by amount)
* 8. Long routes with variables and collector (position of variables from "right" to "left")
*
* Добавить чтобы сначала текст потом переменная затем после переменной первыми тексты и в конце перменные опять и так рекурсивно
*
* @return static The instance from which the method was called (fluent interface)
*
* @todo ПЕРЕДЕЛАТЬ ПОЛНОСТЬЮ
*/
public function sort(): static
{
uksort($this->routes, function (string $a, string $b) {
// Sorting routes
// Initialization of string lengths (multibyte-safe)
$length = [
$a => mb_strlen($a),
$b => mb_strlen($b)
];
// Initialization of the presence of variables
$variables = [
$a => preg_match('/\$([a-zA-Z_\x80-\xff]+)(\/|$)/', $a) === 1,
$b => preg_match('/\$([a-zA-Z_\x80-\xff]+)(\/|$)/', $b) === 1
];
// Initialization of the presence of collectors
$collectors = [
$a => preg_match('/\$([a-zA-Z_\x80-\xff]+)\.\.\.$/', $a) === 1,
$b => preg_match('/\$([a-zA-Z_\x80-\xff]+)\.\.\.$/', $b) === 1
];
if ($variables[$a] && !$variables[$b]) return 1;
else if (!$variables[$a] && $variables[$b]) return -1;
else if ($variables[$a] && $variables[$b]) {
} else if ($collectors[$a] && !$collectors[$b]) return 1;
else if (!$collectors[$a] && $collectors[$b]) return -1;
else {
// NOR variables and XAND collectors (both are not found or both are found)
// The routes are the same length (no changes)
if ($length[$a] === $length[$b]) return 0;
// The longer route moves to the end
return $length[$a] > $length[$b] ? 1 : -1;
}
});
// Exit (success) (fluent interface)
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
*
* Always "/" at the beginning and never "/" at the end
*
* @param string &$urn URN (/foo/bar)
*
* @return string Universalized URN
*/
private function universalize(string $urn): string
{
// Universalization of URN and exit (success)
return (string) preg_replace('/(.+)\/$/', '$1', preg_replace('/^([^\/])/', '/$1', $urn));
}
}

View File

@@ -0,0 +1,81 @@
<?php
declare(strict_types=1);
namespace mirzaev\minimal\traits;
// Built-in libraries
use exception;
/**
* Trait of magical methods
*
* @method void __set(string $name, mixed $value = null) Write property
* @method mixed __get(string $name) Read property
* @method void __unset(string $name) Delete property
* @method bool __isset(string $name) Check property for initialization
*
* @package mirzaev\minimal\traits
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
*/
trait magic
{
/**
* Write property
*
* @param string $name Name of the property
* @param mixed $value Value of the property
*
* @return void
*/
public function __set(string $name, mixed $value = null): void
{
match ($name) {
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) {
default => throw new exception('Property "' . static::class . "::\$$name\" not found", 404)
};
}
/**
* Delete property
*
* @param string $name Name of the property
*
* @return void
*/
public function __unset(string $name): void
{
match ($name) {
default => (function () use ($name) {
unset($this->{$name});
})()
};
}
/**
* Check property for initialization
*
* @param string $name Name of the property
*
* @return bool Is the property initialized?
*/
public function __isset(string $name): bool
{
return match ($name) {
default => isset($this->{$name})
};
}
}