added installer

This commit is contained in:
2024-01-11 05:38:30 +07:00
parent 87a80b7c91
commit a0621b623b
18 changed files with 46 additions and 33 deletions

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\controllers;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\views\manager,
${REPO_OWNER}\${REPO_NAME}\models\core as models,
${REPO_OWNER}\${REPO_NAME}\models\account_model as account,
${REPO_OWNER}\${REPO_NAME}\models\session_model as session;
// Library for ArangoDB
use ArangoDBClient\Document as _document;
// Framework for PHP
use mirzaev\minimal\controller;
/**
* Core of controllers
*
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
* @author ${REPO_OWNER} < mail >
*/
class core extends controller
{
/**
* Postfix for name of controllers files
*/
final public const POSTFIX = '';
/**
* Instance of a session
*/
protected readonly session $session;
/**
* Instance of an account
*/
protected readonly ?account $account;
/**
* Registry of errors
*/
protected array $errors = [
'session' => [],
'account' => []
];
/**
* Constructor of an instance
*
* @param bool $initialize Initialize a controller?
*
* @return void
*/
public function __construct(bool $initialize = true)
{
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
if ($_SERVER['HTTP_USER_AGENT'] === 'nginx-ssl early hints') return;
// For the extends system
parent::__construct($initialize);
if ($initialize) {
// Initializing is requested
// Initializing of models core (connect to ArangoDB...)
new models();
// Initializing of the date until which the session will be active
$expires = strtotime('+1 week');
// Initializing of default value of hash of the session
$_COOKIE["session"] ??= null;
// Initializing of session
$this->session = new session($_COOKIE["session"], $expires, $this->errors['session']);
// Handle a problems with initializing a session
if (!empty($this->errors['session'])) die;
else if ($_COOKIE["session"] !== $this->session->hash) {
// Hash of the session is changed (implies that the session has expired and recreated)
// Write a new hash of the session to cookies
setcookie(
'session',
$this->session->hash,
[
'expires' => $expires,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'strict'
]
);
}
// Initializing of preprocessor of views
$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})
};
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace ${REPO_OWNER}\${REPO_NAME}\controllers;
// Files of the project
use ${REPO_OWNER}\${REPO_NAME}\controllers\core;
/**
* Index controller
*
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
* @author ${REPO_OWNER} < mail >
*/
final class index extends core
{
/**
* Render the main page
*
* @param array $parameters Parameters of the request (POST + GET)
*/
public function index(array $parameters = []): ?string
{
// Exit (success)
if ($_SERVER['REQUEST_METHOD'] === 'GET') return $this->view->render(DIRECTORY_SEPARATOR . 'index.html');
else if ($_SERVER['REQUEST_METHOD'] === 'POST') return $main;
// Exit (fail)
return null;
}
}