PHP 8.4 + MINIMAL 3.2.0 + many improvements
This commit is contained in:
parent
7f7cdced69
commit
0843fd83a5
|
@ -7,40 +7,61 @@ namespace ${REPO_OWNER}\${REPO_NAME}\controllers;
|
||||||
// Files of the project
|
// Files of the project
|
||||||
use ${REPO_OWNER}\${REPO_NAME}\views\manager,
|
use ${REPO_OWNER}\${REPO_NAME}\views\manager,
|
||||||
${REPO_OWNER}\${REPO_NAME}\models\core as models,
|
${REPO_OWNER}\${REPO_NAME}\models\core as models,
|
||||||
${REPO_OWNER}\${REPO_NAME}\models\account_model as account,
|
${REPO_OWNER}\${REPO_NAME}\models\session;
|
||||||
${REPO_OWNER}\${REPO_NAME}\models\session_model as session;
|
|
||||||
|
|
||||||
// Library for ArangoDB
|
|
||||||
use ArangoDBClient\Document as _document;
|
|
||||||
|
|
||||||
// Framework for PHP
|
// Framework for PHP
|
||||||
use mirzaev\minimal\controller;
|
use mirzaev\minimal\core as minimal,
|
||||||
|
mirzaev\minimal\controller,
|
||||||
|
mirzaev\minimal\http\response,
|
||||||
|
mirzaev\minimal\http\enumerations\status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core of controllers
|
* Controllers core
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
||||||
* @author ${REPO_OWNER} < mail >
|
*
|
||||||
|
* @param session $session Instance of the session
|
||||||
|
* @param language $language Language
|
||||||
|
* @param response $response Response
|
||||||
|
* @param array $errors Registry of errors
|
||||||
|
*
|
||||||
|
* @method void __construct(minimal $minimal, bool $initialize) Constructor
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
class core extends controller
|
class core extends controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Postfix for name of controllers files
|
* Session
|
||||||
*/
|
*
|
||||||
final public const POSTFIX = '';
|
* @var session|null $session Instance of the session
|
||||||
|
|
||||||
/**
|
|
||||||
* Instance of a session
|
|
||||||
*/
|
*/
|
||||||
protected readonly session $session;
|
protected readonly session $session;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instance of an account
|
* Language
|
||||||
|
*
|
||||||
|
* @var language $language Language
|
||||||
*/
|
*/
|
||||||
protected readonly ?account $account;
|
protected language $language = language::en;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registry of errors
|
* Response
|
||||||
|
*
|
||||||
|
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
|
||||||
|
*
|
||||||
|
* @var response $response Response
|
||||||
|
*/
|
||||||
|
protected response $response {
|
||||||
|
// Read
|
||||||
|
get => $this->response ??= $this->request->response();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Errors
|
||||||
|
*
|
||||||
|
* @var array $errors Registry of errors
|
||||||
*/
|
*/
|
||||||
protected array $errors = [
|
protected array $errors = [
|
||||||
'session' => [],
|
'session' => [],
|
||||||
|
@ -48,24 +69,25 @@ class core extends controller
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of an instance
|
* Constructor
|
||||||
*
|
*
|
||||||
|
* @param minimal $minimal Instance of the MINIMAL
|
||||||
* @param bool $initialize Initialize a controller?
|
* @param bool $initialize Initialize a controller?
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(bool $initialize = true)
|
public function __construct(minimal $minimal, bool $initialize = true)
|
||||||
{
|
{
|
||||||
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
|
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
|
||||||
if ($_SERVER['HTTP_USER_AGENT'] === 'nginx-ssl early hints') return;
|
if (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] === 'nginx-ssl early hints') return status::bruh->label;
|
||||||
|
|
||||||
// For the extends system
|
// For the extends system
|
||||||
parent::__construct($initialize);
|
parent::__construct(core: $minimal);
|
||||||
|
|
||||||
if ($initialize) {
|
if ($initialize) {
|
||||||
// Initializing is requested
|
// Requestet initializing
|
||||||
|
|
||||||
// Initializing of models core (connect to ArangoDB...)
|
// Initializing core of the models
|
||||||
new models();
|
new models();
|
||||||
|
|
||||||
// Initializing of the date until which the session will be active
|
// Initializing of the date until which the session will be active
|
||||||
|
@ -100,22 +122,4 @@ class core extends controller
|
||||||
$this->view = new templater($this->session);
|
$this->view = new templater($this->session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Check of initialization
|
|
||||||
*
|
|
||||||
* Checks whether a property is initialized in a document instance from ArangoDB
|
|
||||||
*
|
|
||||||
* @param string $name Name of the property from ArangoDB
|
|
||||||
*
|
|
||||||
* @return bool The property is initialized?
|
|
||||||
*/
|
|
||||||
public function __isset(string $name): bool
|
|
||||||
{
|
|
||||||
// Check of initialization of the property and exit (success)
|
|
||||||
return match ($name) {
|
|
||||||
default => isset($this->{$name})
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,24 +7,61 @@ namespace ${REPO_OWNER}\${REPO_NAME}\controllers;
|
||||||
// Files of the project
|
// Files of the project
|
||||||
use ${REPO_OWNER}\${REPO_NAME}\controllers\core;
|
use ${REPO_OWNER}\${REPO_NAME}\controllers\core;
|
||||||
|
|
||||||
|
// Framework for PHP
|
||||||
|
use mirzaev\minimal\http\enumerations\status;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Index controller
|
* Index
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
||||||
* @author ${REPO_OWNER} < mail >
|
*
|
||||||
|
* @param array $errors Registry of errors
|
||||||
|
*
|
||||||
|
* @method null index() Main page
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
final class index extends core
|
final class index extends core
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Render the main page
|
* Errors
|
||||||
*
|
*
|
||||||
* @param array $parameters Parameters of the request (POST + GET)
|
* @var array $errors Registry of errors
|
||||||
*/
|
*/
|
||||||
public function index(array $parameters = []): ?string
|
protected array $errors = [
|
||||||
|
'session' => []
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main page
|
||||||
|
*
|
||||||
|
* @return null
|
||||||
|
*/
|
||||||
|
public function index(): null
|
||||||
{
|
{
|
||||||
// Exit (success)
|
if (str_contains($this->request->headers['accept'], content::any->value)) {
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'GET') return $this->view->render(DIRECTORY_SEPARATOR . 'index.html');
|
// Request for any response
|
||||||
else if ($_SERVER['REQUEST_METHOD'] === 'POST') return $main;
|
|
||||||
|
// Render page
|
||||||
|
$page = $this->view->render('index.html');
|
||||||
|
|
||||||
|
// Sending response
|
||||||
|
$this->response
|
||||||
|
->start()
|
||||||
|
->clean()
|
||||||
|
->sse()
|
||||||
|
->write($page)
|
||||||
|
->validate($this->request)
|
||||||
|
?->body()
|
||||||
|
->end();
|
||||||
|
|
||||||
|
// Deinitializing rendered page
|
||||||
|
unset($page);
|
||||||
|
|
||||||
|
// Exit (success)
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -5,14 +5,15 @@ declare(strict_types=1);
|
||||||
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
||||||
|
|
||||||
// Framework for PHP
|
// Framework for PHP
|
||||||
use mirzaev\minimal\model;
|
use mirzaev\minimal\model,
|
||||||
|
mirzaev\minimal\http\enumerations\status;
|
||||||
|
|
||||||
// Framework for ArangoDB
|
// Framework for ArangoDB
|
||||||
use mirzaev\arangodb\connection as arangodb,
|
use mirzaev\arangodb\connection as arangodb,
|
||||||
mirzaev\arangodb\collection,
|
mirzaev\arangodb\collection,
|
||||||
mirzaev\arangodb\document;
|
mirzaev\arangodb\document;
|
||||||
|
|
||||||
// Libraries for ArangoDB
|
// Library for ArangoDB
|
||||||
use ArangoDBClient\Document as _document,
|
use ArangoDBClient\Document as _document,
|
||||||
ArangoDBClient\DocumentHandler as _document_handler;
|
ArangoDBClient\DocumentHandler as _document_handler;
|
||||||
|
|
||||||
|
@ -20,38 +21,40 @@ use ArangoDBClient\Document as _document,
|
||||||
use exception;
|
use exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Core of models
|
* Models core
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
* @package ${REPO_OWNER}\${REPO_NAME}\models
|
||||||
* @author ${REPO_OWNER} < mail >
|
*
|
||||||
|
* @param public ARANGODB Path to the file with ArangoDB session connection data
|
||||||
|
* @param arangodb $arangodb Instance of the ArangoDB session
|
||||||
|
*
|
||||||
|
* @method void __construct(bool $initialize, ?arangodb $arangodb) Constructor
|
||||||
|
* @method _document|static|array|null read(string $filter, string $sort, int $amount, int $page, string $return, array $parameters, array &$errors) Read document from ArangoDB
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
class core extends model
|
class core extends model
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Postfix for name of models files
|
* ArangoDB connection daa
|
||||||
|
*
|
||||||
|
* @var string ARANGODB Path to the file with ArangoDB session connection data
|
||||||
*/
|
*/
|
||||||
final public const POSTFIX = '';
|
final public const string ARANGODB = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'settings' . DIRECTORY_SEPARATOR . 'arangodb.php';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Path to the file with settings of connecting to the ArangoDB
|
* ArangoDB
|
||||||
*/
|
*
|
||||||
final public const ARANGODB = '../settings/arangodb.php';
|
* @var arangodb $arangodb Instance of the ArangoDB session
|
||||||
|
|
||||||
/**
|
|
||||||
* Instance of the session of ArangoDB
|
|
||||||
*/
|
*/
|
||||||
protected static arangodb $arangodb;
|
protected static arangodb $arangodb;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the collection in ArangoDB
|
* Constructor
|
||||||
*/
|
|
||||||
public const COLLECTION = 'THIS_COLLECTION_SHOULD_NOT_EXIST_REPLACE_IT_IN_THE_MODEL';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor of an instance
|
|
||||||
*
|
*
|
||||||
* @param bool $initialize Initialize a model?
|
* @param bool $initialize Initialize a model?
|
||||||
* @param ?arangodb $arangodb Instance of a session of ArangoDB
|
* @param ?arangodb $arangodb Instance of the ArangoDB session
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
|
@ -63,66 +66,81 @@ class core extends model
|
||||||
if ($initialize) {
|
if ($initialize) {
|
||||||
// Initializing is requested
|
// Initializing is requested
|
||||||
|
|
||||||
if (isset($arangodb)) {
|
// Writing an instance of a session of ArangoDB to the property
|
||||||
// Recieved an instance of a session of ArangoDB
|
self::$arangodb = $arangodb ?? new arangodb(require static::ARANGODB);
|
||||||
|
|
||||||
// Write an instance of a session of ArangoDB to the property
|
|
||||||
$this->__set('arangodb', $arangodb);
|
|
||||||
} else {
|
|
||||||
// Not recieved an instance of a session of ArangoDB
|
|
||||||
|
|
||||||
// Initializing of an instance of a session of ArangoDB
|
|
||||||
$this->__get('arangodb');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read from ArangoDB
|
* Read document from ArangoDB
|
||||||
*
|
*
|
||||||
* @param string $filter Expression for filtering (AQL)
|
* @param string $filter Expression for filtering (AQL)
|
||||||
* @param string $sort Expression for sorting (AQL)
|
* @param string $sort Expression for sorting (AQL)
|
||||||
* @param int $amount Amount of documents for collect
|
* @param int $amount Amount of documents for collect
|
||||||
* @param int $page Page
|
* @param int $page Page
|
||||||
* @param string $return Expression describing the parameters to return (AQL)
|
* @param string $return Expression describing the parameters to return (AQL)
|
||||||
* @param array &$errors The registry on errors
|
* @param array $parameters Binded parameters for placeholders ['placeholder' => parameter]
|
||||||
|
* @param array &$errors Registry of errors
|
||||||
*
|
*
|
||||||
* @return _document|array|null An array of instances of documents from ArangoDB, if they are found
|
* @return mixed An array of instances of documents from ArangoDB, if they are found
|
||||||
*/
|
*/
|
||||||
public static function read(
|
public static function _read(
|
||||||
string $filter = '',
|
string $filter = '',
|
||||||
string $sort = 'd.created DESC, d._key DESC',
|
string $sort = 'd.created DESC, d._key DESC',
|
||||||
int $amount = 1,
|
int $amount = 1,
|
||||||
int $page = 1,
|
int $page = 1,
|
||||||
string $return = 'd',
|
string $return = 'd',
|
||||||
|
array $parameters = [],
|
||||||
array &$errors = []
|
array &$errors = []
|
||||||
): _document|array|null {
|
): _document|static|array|null {
|
||||||
try {
|
try {
|
||||||
if (collection::init(static::$arangodb->session, static::COLLECTION)) {
|
if (collection::initialize(static::COLLECTION, static::TYPE)) {
|
||||||
// Initialized the collection
|
// Initialized the collection
|
||||||
|
|
||||||
// Read from ArangoDB and exit (success)
|
// Read from ArangoDB
|
||||||
return collection::search(
|
$result = collection::execute(
|
||||||
static::$arangodb->session,
|
|
||||||
sprintf(
|
sprintf(
|
||||||
<<<'AQL'
|
<<<'AQL'
|
||||||
FOR d IN %s
|
FOR d IN @@collection
|
||||||
%s
|
%s
|
||||||
%s
|
%s
|
||||||
LIMIT %d, %d
|
LIMIT @offset, @amount
|
||||||
RETURN %s
|
RETURN %s
|
||||||
AQL,
|
AQL,
|
||||||
static::COLLECTION,
|
|
||||||
empty($filter) ? '' : "FILTER $filter",
|
empty($filter) ? '' : "FILTER $filter",
|
||||||
empty($sort) ? '' : "SORT $sort",
|
empty($sort) ? '' : "SORT $sort",
|
||||||
--$page <= 0 ? 0 : $amount * $page,
|
empty($return) ? 'd' : $return
|
||||||
$amount,
|
),
|
||||||
$return
|
[
|
||||||
)
|
'@collection' => static::COLLECTION,
|
||||||
|
'offset' => --$page <= 0 ? 0 : $page * $amount,
|
||||||
|
'amount' => $amount
|
||||||
|
] + $parameters,
|
||||||
|
errors: $errors
|
||||||
);
|
);
|
||||||
} else throw new exception('Failed to initialize the collection');
|
|
||||||
|
if ($amount === 1 && $result instanceof _document) {
|
||||||
|
// Received only 1 document and @todo rebuild
|
||||||
|
|
||||||
|
// Initializing the object
|
||||||
|
$object = new static;
|
||||||
|
|
||||||
|
if (method_exists($object, '__document')) {
|
||||||
|
// Object can implement a document from ArangoDB
|
||||||
|
|
||||||
|
// Writing the instance of document from ArangoDB to the implement object
|
||||||
|
$object->__document($result);
|
||||||
|
|
||||||
|
// Exit (success)
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit (success)
|
||||||
|
return $result;
|
||||||
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
} catch (exception $e) {
|
} catch (exception $e) {
|
||||||
// Write to the registry of errors
|
// Writing to registry of errors
|
||||||
$errors[] = [
|
$errors[] = [
|
||||||
'text' => $e->getMessage(),
|
'text' => $e->getMessage(),
|
||||||
'file' => $e->getFile(),
|
'file' => $e->getFile(),
|
||||||
|
@ -135,50 +153,6 @@ class core extends model
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete from ArangoDB
|
|
||||||
*
|
|
||||||
* @param _document $instance Instance of the document from ArangoDB
|
|
||||||
* @param array &$errors The registry on errors
|
|
||||||
*
|
|
||||||
* @return bool Deleted from ArangoDB without errors?
|
|
||||||
*/
|
|
||||||
public static function delete(_document $instance, array &$errors = []): bool
|
|
||||||
{
|
|
||||||
try {
|
|
||||||
if (collection::init(static::$arangodb->session, static::COLLECTION)) {
|
|
||||||
// Initialized the collection
|
|
||||||
|
|
||||||
// Delete from ArangoDB and exit (success)
|
|
||||||
return (new _document_handler(static::$arangodb->session))->remove($instance);
|
|
||||||
} else throw new exception('Failed to initialize the collection');
|
|
||||||
} catch (exception $e) {
|
|
||||||
// Write to the registry of errors
|
|
||||||
$errors[] = [
|
|
||||||
'text' => $e->getMessage(),
|
|
||||||
'file' => $e->getFile(),
|
|
||||||
'line' => $e->getLine(),
|
|
||||||
'stack' => $e->getTrace()
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit (fail)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update in ArangoDB
|
|
||||||
*
|
|
||||||
* @param _document $instance Instance of the document from ArangoDB
|
|
||||||
*
|
|
||||||
* @return bool Writed to ArangoDB without errors?
|
|
||||||
*/
|
|
||||||
public static function update(_document $instance): bool
|
|
||||||
{
|
|
||||||
// Update in ArangoDB and exit (success)
|
|
||||||
return document::update(static::$arangodb->session, $instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write
|
* Write
|
||||||
*
|
*
|
||||||
|
@ -191,26 +165,9 @@ class core extends model
|
||||||
{
|
{
|
||||||
match ($name) {
|
match ($name) {
|
||||||
'arangodb' => (function () use ($value) {
|
'arangodb' => (function () use ($value) {
|
||||||
if ($this->__isset('arangodb')) {
|
if (isset(static::$arangodb)) throw new exception('Forbidden to reinitialize the ArangoDB session($this::$arangodb)', status::internal_server_error->value);
|
||||||
// Is alredy initialized
|
else if ($value instanceof arangodb) self::$arangodb = $value;
|
||||||
|
else throw new exception('Session of connection to ArangoDB ($this::$arangodb) is need to be mirzaev\arangodb\connection', status::internal_server_error->value);
|
||||||
// Exit (fail)
|
|
||||||
throw new exception('Forbidden to reinitialize the session of ArangoDB ($this::$arangodb)', 500);
|
|
||||||
} else {
|
|
||||||
// Is not already initialized
|
|
||||||
|
|
||||||
if ($value instanceof arangodb) {
|
|
||||||
// Recieved an appropriate value
|
|
||||||
|
|
||||||
// Write the property and exit (success)
|
|
||||||
self::$arangodb = $value;
|
|
||||||
} else {
|
|
||||||
// Recieved an inappropriate value
|
|
||||||
|
|
||||||
// Exit (fail)
|
|
||||||
throw new exception('Session of ArangoDB ($this::$arangodb) is need to be mirzaev\arangodb\connection', 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})(),
|
})(),
|
||||||
default => parent::__set($name, $value)
|
default => parent::__set($name, $value)
|
||||||
};
|
};
|
||||||
|
@ -226,22 +183,6 @@ class core extends model
|
||||||
public function __get(string $name): mixed
|
public function __get(string $name): mixed
|
||||||
{
|
{
|
||||||
return match ($name) {
|
return match ($name) {
|
||||||
'arangodb' => (function () {
|
|
||||||
try {
|
|
||||||
if (!$this->__isset('arangodb')) {
|
|
||||||
// Is not initialized
|
|
||||||
|
|
||||||
// Initializing of a default value from settings
|
|
||||||
$this->__set('arangodb', new arangodb(require static::ARANGODB));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit (success)
|
|
||||||
return self::$arangodb;
|
|
||||||
} catch (exception) {
|
|
||||||
// Exit (fail)
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
})(),
|
|
||||||
default => parent::__get($name)
|
default => parent::__get($name)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -280,7 +221,7 @@ class core extends model
|
||||||
*/
|
*/
|
||||||
public static function __callStatic(string $name, array $arguments): mixed
|
public static function __callStatic(string $name, array $arguments): mixed
|
||||||
{
|
{
|
||||||
match ($name) {
|
return match ($name) {
|
||||||
'arangodb' => (new static)->__get('arangodb'),
|
'arangodb' => (new static)->__get('arangodb'),
|
||||||
default => throw new exception("Not found: $name", 500)
|
default => throw new exception("Not found: $name", 500)
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\enumerations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Language
|
||||||
|
*
|
||||||
|
* Types of languages by ISO 639-1 standart
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\enumerations
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
enum language
|
||||||
|
{
|
||||||
|
case en;
|
||||||
|
case ru;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Label
|
||||||
|
*
|
||||||
|
* Initialize label of the language
|
||||||
|
*
|
||||||
|
* @param language|null $language Language into which to translate
|
||||||
|
*
|
||||||
|
* @return string Translated label of the language
|
||||||
|
*
|
||||||
|
* @todo
|
||||||
|
* 1. More languages
|
||||||
|
* 2. Cases???
|
||||||
|
*/
|
||||||
|
public function label(?language $language = language::en): string
|
||||||
|
{
|
||||||
|
// Exit (success)
|
||||||
|
return match ($this) {
|
||||||
|
language::en => match ($language) {
|
||||||
|
language::en => 'English',
|
||||||
|
language::ru => 'Английский'
|
||||||
|
},
|
||||||
|
language::ru => match ($language) {
|
||||||
|
language::en => 'Russian',
|
||||||
|
language::ru => 'Русский'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\enumerations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session
|
||||||
|
*
|
||||||
|
* Types of session verification
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\enumerations
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
enum session
|
||||||
|
{
|
||||||
|
case hash_only;
|
||||||
|
case hash_else_address;
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\interfaces;
|
||||||
|
|
||||||
|
// Framework for ArangoDB
|
||||||
|
use mirzaev\arangodb\enumerations\collection\type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection
|
||||||
|
*
|
||||||
|
* Interface for implementing a collection from ArangoDB
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\interfaces
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
interface collection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Name of the collection in ArangoDB
|
||||||
|
*/
|
||||||
|
public const string COLLECTION = 'THIS_COLLECTION_SHOULD_NOT_EXIST';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type of the collection in ArangoDB
|
||||||
|
*/
|
||||||
|
public const type TYPE = type::document;
|
||||||
|
}
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\interfaces;
|
||||||
|
|
||||||
|
// Library для ArangoDB
|
||||||
|
use ArangoDBClient\Document as _document;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Document
|
||||||
|
*
|
||||||
|
* Interface for implementing a document instance from ArangoDB
|
||||||
|
*
|
||||||
|
* @param _document $document An instance of the ArangoDB document from ArangoDB (protected readonly)
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\interfaces
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
interface document
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Write
|
||||||
|
*
|
||||||
|
* Write a property into an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
* @param mixed $value Content of the property
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, mixed $value = null): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read
|
||||||
|
*
|
||||||
|
* Read a property from an instance of the ArangoDB docuemnt
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return mixed Content of the property
|
||||||
|
*/
|
||||||
|
public function __get(string $name): mixed;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete
|
||||||
|
*
|
||||||
|
* Deinitialize the property in an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __unset(string $name): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check of initialization
|
||||||
|
*
|
||||||
|
* Check of initialization of the property into an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return bool The property is initialized?
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a method
|
||||||
|
*
|
||||||
|
* Execute a method from an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the method
|
||||||
|
* @param array $arguments Arguments for the method
|
||||||
|
*
|
||||||
|
* @return mixed Result of execution of the method
|
||||||
|
*/
|
||||||
|
public function __call(string $name, array $arguments = []): mixed;
|
||||||
|
}
|
|
@ -5,8 +5,13 @@ declare(strict_types=1);
|
||||||
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
||||||
|
|
||||||
// Files of the project
|
// Files of the project
|
||||||
use mirzaev\ebala\models\account,
|
use ${REPO_OWNER}\${REPO_NAME}\models\traits\status,
|
||||||
mirzaev\ebala\models\traits\status;
|
${REPO_OWNER}\${REPO_NAME}\models\traits\buffer,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\enumerations\session as verification,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
|
||||||
|
|
||||||
// Framework for ArangoDB
|
// Framework for ArangoDB
|
||||||
use mirzaev\arangodb\collection,
|
use mirzaev\arangodb\collection,
|
||||||
|
@ -19,81 +24,113 @@ use ArangoDBClient\Document as _document;
|
||||||
use exception;
|
use exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model of session
|
* Session model
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
* @package ${REPO_OWNER}\${REPO_NAME}\models
|
||||||
* @author ${REPO_OWNER} < mail >
|
*
|
||||||
|
* @param string COLLECTION Name of the collection in ArangoDB
|
||||||
|
* @param verification VERIFICATION Type of session verification
|
||||||
|
*
|
||||||
|
* @method void __construct(?string $hash, ?int $expires, array &$errors) Constructor
|
||||||
|
* @method document|null hash(string $hash, array &$errors) Search by hash
|
||||||
|
* @method document|null address(string $address, array &$errors) Search by IP-address
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
final class session extends core
|
final class session extends core implements document_interface, collection_interface
|
||||||
{
|
{
|
||||||
/**
|
use status, document_trait, buffer, cart {
|
||||||
* Name of the collection in ArangoDB
|
buffer::write as write;
|
||||||
*/
|
cart::initialize as cart;
|
||||||
final public const COLLECTION = 'session';
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An instance of the ArangoDB document from ArangoDB
|
* Collection name
|
||||||
*/
|
|
||||||
protected readonly _document $document;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor of an instance
|
|
||||||
*
|
*
|
||||||
* Initialize of a session and write them to the $this->document property
|
* @var string COLLECTION Name of the collection in ArangoDB
|
||||||
|
*/
|
||||||
|
final public const string COLLECTION = 'session';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session verification type
|
||||||
|
*
|
||||||
|
* @var verification VERIFICATION Type of session verification
|
||||||
|
*/
|
||||||
|
final public const verification VERIFICATION = verification::hash_else_address;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* Initialize session and write into the $this->document property
|
||||||
*
|
*
|
||||||
* @param ?string $hash Hash of the session in ArangoDB
|
* @param ?string $hash Hash of the session in ArangoDB
|
||||||
* @param ?int $expires Date of expiring of the session (used for creating a new session)
|
* @param ?int $expires Date of expiring of the session (used for creating a new session)
|
||||||
* @param array &$errors Registry of errors
|
* @param array &$errors Registry of errors
|
||||||
*
|
*
|
||||||
* @return static instance of the ArangoDB document of session
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(?string $hash = null, ?int $expires = null, array &$errors = [])
|
public function __construct(?string $hash = null, ?int $expires = null, array &$errors = [])
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (collection::init(static::$arangodb->session, self::COLLECTION)) {
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
|
||||||
// Initialized the collection
|
// Initialized the collection
|
||||||
|
|
||||||
if ($this->search($hash, $errors)) {
|
if (isset($hash) && $document = $this->hash($hash, errors: $errors)) {
|
||||||
// Found an instance of the ArangoDB document of session and received a session hash
|
// Found the instance of the ArangoDB document of session and received a session hash
|
||||||
|
|
||||||
|
// Writing document instance of the session from ArangoDB to the property of the implementing object
|
||||||
|
$this->__document($document);
|
||||||
|
} else if (static::VERIFICATION === verification::hash_else_address && $document = $this->address($_SERVER['REMOTE_ADDR'], errors: $errors)) {
|
||||||
|
// Found the instance of the ArangoDB document of session and received a session hash
|
||||||
|
|
||||||
|
// Writing document instance of the session from ArangoDB to the property of the implementing object
|
||||||
|
$this->__document($document);
|
||||||
} else {
|
} else {
|
||||||
// Not found an instance of the ArangoDB document of session
|
// Not found the instance of the ArangoDB document of session
|
||||||
|
|
||||||
// Initializing a new session and write they into ArangoDB
|
// Initializing a new session and write they into ArangoDB
|
||||||
$_id = document::write($this::$arangodb->session, self::COLLECTION, [
|
$_id = document::write(
|
||||||
'active' => true,
|
static::COLLECTION,
|
||||||
'expires' => $expires ?? time() + 604800,
|
[
|
||||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
'active' => true,
|
||||||
'x-forwarded-for' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
|
'expires' => $expires ?? time() + 604800,
|
||||||
'referer' => $_SERVER['HTTP_REFERER'] ?? null,
|
'address' => $_SERVER['REMOTE_ADDR'],
|
||||||
'useragent' => $_SERVER['HTTP_USER_AGENT'] ?? null
|
'x-forwarded-for' => $_SERVER['HTTP_X_FORWARDED_FOR'] ?? null,
|
||||||
]);
|
'referer' => $_SERVER['HTTP_REFERER'] ?? null,
|
||||||
|
'useragent' => $_SERVER['HTTP_USER_AGENT'] ?? null
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
if ($session = collection::search($this::$arangodb->session, sprintf(
|
if ($session = collection::execute(
|
||||||
<<<AQL
|
<<<'AQL'
|
||||||
FOR d IN %s
|
FOR d IN @@collection
|
||||||
FILTER d._id == '%s' && d.expires > %d && d.active == true
|
FILTER d._id == @_id && d.expires > @time && d.active == true
|
||||||
RETURN d
|
RETURN d
|
||||||
AQL,
|
AQL,
|
||||||
self::COLLECTION,
|
[
|
||||||
$_id,
|
'@collection' => static::COLLECTION,
|
||||||
time()
|
'_id' => $_id,
|
||||||
))) {
|
'time' => time()
|
||||||
// Found an instance of just created new session
|
],
|
||||||
|
errors: $errors
|
||||||
|
)) {
|
||||||
|
// Found the instance of just created new session
|
||||||
|
|
||||||
// Generate a hash and write into an instance of the ArangoDB document of session property
|
// Generating a hash and write into the instance of the ArangoDB document of session property
|
||||||
$session->hash = sodium_bin2hex(sodium_crypto_generichash($_id));
|
$session->hash = sodium_bin2hex(sodium_crypto_generichash($_id));
|
||||||
|
|
||||||
if (document::update($this::$arangodb->session, $session)) {
|
if (document::update($session, errors: $errors)) {
|
||||||
// Is writed update
|
// Writed to ArangoDB
|
||||||
|
|
||||||
// Write instance of the ArangoDB document of session into property and exit (success)
|
// Writing instance of the session document from ArangoDB to the property of the implementing object
|
||||||
$this->document = $session;
|
$this->__document($session);
|
||||||
} else throw new exception('Could not write the session data');
|
} else throw new exception('Failed to write the session data');
|
||||||
} else throw new exception('Could not create or find just created session');
|
} else throw new exception('Failed to create or find just created session');
|
||||||
}
|
}
|
||||||
} else throw new exception('Could not initialize the collection');
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
} catch (exception $e) {
|
} catch (exception $e) {
|
||||||
// Write to the registry of errors
|
// Writing to the registry of errors
|
||||||
$errors[] = [
|
$errors[] = [
|
||||||
'text' => $e->getMessage(),
|
'text' => $e->getMessage(),
|
||||||
'file' => $e->getFile(),
|
'file' => $e->getFile(),
|
||||||
|
@ -104,45 +141,38 @@ final class session extends core
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search
|
* Search by hash
|
||||||
*
|
*
|
||||||
* Search for the session in ArangoDB by hash and write they into $this->document property if they are found
|
* Search for the session in ArangoDB by hash
|
||||||
*
|
*
|
||||||
* @param ?string $hash Hash of the session in ArangoDB
|
* @param string $hash Hash of the session in ArangoDB
|
||||||
* @param array &$errors Registry of errors
|
* @param array &$errors Registry of errors
|
||||||
*
|
*
|
||||||
* @return static instance of the ArangoDB document of session
|
* @return _document|null instance of document of the session in ArangoDB
|
||||||
*/
|
*/
|
||||||
public function search(?string $hash, array &$errors = []): bool
|
public static function hash(string $hash, array &$errors = []): ?_document
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (isset($hash)) {
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
|
||||||
// Recieved a hash
|
// Collection initialized
|
||||||
|
|
||||||
// Search the session data in ArangoDB
|
// Search the session data in ArangoDB
|
||||||
$_document = $session = collection::search($this::$arangodb->session, sprintf(
|
return collection::execute(
|
||||||
<<<AQL
|
<<<'AQL'
|
||||||
FOR d IN %s
|
FOR d IN @@collection
|
||||||
FILTER d.hash == '%s' && d.expires > %d && d.active == true
|
FILTER d.hash == @hash && d.expires > @time && d.active == true
|
||||||
RETURN d
|
RETURN d
|
||||||
AQL,
|
AQL,
|
||||||
self::COLLECTION,
|
[
|
||||||
$hash,
|
'@collection' => static::COLLECTION,
|
||||||
time()
|
'hash' => $hash,
|
||||||
));
|
'time' => time()
|
||||||
|
],
|
||||||
if ($_document instanceof _document) {
|
errors: $errors
|
||||||
// An instance of the ArangoDB document of session is found
|
);
|
||||||
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
// Write the session data to the property
|
|
||||||
$this->document = $_document;
|
|
||||||
|
|
||||||
// Exit (success)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (exception $e) {
|
} catch (exception $e) {
|
||||||
// Write to the registry of errors
|
// Writing to the registry of errors
|
||||||
$errors[] = [
|
$errors[] = [
|
||||||
'text' => $e->getMessage(),
|
'text' => $e->getMessage(),
|
||||||
'file' => $e->getFile(),
|
'file' => $e->getFile(),
|
||||||
|
@ -152,37 +182,44 @@ final class session extends core
|
||||||
}
|
}
|
||||||
|
|
||||||
// Exit (fail)
|
// Exit (fail)
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Write to buffer of the session
|
* Search by IP-address
|
||||||
*
|
*
|
||||||
* @param array $data Data for merging
|
* Search for the session in ArangoDB by IP-address
|
||||||
|
*
|
||||||
|
* @param string $address IP-address writed to the session in ArangoDB
|
||||||
* @param array &$errors Registry of errors
|
* @param array &$errors Registry of errors
|
||||||
*
|
*
|
||||||
* @return bool Is data has written into the session buffer?
|
* @return _document|null instance of document of the session in ArangoDB
|
||||||
*/
|
*/
|
||||||
public function write(array $data, array &$errors = []): bool
|
public static function address(string $address, array &$errors = []): ?_document
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
if (collection::init($this::$arangodb->session, self::COLLECTION)) {
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
|
||||||
// Initialized the collection
|
// Collection initialized
|
||||||
|
|
||||||
// An instance of the ArangoDB document of session is initialized?
|
// Search the session data in ArangoDB
|
||||||
if (!isset($this->document)) throw new exception('An instance of the ArangoDB document of session is not initialized');
|
return collection::execute(
|
||||||
|
<<<'AQL'
|
||||||
// Write data into buffwer of an instance of the ArangoDB document of session
|
FOR d IN @@collection
|
||||||
$this->document->buffer = array_replace_recursive(
|
FILTER d.address == @address && d.expires > @time && d.active == true
|
||||||
$this->document->buffer ?? [],
|
SORT d.updated DESC
|
||||||
[$_SERVER['INTERFACE'] => array_replace_recursive($this->document->buffer[$_SERVER['INTERFACE']] ?? [], $data)]
|
LIMIT 1
|
||||||
|
RETURN d
|
||||||
|
AQL,
|
||||||
|
[
|
||||||
|
'@collection' => static::COLLECTION,
|
||||||
|
'address' => $address,
|
||||||
|
'time' => time()
|
||||||
|
],
|
||||||
|
errors: $errors
|
||||||
);
|
);
|
||||||
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
// Write to ArangoDB and exit (success)
|
|
||||||
return document::update($this::$arangodb->session, $this->document) ? true : throw new exception('Не удалось записать данные в буфер сессии');
|
|
||||||
} else throw new exception('Could not initialize the collection');
|
|
||||||
} catch (exception $e) {
|
} catch (exception $e) {
|
||||||
// Write to the registry of errors
|
// Writing to the registry of errors
|
||||||
$errors[] = [
|
$errors[] = [
|
||||||
'text' => $e->getMessage(),
|
'text' => $e->getMessage(),
|
||||||
'file' => $e->getFile(),
|
'file' => $e->getFile(),
|
||||||
|
@ -191,86 +228,6 @@ final class session extends core
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
// Exit (fail)
|
||||||
}
|
return null;
|
||||||
|
}}
|
||||||
/**
|
|
||||||
* Write
|
|
||||||
*
|
|
||||||
* Write a property into an instance of the ArangoDB document
|
|
||||||
*
|
|
||||||
* @param string $name Name of the property
|
|
||||||
* @param mixed $value Content of the property
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __set(string $name, mixed $value = null): void
|
|
||||||
{
|
|
||||||
// Write to the property into an instance of the ArangoDB document and exit (success)
|
|
||||||
$this->document->{$name} = $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read
|
|
||||||
*
|
|
||||||
* Read a property from an instance of the ArangoDB docuemnt
|
|
||||||
*
|
|
||||||
* @param string $name Name of the property
|
|
||||||
*
|
|
||||||
* @return mixed Content of the property
|
|
||||||
*/
|
|
||||||
public function __get(string $name): mixed
|
|
||||||
{
|
|
||||||
// Read a property from an instance of the ArangoDB document and exit (success)
|
|
||||||
return match ($name) {
|
|
||||||
'arangodb' => $this::$arangodb,
|
|
||||||
default => $this->document->{$name}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete
|
|
||||||
*
|
|
||||||
* Deinitialize the property in an instance of the ArangoDB document
|
|
||||||
*
|
|
||||||
* @param string $name Name of the property
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __unset(string $name): void
|
|
||||||
{
|
|
||||||
// Delete the property in an instance of the ArangoDB document and exit (success)
|
|
||||||
unset($this->document->{$name});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check of initialization
|
|
||||||
*
|
|
||||||
* Check of initialization of the property into an instance of the ArangoDB document
|
|
||||||
*
|
|
||||||
* @param string $name Name of the property
|
|
||||||
*
|
|
||||||
* @return bool The property is initialized?
|
|
||||||
*/
|
|
||||||
public function __isset(string $name): bool
|
|
||||||
{
|
|
||||||
// Check of initializatio nof the property and exit (success)
|
|
||||||
return isset($this->document->{$name});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute a method
|
|
||||||
*
|
|
||||||
* Execute a method from an instance of the ArangoDB document
|
|
||||||
*
|
|
||||||
* @param string $name Name of the method
|
|
||||||
* @param array $arguments Arguments for the method
|
|
||||||
*
|
|
||||||
* @return mixed Result of execution of the method
|
|
||||||
*/
|
|
||||||
public function __call(string $name, array $arguments = []): mixed
|
|
||||||
{
|
|
||||||
// Execute the method and exit (success)
|
|
||||||
if (method_exists($this->document, $name)) return $this->document->{$name}($arguments);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,84 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
||||||
|
|
||||||
|
// Files of the project
|
||||||
|
use ${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
|
||||||
|
|
||||||
|
// Library for ArangoDB
|
||||||
|
use ArangoDBClient\Document as _document;
|
||||||
|
|
||||||
|
// Framework for ArangoDB
|
||||||
|
use mirzaev\arangodb\collection,
|
||||||
|
mirzaev\arangodb\document;
|
||||||
|
|
||||||
|
// Built-in libraries
|
||||||
|
use exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Buffer
|
||||||
|
*
|
||||||
|
* Storage of data in the document from ArangoDB
|
||||||
|
*
|
||||||
|
* @uses document
|
||||||
|
* @uses document_interface
|
||||||
|
* @uses collection_interface
|
||||||
|
*
|
||||||
|
* @param static COLLECTION Name of the collection in ArangoDB
|
||||||
|
* @param static TYPE Type of the collection in ArangoDB
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
trait buffer
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Write to buffer of the document
|
||||||
|
*
|
||||||
|
* @param array $data Data for writing (merge)
|
||||||
|
* @param array &$errors Registry of errors
|
||||||
|
*
|
||||||
|
* @return bool Is data has written into the document from ArangoDB?
|
||||||
|
*/
|
||||||
|
public function write(array $data, array &$errors = []): bool
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
|
||||||
|
// Initialized the collection
|
||||||
|
|
||||||
|
// Is the instance of the document from ArangoDB are initialized?
|
||||||
|
if (!isset($this->document)) throw new exception('The instance of the sessoin document from ArangoDB is not initialized');
|
||||||
|
|
||||||
|
// Writing data into buffer of the instance of the document from ArangoDB
|
||||||
|
$this->document->buffer = array_replace_recursive($this->document->buffer ?? [], $data);
|
||||||
|
|
||||||
|
// Is the buffer of the instance of the document from ArangoDB exceed 10 megabytes?
|
||||||
|
if (mb_strlen(json_encode($this->document->buffer)) > 10485760) throw new exception('The buffer size exceeds 10 megabytes');
|
||||||
|
|
||||||
|
// Serializing parameters
|
||||||
|
if ($this->document->language instanceof language) $this->document->language = $this->document->language->name;
|
||||||
|
|
||||||
|
// Writing to ArangoDB and exit (success)
|
||||||
|
return document::update($this->document, errors: $errors);
|
||||||
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
|
} catch (exception $e) {
|
||||||
|
// Writing to the registry of errors
|
||||||
|
$errors[] = [
|
||||||
|
'text' => $e->getMessage(),
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
'line' => $e->getLine(),
|
||||||
|
'stack' => $e->getTrace()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit (fail)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,206 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
||||||
|
|
||||||
|
// Files of the project
|
||||||
|
use ${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\collection as collection_interface,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\connect;
|
||||||
|
|
||||||
|
// Library для ArangoDB
|
||||||
|
use ArangoDBClient\Document as _document;
|
||||||
|
|
||||||
|
// Framework for ArangoDB
|
||||||
|
use mirzaev\arangodb\connection as arangodb,
|
||||||
|
mirzaev\arangodb\document as framework_document,
|
||||||
|
mirzaev\arangodb\collection;
|
||||||
|
|
||||||
|
// Built-in libraries
|
||||||
|
use exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Trait for implementing a document instance from ArangoDB
|
||||||
|
*
|
||||||
|
* @uses document_interface
|
||||||
|
*
|
||||||
|
* @var protected readonly _document|null $document An instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
trait document
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Document
|
||||||
|
*
|
||||||
|
* @var _document $document An instance of the document from ArangoDB
|
||||||
|
*/
|
||||||
|
protected readonly _document $document;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*
|
||||||
|
* @param bool $initialize Initialize a model?
|
||||||
|
* @param ?arangodb $arangodb Instance of a session of ArangoDB
|
||||||
|
* @param _document|null|false $document An instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(
|
||||||
|
bool $initialize = true,
|
||||||
|
?arangodb $arangodb = null,
|
||||||
|
_document|null|false $document = false
|
||||||
|
) {
|
||||||
|
// For the extends system
|
||||||
|
parent::__construct($initialize, $arangodb);
|
||||||
|
|
||||||
|
// Writing to the property
|
||||||
|
if ($document instanceof _document) $this->__document($document);
|
||||||
|
else if ($document === null) throw new exception('Failed to initialize an instance of the document from ArangoDB');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write or read document
|
||||||
|
*
|
||||||
|
* @param _document|null $document Instance of document from ArangoDB
|
||||||
|
*
|
||||||
|
* @return _document|null Instance of document from ArangoDB
|
||||||
|
*/
|
||||||
|
public function __document(?_document $document = null): ?_document
|
||||||
|
{
|
||||||
|
// Writing a property storing a document instance to ArangoDB
|
||||||
|
if ($document) $this->document ??= $document;
|
||||||
|
|
||||||
|
// Read a property storing a document instance to ArangoDB and exit (success)
|
||||||
|
return $this->document ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connect
|
||||||
|
*
|
||||||
|
* @param collecton_interface $document Document
|
||||||
|
* @param array &$errors Registry of errors
|
||||||
|
*
|
||||||
|
* @return string|null The identifier of the created edge of the "connect" collection, if created
|
||||||
|
*/
|
||||||
|
public function connect(collection_interface $document, array &$errors = []): ?string
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (collection::initialize(static::COLLECTION, static::TYPE, errors: $errors)) {
|
||||||
|
if (collection::initialize(connect::COLLECTION, connect::TYPE, errors: $errors)) {
|
||||||
|
if (collection::initialize($document::COLLECTION, $document::TYPE, errors: $errors)) {
|
||||||
|
// Initialized collections
|
||||||
|
|
||||||
|
if ($this->document instanceof _document) {
|
||||||
|
// Initialized instance of the document from ArangoDB
|
||||||
|
|
||||||
|
// Writing document and exit (success)
|
||||||
|
return framework_document::write(
|
||||||
|
connect::COLLECTION,
|
||||||
|
[
|
||||||
|
'_from' => $document->getId(),
|
||||||
|
'_to' => $this->document->getId()
|
||||||
|
],
|
||||||
|
errors: $errors
|
||||||
|
);
|
||||||
|
} else throw new exception('The instance of the document from ArangoDB is not initialized');
|
||||||
|
} else throw new exception('Failed to initialize ' . $document::TYPE . ' collection: ' . $document::COLLECTION);
|
||||||
|
} else throw new exception('Failed to initialize ' . connect::TYPE . ' collection: ' . connect::COLLECTION);
|
||||||
|
} else throw new exception('Failed to initialize ' . static::TYPE . ' collection: ' . static::COLLECTION);
|
||||||
|
} catch (exception $e) {
|
||||||
|
// Writing to the registry of errors
|
||||||
|
$errors[] = [
|
||||||
|
'text' => $e->getMessage(),
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
'line' => $e->getLine(),
|
||||||
|
'stack' => $e->getTrace()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit (fail)
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write
|
||||||
|
*
|
||||||
|
* Write a property into an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
* @param mixed $value Content of the property
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, mixed $value = null): void
|
||||||
|
{
|
||||||
|
// Writing to the property into an instance of the ArangoDB document and exit (success)
|
||||||
|
$this->document->{$name} = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read
|
||||||
|
*
|
||||||
|
* Read a property from an instance of the ArangoDB docuemnt
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return mixed Content of the property
|
||||||
|
*/
|
||||||
|
public function __get(string $name): mixed
|
||||||
|
{
|
||||||
|
// Read a property from an instance of the ArangoDB document and exit (success)
|
||||||
|
return match ($name) {
|
||||||
|
default => $this->document->{$name}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete
|
||||||
|
*
|
||||||
|
* Deinitialize the property in an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __unset(string $name): void
|
||||||
|
{
|
||||||
|
// Delete the property in an instance of the ArangoDB document and exit (success)
|
||||||
|
unset($this->document->{$name});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check of initialization
|
||||||
|
*
|
||||||
|
* Check of initialization of the property into an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the property
|
||||||
|
*
|
||||||
|
* @return bool The property is initialized?
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool
|
||||||
|
{
|
||||||
|
// Check of initializatio nof the property and exit (success)
|
||||||
|
return isset($this->document->{$name});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute a method
|
||||||
|
*
|
||||||
|
* Execute a method from an instance of the ArangoDB document
|
||||||
|
*
|
||||||
|
* @param string $name Name of the method
|
||||||
|
* @param array $arguments Arguments for the method
|
||||||
|
*
|
||||||
|
* @return mixed Result of execution of the method
|
||||||
|
*/
|
||||||
|
public function __call(string $name, array $arguments = []): mixed
|
||||||
|
{
|
||||||
|
// Execute the method and exit (success)
|
||||||
|
return method_exists($this->document, $name) ? $this->document->{$name}($arguments) ?? null : null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
||||||
|
|
||||||
|
// Built-in libraries
|
||||||
|
use exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Files
|
||||||
|
*
|
||||||
|
* Trait with files handlers
|
||||||
|
*
|
||||||
|
* @method static void delete(string $directory, array &$errors)
|
||||||
|
*
|
||||||
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
|
*/
|
||||||
|
trait files
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Delete
|
||||||
|
*
|
||||||
|
* Delete files recursively
|
||||||
|
*
|
||||||
|
* @param string $directory Directory
|
||||||
|
* @param array &$errors Registry of errors
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private static function delete(string $directory, array &$errors = []): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (file_exists($directory)) {
|
||||||
|
// Directory exists
|
||||||
|
|
||||||
|
// Deleting descendant files and directories (enter to the recursion)
|
||||||
|
foreach (scandir($directory) as $file) {
|
||||||
|
if ($file === '.' || $file === '..') continue;
|
||||||
|
else if (is_dir("$directory/$file")) static::delete("$directory/$file", $errors);
|
||||||
|
else unlink("$directory/$file");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deleting the directory
|
||||||
|
rmdir($directory);
|
||||||
|
|
||||||
|
// Exit (success)
|
||||||
|
return;
|
||||||
|
} else throw new exception('Directory does not exist');
|
||||||
|
} catch (exception $e) {
|
||||||
|
// Writing to the registry of errors
|
||||||
|
$errors[] = [
|
||||||
|
'text' => $e->getMessage(),
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
'line' => $e->getLine(),
|
||||||
|
'stack' => $e->getTrace()
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit (fail)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,24 +4,38 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
||||||
|
|
||||||
|
// Files of the project
|
||||||
|
use ${REPO_OWNER}\${REPO_NAME}\models\traits\document as document_trait,
|
||||||
|
${REPO_OWNER}\${REPO_NAME}\models\interfaces\document as document_interface;
|
||||||
|
|
||||||
// Built-in libraries
|
// Built-in libraries
|
||||||
use exception;
|
use exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait fo initialization of a status
|
* Status (DUMB SHIT)
|
||||||
|
*
|
||||||
|
* Trait for initialization of a status
|
||||||
|
*
|
||||||
|
* @uses document_trait
|
||||||
|
* @uses document_interface
|
||||||
|
*
|
||||||
|
* @method bool|null status(array &$errors) Check document by its status
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
||||||
*
|
*
|
||||||
* @author ${REPO_OWNER} < mail >
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
trait status
|
trait status
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Initialize of a status
|
* Status
|
||||||
|
*
|
||||||
|
* Check document by its status
|
||||||
*
|
*
|
||||||
* @param array &$errors Registry of errors
|
* @param array &$errors Registry of errors
|
||||||
*
|
*
|
||||||
* @return ?bool Status, if they are found
|
* @return ?bool Status, if found
|
||||||
*/
|
*/
|
||||||
public function status(array &$errors = []): ?bool
|
public function status(array &$errors = []): ?bool
|
||||||
{
|
{
|
||||||
|
@ -29,7 +43,7 @@ trait status
|
||||||
// Read from ArangoDB and exit (success)
|
// Read from ArangoDB and exit (success)
|
||||||
return $this->document->active ?? false;
|
return $this->document->active ?? false;
|
||||||
} catch (exception $e) {
|
} catch (exception $e) {
|
||||||
// Write to the registry of errors
|
// Writing to the registry of errors
|
||||||
$errors[] = [
|
$errors[] = [
|
||||||
'text' => $e->getMessage(),
|
'text' => $e->getMessage(),
|
||||||
'file' => $e->getFile(),
|
'file' => $e->getFile(),
|
||||||
|
|
|
@ -18,7 +18,6 @@ body {
|
||||||
background-color: var(--background, #fafafa);
|
background-color: var(--background, #fafafa);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
aside {
|
aside {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,29 +4,40 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ${REPO_OWNER}\${REPO_NAME};
|
namespace ${REPO_OWNER}\${REPO_NAME};
|
||||||
|
|
||||||
use mirzaev\minimal\core;
|
// Framework for PHP
|
||||||
use mirzaev\minimal\router;
|
use mirzaev\minimal\core,
|
||||||
|
mirzaev\minimal\route;
|
||||||
|
|
||||||
|
// Enabling debugging
|
||||||
/* ini_set('error_reporting', E_ALL);
|
/* ini_set('error_reporting', E_ALL);
|
||||||
ini_set('display_errors', 1);
|
ini_set('display_errors', 1);
|
||||||
ini_set('display_startup_errors', 1); */
|
ini_set('display_startup_errors', 1); */
|
||||||
|
|
||||||
|
// Initializing path to the public directory
|
||||||
|
define('PUBLIC', __DIR__);
|
||||||
|
|
||||||
|
// Initializing path to the project root directory
|
||||||
|
define('ROOT', INDEX . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
|
||||||
|
|
||||||
|
// Initializing path to the directory of views
|
||||||
define('VIEWS', realpath('..' . DIRECTORY_SEPARATOR . 'views'));
|
define('VIEWS', realpath('..' . DIRECTORY_SEPARATOR . 'views'));
|
||||||
|
|
||||||
|
// Initializing path to the directory of the storage
|
||||||
define('STORAGE', realpath('..' . DIRECTORY_SEPARATOR . 'storage'));
|
define('STORAGE', realpath('..' . DIRECTORY_SEPARATOR . 'storage'));
|
||||||
define('INDEX', __DIR__);
|
|
||||||
|
|
||||||
// Автозагрузка
|
// Initializing default theme for the views templater
|
||||||
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
define('THEME', 'default');
|
||||||
|
|
||||||
// Инициализация маршрутизатора
|
// Initializing dependencies
|
||||||
$router = new router;
|
require ROOT . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
|
||||||
|
|
||||||
// Запись маршрутов
|
// Initializing core
|
||||||
$router->write('/', 'index', 'index');
|
$core = new core(namespace: __NAMESPACE__);
|
||||||
|
|
||||||
// Инициализация ядра
|
// Initializing routes
|
||||||
$core = new core(namespace: __NAMESPACE__, router: $router);
|
$router->router
|
||||||
|
->write('/', new route('index', 'index'), 'GET')
|
||||||
// Обработка запроса
|
;
|
||||||
echo $core->start();
|
|
||||||
|
|
||||||
|
// Handling request
|
||||||
|
$core->start();
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace ${REPO_OWNER}\${REPO_NAME}\views;
|
||||||
|
|
||||||
// Files of the project
|
// Files of the project
|
||||||
use ${REPO_OWNER}\${REPO_NAME}\models\session,
|
use ${REPO_OWNER}\${REPO_NAME}\models\session,
|
||||||
${REPO_OWNER}\${REPO_NAME}\models\account;
|
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
|
||||||
|
|
||||||
// Framework for PHP
|
// Framework for PHP
|
||||||
use mirzaev\minimal\controller;
|
use mirzaev\minimal\controller;
|
||||||
|
@ -15,61 +15,72 @@ use mirzaev\minimal\controller;
|
||||||
use Twig\Loader\FilesystemLoader,
|
use Twig\Loader\FilesystemLoader,
|
||||||
Twig\Environment as twig,
|
Twig\Environment as twig,
|
||||||
Twig\Extra\Intl\IntlExtension as intl,
|
Twig\Extra\Intl\IntlExtension as intl,
|
||||||
Twig\TwigFilter;
|
Twig\TwigFilter,
|
||||||
|
Twig\TwigFunction;
|
||||||
|
|
||||||
// Built-in libraries
|
// Built-in libraries
|
||||||
use ArrayAccess;
|
use ArrayAccess as array_access,
|
||||||
|
Error as error;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Templater core
|
* Templater
|
||||||
*
|
*
|
||||||
* @package ${REPO_OWNER}\${REPO_NAME}\views
|
* @package ${REPO_OWNER}\${REPO_NAME}\views
|
||||||
* @author ${REPO_OWNER} < mail >
|
*
|
||||||
|
* @param twig $twig Instance of the twig templater
|
||||||
|
* @param array $variables Registry of view global variables
|
||||||
|
*
|
||||||
|
* @method void __construct(?session &$session) Constructor
|
||||||
|
* @method string|null render(string $file, ?array $variables) Render the HTML-document
|
||||||
|
*
|
||||||
|
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
|
||||||
|
* @author ${REPO_OWNER} <mail@domain.zone>
|
||||||
*/
|
*/
|
||||||
final class templater extends controller implements ArrayAccess
|
final class templater extends controller implements array_access
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Registry of global variables of view
|
* Twig
|
||||||
*/
|
*
|
||||||
public array $variables = [];
|
* @var twig $twig Instance of the twig templater
|
||||||
|
|
||||||
/**
|
|
||||||
* Instance of twig templater
|
|
||||||
*/
|
*/
|
||||||
readonly public twig $twig;
|
readonly public twig $twig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Variables
|
||||||
|
*
|
||||||
|
* @var array $variables Registry of view global variables
|
||||||
|
*/
|
||||||
|
public array $variables = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor of an instance
|
* Constructor of an instance
|
||||||
*
|
*
|
||||||
* @param ?session $session Instance of the session of ArangoDB
|
* @param ?session $session Instance of the session in ArangoDB
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function __construct(?session &$session = null): void
|
public function __construct(?session &$session = null)
|
||||||
{
|
{
|
||||||
// Initializing of an instance of twig
|
// Initializing the Twig instance
|
||||||
$this->twig = new twig(new FilesystemLoader(VIEWS));
|
$this->twig = new twig(new FilesystemLoader(VIEWS));
|
||||||
|
|
||||||
// Initializing of global variables
|
// Initializing global variables
|
||||||
$this->twig->addGlobal('theme', 'default');
|
$this->twig->addGlobal('theme', 'default');
|
||||||
$this->twig->addGlobal('server', $_SERVER);
|
$this->twig->addGlobal('server', $_SERVER);
|
||||||
$this->twig->addGlobal('cookies', $_COOKIE);
|
$this->twig->addGlobal('cookies', $_COOKIE);
|
||||||
if (!empty($session->status())) {
|
if (!empty($session->status())) $this->twig->addGlobal('session', $session);
|
||||||
$this->twig->addGlobal('session', $session);
|
$this->twig->addGlobal('language', $language = $session?->buffer['language'] ?? language::en);
|
||||||
}
|
|
||||||
|
|
||||||
// Initializing of twig extensions
|
|
||||||
$this->twig->addExtension(new intl());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Render a HTML-document
|
* Render
|
||||||
|
*
|
||||||
|
* Render the HTML-document
|
||||||
*
|
*
|
||||||
* @param string $file Related path to a HTML-document
|
* @param string $file Related path to a HTML-document
|
||||||
* @param ?array $variables Registry of variables to push into registry of global variables
|
* @param ?array $variables Registry of variables to push into registry of global variables
|
||||||
*
|
*
|
||||||
* @return ?string HTML-документ
|
* @return ?string HTML-document
|
||||||
*/
|
*/
|
||||||
public function render(string $file, ?array $variables = null): ?string
|
public function render(string $file, ?array $variables = null): ?string
|
||||||
{
|
{
|
||||||
|
@ -80,7 +91,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Write
|
* Write
|
||||||
*
|
*
|
||||||
* Write a variable into registry of global variables
|
* Write the variable into the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param string $name Name of the variable
|
* @param string $name Name of the variable
|
||||||
* @param mixed $value Value of the variable
|
* @param mixed $value Value of the variable
|
||||||
|
@ -96,7 +107,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Read
|
* Read
|
||||||
*
|
*
|
||||||
* Read a variable from registry of global variables
|
* Read the variable from the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param string $name Name of the variable
|
* @param string $name Name of the variable
|
||||||
*
|
*
|
||||||
|
@ -111,7 +122,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Delete
|
* Delete
|
||||||
*
|
*
|
||||||
* Delete a variable from the registry of global variables
|
* Delete the variable from the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param string $name Name of the variable
|
* @param string $name Name of the variable
|
||||||
*
|
*
|
||||||
|
@ -126,7 +137,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Check of initialization
|
* Check of initialization
|
||||||
*
|
*
|
||||||
* Check of initialization in registry of global variables
|
* Check of initialization in the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param string $name Name of the variable
|
* @param string $name Name of the variable
|
||||||
*
|
*
|
||||||
|
@ -141,7 +152,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Write
|
* Write
|
||||||
*
|
*
|
||||||
* Write a variable into registry of global variables
|
* Write the variable into the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param mixed $name Name of an offset of the variable
|
* @param mixed $name Name of an offset of the variable
|
||||||
* @param mixed $value Value of the variable
|
* @param mixed $value Value of the variable
|
||||||
|
@ -157,7 +168,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Read
|
* Read
|
||||||
*
|
*
|
||||||
* Read a variable from registry of global variables
|
* Read the variable from the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param mixed $name Name of the variable
|
* @param mixed $name Name of the variable
|
||||||
*
|
*
|
||||||
|
@ -172,7 +183,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Delete
|
* Delete
|
||||||
*
|
*
|
||||||
* Delete a variable from the registry of global variables
|
* Delete the variable from the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param mixed $name Name of the variable
|
* @param mixed $name Name of the variable
|
||||||
*
|
*
|
||||||
|
@ -187,7 +198,7 @@ final class templater extends controller implements ArrayAccess
|
||||||
/**
|
/**
|
||||||
* Check of initialization
|
* Check of initialization
|
||||||
*
|
*
|
||||||
* Check of initialization in registry of global variables
|
* Check of initialization in the registry of the view global variables
|
||||||
*
|
*
|
||||||
* @param mixed $name Name of the variable
|
* @param mixed $name Name of the variable
|
||||||
*
|
*
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
"description": "${REPO_DESCRIPTION}",
|
"description": "${REPO_DESCRIPTION}",
|
||||||
"homepage": "https://git.mirzaev.sexy${REPO_LINK}",
|
"homepage": "https://git.mirzaev.sexy${REPO_LINK}",
|
||||||
"type": "site",
|
"type": "site",
|
||||||
"keywords": [],
|
"keywords": [
|
||||||
|
"minimal"
|
||||||
|
],
|
||||||
"readme": "README.md",
|
"readme": "README.md",
|
||||||
"license": "WTFPL",
|
"license": "WTFPL",
|
||||||
"authors": [
|
"authors": [
|
||||||
|
@ -15,17 +17,17 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"support": {
|
"support": {
|
||||||
"docs": "https://git.mirzaev.sexy${REPO_LINK}/manual",
|
"wiki": "https://git.mirzaev.sexy${REPO_LINK}/wiki",
|
||||||
"issues": "https://git.mirzaev.sexy${REPO_LINK}/issues"
|
"issues": "https://git.mirzaev.sexy${REPO_LINK}/issues"
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"php": "~8.3",
|
"php": "^8.4",
|
||||||
"ext-sodium": "~8.3",
|
"triagens/arangodb": "^3.8",
|
||||||
"mirzaev/minimal": "^2.2.0",
|
"mirzaev/minimal": "^3.2.0",
|
||||||
"mirzaev/accounts": "~1.2.x-dev",
|
"mirzaev/arangodb": "^1.3",
|
||||||
"mirzaev/arangodb": "^1.0.0",
|
"twig/twig": "^3.10",
|
||||||
"triagens/arangodb": "~3.9.x-dev",
|
"twig/extra-bundle": "^3.7",
|
||||||
"twig/twig": "^3.4"
|
"twig/intl-extra": "^3.10"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "~9.5"
|
"phpunit/phpunit": "~9.5"
|
||||||
|
|
Loading…
Reference in New Issue