added installer
This commit is contained in:
289
author/project/system/models/core.php
Normal file
289
author/project/system/models/core.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
||||
|
||||
// Framework for PHP
|
||||
use mirzaev\minimal\model;
|
||||
|
||||
// Framework for ArangoDB
|
||||
use mirzaev\arangodb\connection as arangodb,
|
||||
mirzaev\arangodb\collection,
|
||||
mirzaev\arangodb\document;
|
||||
|
||||
// Libraries for ArangoDB
|
||||
use ArangoDBClient\Document as _document,
|
||||
ArangoDBClient\DocumentHandler as _document_handler;
|
||||
|
||||
// Built-in libraries
|
||||
use exception;
|
||||
|
||||
/**
|
||||
* Core of models
|
||||
*
|
||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
||||
* @author ${REPO_OWNER} < mail >
|
||||
*/
|
||||
class core extends model
|
||||
{
|
||||
/**
|
||||
* Postfix for name of models files
|
||||
*/
|
||||
final public const POSTFIX = '';
|
||||
|
||||
/**
|
||||
* Path to the file with settings of connecting to the ArangoDB
|
||||
*/
|
||||
final public const ARANGODB = '../settings/arangodb.php';
|
||||
|
||||
/**
|
||||
* Instance of the session of ArangoDB
|
||||
*/
|
||||
protected static arangodb $arangodb;
|
||||
|
||||
/**
|
||||
* Name of the collection in ArangoDB
|
||||
*/
|
||||
public const COLLECTION = 'THIS_COLLECTION_SHOULD_NOT_EXIST_REPLACE_IT_IN_THE_MODEL';
|
||||
|
||||
/**
|
||||
* Constructor of an instance
|
||||
*
|
||||
* @param bool $initialize Initialize a model?
|
||||
* @param ?arangodb $arangodb Instance of a session of ArangoDB
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(bool $initialize = true, ?arangodb $arangodb = null)
|
||||
{
|
||||
// For the extends system
|
||||
parent::__construct($initialize);
|
||||
|
||||
if ($initialize) {
|
||||
// Initializing is requested
|
||||
|
||||
if (isset($arangodb)) {
|
||||
// Recieved an instance of a session of 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
|
||||
*
|
||||
* @param string $filter Expression for filtering (AQL)
|
||||
* @param string $sort Expression for sorting (AQL)
|
||||
* @param int $amount Amount of documents for collect
|
||||
* @param int $page Page
|
||||
* @param string $return Expression describing the parameters to return (AQL)
|
||||
* @param array &$errors The registry on errors
|
||||
*
|
||||
* @return _document|array|null An array of instances of documents from ArangoDB, if they are found
|
||||
*/
|
||||
public static function read(
|
||||
string $filter = '',
|
||||
string $sort = 'd.created DESC, d._key DESC',
|
||||
int $amount = 1,
|
||||
int $page = 1,
|
||||
string $return = 'd',
|
||||
array &$errors = []
|
||||
): _document|array|null {
|
||||
try {
|
||||
if (collection::init(static::$arangodb->session, static::COLLECTION)) {
|
||||
// Initialized the collection
|
||||
|
||||
// Read from ArangoDB and exit (success)
|
||||
return collection::search(
|
||||
static::$arangodb->session,
|
||||
sprintf(
|
||||
<<<'AQL'
|
||||
FOR d IN %s
|
||||
%s
|
||||
%s
|
||||
LIMIT %d, %d
|
||||
RETURN %s
|
||||
AQL,
|
||||
static::COLLECTION,
|
||||
empty($filter) ? '' : "FILTER $filter",
|
||||
empty($sort) ? '' : "SORT $sort",
|
||||
--$page <= 0 ? 0 : $amount * $page,
|
||||
$amount,
|
||||
$return
|
||||
)
|
||||
);
|
||||
} 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 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
|
||||
*
|
||||
* @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) {
|
||||
'arangodb' => (function () use ($value) {
|
||||
if ($this->__isset('arangodb')) {
|
||||
// Is alredy initialized
|
||||
|
||||
// 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)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Read
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*
|
||||
* @return mixed Content of the property, if they are found
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
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)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __unset(string $name): void
|
||||
{
|
||||
// Deleting a property and exit (success)
|
||||
parent::__unset($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check of initialization
|
||||
*
|
||||
* @param string $name Name of the property
|
||||
*
|
||||
* @return bool The property is initialized?
|
||||
*/
|
||||
public function __isset(string $name): bool
|
||||
{
|
||||
// Check of initialization of the property and exit (success)
|
||||
return parent::__isset($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a static property or method
|
||||
*
|
||||
* @param string $name Name of the property or the method
|
||||
* @param array $arguments Arguments for the method
|
||||
*/
|
||||
public static function __callStatic(string $name, array $arguments): mixed
|
||||
{
|
||||
match ($name) {
|
||||
'arangodb' => (new static)->__get('arangodb'),
|
||||
default => throw new exception("Not found: $name", 500)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
276
author/project/system/models/session.php
Normal file
276
author/project/system/models/session.php
Normal file
@@ -0,0 +1,276 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ${REPO_OWNER}\${REPO_NAME}\models;
|
||||
|
||||
// Files of the project
|
||||
use mirzaev\ebala\models\account,
|
||||
mirzaev\ebala\models\traits\status;
|
||||
|
||||
// Framework for ArangoDB
|
||||
use mirzaev\arangodb\collection,
|
||||
mirzaev\arangodb\document;
|
||||
|
||||
// Library для ArangoDB
|
||||
use ArangoDBClient\Document as _document;
|
||||
|
||||
// Built-in libraries
|
||||
use exception;
|
||||
|
||||
/**
|
||||
* Model of session
|
||||
*
|
||||
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
||||
* @author ${REPO_OWNER} < mail >
|
||||
*/
|
||||
final class session extends core
|
||||
{
|
||||
/**
|
||||
* Name of the collection in ArangoDB
|
||||
*/
|
||||
final public const COLLECTION = 'session';
|
||||
|
||||
/**
|
||||
* An instance of the ArangoDB document from ArangoDB
|
||||
*/
|
||||
protected readonly _document $document;
|
||||
|
||||
/**
|
||||
* Constructor of an instance
|
||||
*
|
||||
* Initialize of a session and write them to the $this->document property
|
||||
*
|
||||
* @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 array &$errors Registry of errors
|
||||
*
|
||||
* @return static instance of the ArangoDB document of session
|
||||
*/
|
||||
public function __construct(?string $hash = null, ?int $expires = null, array &$errors = [])
|
||||
{
|
||||
try {
|
||||
if (collection::init(static::$arangodb->session, self::COLLECTION)) {
|
||||
// Initialized the collection
|
||||
|
||||
if ($this->search($hash, $errors)) {
|
||||
// Found an instance of the ArangoDB document of session and received a session hash
|
||||
} else {
|
||||
// Not found an instance of the ArangoDB document of session
|
||||
|
||||
// Initializing a new session and write they into ArangoDB
|
||||
$_id = document::write($this::$arangodb->session, self::COLLECTION, [
|
||||
'active' => true,
|
||||
'expires' => $expires ?? time() + 604800,
|
||||
'ip' => $_SERVER['REMOTE_ADDR'],
|
||||
'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(
|
||||
<<<AQL
|
||||
FOR d IN %s
|
||||
FILTER d._id == '%s' && d.expires > %d && d.active == true
|
||||
RETURN d
|
||||
AQL,
|
||||
self::COLLECTION,
|
||||
$_id,
|
||||
time()
|
||||
))) {
|
||||
// Found an instance of just created new session
|
||||
|
||||
// Generate a hash and write into an instance of the ArangoDB document of session property
|
||||
$session->hash = sodium_bin2hex(sodium_crypto_generichash($_id));
|
||||
|
||||
if (document::update($this::$arangodb->session, $session)) {
|
||||
// Is writed update
|
||||
|
||||
// Write instance of the ArangoDB document of session into property and exit (success)
|
||||
$this->document = $session;
|
||||
} else throw new exception('Could not write the session data');
|
||||
} else throw new exception('Could not create or find just created session');
|
||||
}
|
||||
} else throw new exception('Could not 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()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* Search for the session in ArangoDB by hash and write they into $this->document property if they are found
|
||||
*
|
||||
* @param ?string $hash Hash of the session in ArangoDB
|
||||
* @param array &$errors Registry of errors
|
||||
*
|
||||
* @return static instance of the ArangoDB document of session
|
||||
*/
|
||||
public function search(?string $hash, array &$errors = []): bool
|
||||
{
|
||||
try {
|
||||
if (isset($hash)) {
|
||||
// Recieved a hash
|
||||
|
||||
// Search the session data in ArangoDB
|
||||
$_document = $session = collection::search($this::$arangodb->session, sprintf(
|
||||
<<<AQL
|
||||
FOR d IN %s
|
||||
FILTER d.hash == '%s' && d.expires > %d && d.active == true
|
||||
RETURN d
|
||||
AQL,
|
||||
self::COLLECTION,
|
||||
$hash,
|
||||
time()
|
||||
));
|
||||
|
||||
if ($_document instanceof _document) {
|
||||
// An instance of the ArangoDB document of session is found
|
||||
|
||||
// Write the session data to the property
|
||||
$this->document = $_document;
|
||||
|
||||
// Exit (success)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to buffer of the session
|
||||
*
|
||||
* @param array $data Data for merging
|
||||
* @param array &$errors Registry of errors
|
||||
*
|
||||
* @return bool Is data has written into the session buffer?
|
||||
*/
|
||||
public function write(array $data, array &$errors = []): bool
|
||||
{
|
||||
try {
|
||||
if (collection::init($this::$arangodb->session, self::COLLECTION)) {
|
||||
// Initialized the collection
|
||||
|
||||
// An instance of the ArangoDB document of session is initialized?
|
||||
if (!isset($this->document)) throw new exception('An instance of the ArangoDB document of session is not initialized');
|
||||
|
||||
// Write data into buffwer of an instance of the ArangoDB document of session
|
||||
$this->document->buffer = array_replace_recursive(
|
||||
$this->document->buffer ?? [],
|
||||
[$_SERVER['INTERFACE'] => array_replace_recursive($this->document->buffer[$_SERVER['INTERFACE']] ?? [], $data)]
|
||||
);
|
||||
|
||||
// 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) {
|
||||
// Write to the registry of errors
|
||||
$errors[] = [
|
||||
'text' => $e->getMessage(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'stack' => $e->getTrace()
|
||||
];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
45
author/project/system/models/traits/status.php
Normal file
45
author/project/system/models/traits/status.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ${REPO_OWNER}\${REPO_NAME}\models\traits;
|
||||
|
||||
// Built-in libraries
|
||||
use exception;
|
||||
|
||||
/**
|
||||
* Trait fo initialization of a status
|
||||
*
|
||||
* @package ${REPO_OWNER}\${REPO_NAME}\models\traits
|
||||
*
|
||||
* @author ${REPO_OWNER} < mail >
|
||||
*/
|
||||
trait status
|
||||
{
|
||||
/**
|
||||
* Initialize of a status
|
||||
*
|
||||
* @param array &$errors Registry of errors
|
||||
*
|
||||
* @return ?bool Status, if they are found
|
||||
*/
|
||||
public function status(array &$errors = []): ?bool
|
||||
{
|
||||
try {
|
||||
// Read from ArangoDB and exit (success)
|
||||
return $this->document->active ?? false;
|
||||
} 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 null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user