14 Commits
1.0.0 ... 2.3.1

Author SHA1 Message Date
9011ccf557 beauty 2024-10-11 10:45:56 +03:00
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
10 changed files with 3176 additions and 3108 deletions

0
.gitignore vendored Normal file → Executable file
View File

11
LICENSE Normal file
View File

@@ -0,0 +1,11 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.

54
README.md Normal file
View File

@@ -0,0 +1,54 @@
🤟 The MINIMAL framework that does **not limit your project with its own rules**, has **no dependencies**, implements the **best practices** of popular MVC-frameworks, it **VERY fast** and **optimized** for all the innovations in **PHP 8.2**
Can be configured to work with **any database** (`core::$session`) and **any HTML template engine** (`$this->view`)
*personally, i prefer **ArangoDB** and **Twig***
## Nearest plans (first half of 2025)
1. Add **middlewares** technology
2. Route sorting in the router: `router::sort()`
3. Add trigger routes from within routes
4. Think about adding asynchronous executions
5. Write an article describing the principles of the framework
## Installation
Execute: `composer require mirzaev/minimal`
## Usage
*index.php*
```
// Initializing the router
$router = new router;
// Initializing of routes
$router
->write('/', 'catalog', 'index', 'GET')
->write('/search', 'catalog', 'search', 'POST')
->write('/session/connect/telegram', 'session', 'telegram', 'POST')
->write('/product/$id', 'catalog', 'product', 'POST')
->write('/$categories...', 'catalog', 'index', 'POST'); // Collector (since 0.3.0)
// Initializing the core
$core = new core(namespace: __NAMESPACE__, router: $router, controller: new controller(false), model: new model(false));
// Handle the request
echo $core->start();
```
## Examples of projects based on MINIMAL
### ebala (⚠VERY HUGE)
Repository: https://git.mirzaev.sexy/mirzaev/ebala
Github mirror: https://github.com/mature-woman/ebala
**I earned more than a million rubles from this project**
**Repositories *may* be closed at the request of the customer**
### notchat
Repository: https://git.mirzaev.sexy/mirzaev/notchat
Github mirror: https://github.com/mature-woman/notchat
**P2P chat project with different blockchains and smart stuff**
### site-repression
Link: https://repression.mirzaev.sexy
Repository: https://git.mirzaev.sexy/mirzaev/site-repression
Github mirror: https://github.com/mature-woman/site-repression
**A simple site for my article about *political repressions in Russia* and my *abduction by Wagner PMC operatives* from my home**

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

273
mirzaev/minimal/system/controller.php Normal file → Executable file
View File

@@ -1,179 +1,94 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
use mirzaev\minimal\model; // Files of the project
use mirzaev\minimal\model,
use Exception; mirzaev\minimal\traits\magic;
/** // Встроенные библиотеки
* Контроллер use exception;
*
* @package mirzaev\minimal /**
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> * Controller (base)
*/ *
class controller * @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>
*/ */
private string $postfix = '_controller'; class controller
{
/** use magic;
* Модель
*/ /**
protected model $model; * Postfix of file names
*/
/** public const string POSTFIX = '_controller';
* Шаблонизатор представления
*/ /**
protected object $view; * Instance of the model connected in the router
*/
/** protected model $model;
* Конструктор
* /**
* @return void * View template engine instance (twig)
*/ */
public function __construct() protected object $view;
{
} /**
* Constructor
/** */
* Записать свойство public function __construct() {}
*
* @param string $name Название /**
* @param mixed $value Значение * Write property
*/ *
public function __set(string $name, mixed $value = null): void * @param string $name Name of the property
{ * @param mixed $value Value of the property
match ($name) { *
'model' => (function () use ($value) { * @return void
if (isset($this->model)) { */
// Свойство уже было инициализировано public function __set(string $name, mixed $value = null): void
{
// Выброс исключения (неудача) match ($name) {
throw new exception('Запрещено реинициализировать модель ($this->model)', 500); 'model' => (function () use ($value) {
} else { if ($this->__isset('model')) throw new exception('Can not reinitialize property: ' . static::class . '::$model', 500);
// Свойство ещё не было инициализировано else {
// Property not initialized
if ($value instanceof model) {
// Передано подходящее значение if (is_object($value)) $this->model = $value;
else throw new exception('Property "' . static::class . '::view" should store an instance of a model', 500);
// Запись свойства (успех) }
$this->model = $value; })(),
} else { 'view' => (function () use ($value) {
// Передано неподходящее значение if ($this->__isset('view')) throw new exception('Can not reinitialize property: ' . static::class . '::$view', 500);
else {
// Выброс исключения (неудача) // Property not initialized
throw new exception('Модель ($this->model) должна хранить инстанцию "mirzaev\minimal\model"', 500);
} 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);
})(), }
'view' => (function () use ($value) { })(),
if (isset($this->view)) { default => throw new exception('Property "' . static::class . "::\$$name\" not found", 404)
// Свойство уже было инициализировано };
}
// Выброс исключения (неудача)
throw new exception('Запрещено реинициализировать шаблонизатор представления ($this->view)', 500); /**
} else { * Read property
// Свойство ещё не было инициализировано *
* @param string $name Name of the property
if (is_object($value)) { *
// Передано подходящее значение * @return mixed Value of the property
*/
// Запись свойства (успех) public function __get(string $name): mixed
$this->view = $value; {
} else { 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)
throw new exception('Шаблонизатор представлений ($this->view) должен хранить объект', 500); };
} }
} }
})(),
'postfix' => (function () use ($value) {
if (isset($this->postfix)) {
// Свойство уже было инициализировано
// Выброс исключения (неудача)
throw new exception('Запрещено реинициализировать постфикс ($this->postfix)', 500);
} else {
// Свойство ещё не было инициализировано
if ($value = filter_var($value, FILTER_SANITIZE_STRING)) {
// Передано подходящее значение
// Запись свойства (успех)
$this->postfix = $value;
} else {
// Передано неподходящее значение
// Выброс исключения (неудача)
throw new exception('Постфикс ($this->postfix) должен быть строкой', 500);
}
}
})(),
default => throw new exception("Свойство \"\$$name\" не найдено", 404)
};
}
/**
* Прочитать свойство
*
* Записывает значение по умолчанию, если свойство не инициализировано
*
* @param string $name Название
*
* @return mixed Содержимое
*/
public function __get(string $name): mixed
{
return match ($name) {
'postfix' => (function () {
if (isset($this->postfix)) {
// Свойство уже было инициализировано
} else {
// Свойство ещё не было инициализировано
// Инициализация со значением по умолчанию
$this->__set('postfix', '_controller');
}
// Возврат (успех)
return $this->postfix;
})(),
'view' => $this->view ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500),
'model' => $this->model ?? throw new exception("Свойство \"\$$name\" не инициализировано", 500),
default => throw new exception("Свойство \"\$$name\" не обнаружено", 404)
};
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*/
public function __isset(string $name): bool
{
return match ($name) {
default => isset($this->{$name})
};
}
/**
* Удалить свойство
*
* @param string $name Название
*/
public function __unset(string $name): void
{
match ($name) {
default => (function () use ($name) {
// Удаление
unset($this->{$name});
})()
};
}
}

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

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

146
mirzaev/minimal/system/model.php Normal file → Executable file
View File

@@ -1,112 +1,34 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
use Exception; // Files of the project
use mirzaev\minimal\traits\magic;
/**
* Модель // Built-in libraries
* use exception;
* @package mirzaev\minimal
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> /**
*/ * Model (base)
class model *
{ * @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>
private string $postfix = '_model'; */
class model
/** {
* Записать свойство use magic;
*
* @param string $name Название /**
* @param mixed $value Значение * Postfix of file names
*/ */
public function __set(string $name, mixed $value = null): void public const string POSTFIX = '_model';
{
match ($name) { /**
'postfix' => (function () use ($value) { * Constructor
if (isset($this->postfix)) { */
// Свойство уже было инициализировано public function __construct() {}
}
// Выброс исключения (неудача)
throw new exception('Запрещено реинициализировать постфикс ($this->postfix)', 500);
} else {
// Свойство ещё не было инициализировано
if ($value = filter_var($value, FILTER_SANITIZE_STRING)) {
// Передано подходящее значение
// Запись свойства (успех)
$this->postfix = $value;
} else {
// Передано неподходящее значение
// Выброс исключения (неудача)
throw new exception('Постфикс ($this->postfix) должен быть строкой', 500);
}
}
})(),
default => throw new exception("Свойство \"\$$name\" не найдено", 404)
};
}
/**
* Прочитать свойство
*
* Записывает значение по умолчанию, если свойство не инициализировано
*
* @param string $name Название
*
* @return mixed Содержимое
*/
public function __get(string $name): mixed
{
return match ($name) {
'postfix' => (function() {
if (isset($this->postfix)) {
// Свойство уже было инициализировано
} else {
// Свойство ещё не было инициализировано
// Инициализация со значением по умолчанию
$this->__set('postfix', '_model');
}
// Возврат (успех)
return $this->postfix;
})(),
default => throw new exception("Свойство \"\$$name\" не обнаружено", 404)
};
}
/**
* Проверить свойство на инициализированность
*
* @param string $name Название
*/
public function __isset(string $name): bool
{
return match ($name) {
default => isset($this->{$name})
};
}
/**
* Удалить свойство
*
* @param string $name Название
*/
public function __unset(string $name): void
{
match ($name) {
default => (function () use ($name) {
// Удаление
unset($this->{$name});
})()
};
}
}

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

@@ -1,137 +1,318 @@
<?php <?php
declare(strict_types=1); declare(strict_types=1);
namespace mirzaev\minimal; namespace mirzaev\minimal;
use mirzaev\minimal\core; // Файлы проекта
use mirzaev\minimal\core;
use ReflectionClass;
/**
/** * Маршрутизатор
* Маршрутизатор *
* * @package mirzaev\minimal
* @package mirzaev\shop *
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy> * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* * @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
* @todo */
* 1. Доработать обработку ошибок final class router
* 2. Добавить __set(), __get(), __isset() и __unset() {
*/ /**
final class router * @var array $router Реестр маршрутов
{ */
/** /* protected array $routes = []; */
* @var array $router Маршруты public array $routes = [];
*/
public array $routes = []; /**
* Записать маршрут
/** *
* Записать маршрут * @param string $route Маршрут
* * @param string $handler Обработчик - инстанции контроллера и модели (не обязательно), без постфиксов
* @param string $route Маршрут * @param ?string $method Вызываемый метод в инстанции контроллера обработчика
* @param string $target Обработчик (контроллер и модель, без постфиксов) * @param ?string $request HTTP-метод запроса (GET, POST, PUT...)
* @param string|null $method Метод * @param ?string $model Инстанция модели (переопределение инстанции модели в $target)
* @param string|null $type Тип * @param array $variables
* @param string|null $model Модель *
*/ * @return static The instance from which the method was called (fluent interface)
public function write(string $route, string $target, string $method = null, string $type = 'GET', string $model = null): void */
{ public function write(
// Запись в реестр string $route,
$this->routes[$route][$type] = [ string $handler,
'target' => $target, ?string $method = 'index',
'method' => $method ?? '__construct' ?string $request = 'GET',
]; ?string $model = null,
} array $variables = []
): static {
/** // Запись в реестр
* Обработать маршрут $this->routes[$route][$request] = [
* 'controller' => $handler,
* @param string $route Маршрут 'model' => $model ?? $handler,
*/ 'method' => $method,
public function handle(string $uri = null, core $core = null): ?string 'variables' => $variables
{ ];
// Запись полученного URI или из данных веб-сервера
$uri = $uri ?? $_SERVER['REQUEST_URI'] ?? ''; // Exit (success) (fluent interface)
return $this;
// Инициализация URL }
$url = parse_url($uri, PHP_URL_PATH);
/**
// Сортировка массива маршрутов от большего ключа к меньшему * Handle a request
krsort($this->routes); *
* @param string|null $uri URI (protocol://domain/foo/bar)
foreach ($this->routes as $key => $value) { * @param string|null $method Method (GET, POST, PUT...)
// Перебор маршрутов * @param core|null $core Instence of the system core
*
// Если не записан "/" в начале, то записать * @return string|int|null Response
$route_name = preg_replace('/^([^\/])/', '/$1', $key); */
$url = preg_replace('/^([^\/])/', '/$1', $url); public function handle(
?string $uri = null,
// Если не записан "/" в конце, то записать ?string $method = null,
$route_name = preg_replace('/([^\/])$/', '$1/', $route_name); ?core $core = new core
$url = preg_replace('/([^\/])$/', '$1/', $url); ): string|int|null {
// Declaration of the registry of routes directoies
if (mb_stripos($route_name, $url, 0, "UTF-8") === 0 && mb_strlen($route_name, 'UTF-8') <= mb_strlen($url, 'UTF-8')) { $routes = [];
// Найден маршрут, а так же его длина не меньше длины запрошенного URL
foreach ($this->routes as $route => $data) {
// Инициализация маршрута // Iteration over routes
$route = $value[$_SERVER["REQUEST_METHOD"] ?? 'GET'];
// Search directories of route (explode() creates empty value in array)
// Выход из цикла (успех) preg_match_all('/(^\/$|[^\/]+)/', $route, $data['directories']);
break; $routes[$route] = $data['directories'][0];
} }
}
if (count($routes) === count($this->routes)) {
if (!empty($route)) { // Initialized the registry of routes directoies
// Найден маршрут
// Initializing default values
if (class_exists($controller = ($core->namespace ?? (new core)->namespace) . '\\controllers\\' . $route['target'] . $core->controller->postfix ?? (new core())->controller->postfix)) { $uri ??= $_SERVER['REQUEST_URI'] ?? '/';
// Найден контроллер $method ??= $_SERVER["REQUEST_METHOD"];
// Инициализация контроллера // Initializing of URN (/foo/bar)
$controller = new $controller; $urn = parse_url(urldecode($uri), PHP_URL_PATH);
if (class_exists($model = ($core->namespace ?? (new core)->namespace) . '\\models\\' . $route['target'] . $core->model->postfix ?? (new core())->model->postfix)) { // Universalization of URN
// Найдена модель $urn = self::universalize($urn);
// Инициализация модели // Search directories of URN (explode() creates empty value in array)
$controller->model = new $model; preg_match_all('/(^\/$|[^\/]+)/', $urn, $directories);
} $directories = $directories[0];
if (empty($response = $controller->{$route['method']}($_REQUEST))) { /**
// Не удалось получить ответ после обработки контроллера * Initialization of the route
*/
// Возврат (неудача)
return $this->error($core); // Initializing the buffer of matches of route directories with URN directories
} $matches = [];
// Возврат (успех) foreach ($directories as $i => $urn_directory) {
return $response; // Iteration over directories of URN
}
} foreach ($this->routes as $route => $data) {
// Iteration over routes
// Возврат (неудача)
return $this->error($core); if (isset($data[$method])) {
} // The request method matches the route method
/** // Universalization of route
* Контроллер ошибок $route = self::universalize($route);
*/
private function error(core $core = null): ?string // Skipping unmatched routes based on results of previous iterations
{ if (isset($matches[$route]) && $matches[$route] === false) continue;
if (
class_exists($class = (new ReflectionClass(core::class))->getNamespaceName() . '\\controllers\\errors' . $core->controller->postfix ?? (new core())->controller->postfix) && // Initializing of route directory
method_exists($class, $method = 'error404') $route_directory = $routes[$route][$i] ?? null;
) {
// Существует контроллер ошибок и метод для обработки ошибки if (isset($route_directory)) {
// Initialized of route directory
// Возврат (вызов метода для обработки ошибки)
return (new $class(basename($class)))->$method(); if ($urn_directory === $route_directory) {
} else { // 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]));
// либо вызвать, но отображать в зависимости от включенного дебаг режима !!!!!!!!!!!!!!!!!!!! см. @todo } else if (preg_match('/^\$([a-zA-Z_\x80-\xff]+)$/', $route_directory) === 1) {
return null; // 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})
};
}
}