Doc blocks
This commit is contained in:
parent
2e1a6dcced
commit
732cf7ff48
6 changed files with 88 additions and 1 deletions
|
@ -15,6 +15,7 @@ abstract class Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Check if the current user is logged in
|
||||||
* @throws AuthenticationException
|
* @throws AuthenticationException
|
||||||
*/
|
*/
|
||||||
protected function checkLogin(): void
|
protected function checkLogin(): void
|
||||||
|
@ -34,6 +35,7 @@ abstract class Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Check if the current user is an admin
|
||||||
* @throws AuthenticationException
|
* @throws AuthenticationException
|
||||||
* @throws UnauthorizedException
|
* @throws UnauthorizedException
|
||||||
*/
|
*/
|
||||||
|
@ -50,6 +52,12 @@ abstract class Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a human readable file size
|
||||||
|
* @param $size
|
||||||
|
* @param int $precision
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
protected function humanFilesize($size, $precision = 2): string
|
protected function humanFilesize($size, $precision = 2): string
|
||||||
{
|
{
|
||||||
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
|
for ($i = 0; ($size / 1024) > 0.9; $i++, $size /= 1024) {
|
||||||
|
@ -57,11 +65,20 @@ abstract class Controller
|
||||||
return round($size, $precision) . ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
|
return round($size, $precision) . ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'][$i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a filesystem instance
|
||||||
|
* @return Filesystem
|
||||||
|
*/
|
||||||
protected function getStorage(): Filesystem
|
protected function getStorage(): Filesystem
|
||||||
{
|
{
|
||||||
return new Filesystem(new Local(Flight::get('config')['storage_dir']));
|
return new Filesystem(new Local(Flight::get('config')['storage_dir']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set http2 header for a resource if is supported
|
||||||
|
* @param string $url
|
||||||
|
* @param string $as
|
||||||
|
*/
|
||||||
protected function http2push(string $url, string $as = 'image'): void
|
protected function http2push(string $url, string $as = 'image'): void
|
||||||
{
|
{
|
||||||
if (Flight::request()->scheme === 'HTTP/2.0') {
|
if (Flight::request()->scheme === 'HTTP/2.0') {
|
||||||
|
|
|
@ -50,6 +50,7 @@ class DB
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the PDO instance
|
||||||
* @return PDO
|
* @return PDO
|
||||||
*/
|
*/
|
||||||
public function getPdo(): PDO
|
public function getPdo(): PDO
|
||||||
|
@ -58,6 +59,7 @@ class DB
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the current PDO driver
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getCurrentDriver(): string
|
public function getCurrentDriver(): string
|
||||||
|
@ -65,6 +67,12 @@ class DB
|
||||||
return $this->currentDriver;
|
return $this->currentDriver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a query
|
||||||
|
* @param string $query
|
||||||
|
* @param array $parameters
|
||||||
|
* @return bool|\PDOStatement|string
|
||||||
|
*/
|
||||||
public static function query(string $query, $parameters = [])
|
public static function query(string $query, $parameters = [])
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -75,6 +83,10 @@ class DB
|
||||||
return self::$instance->doQuery($query, $parameters);
|
return self::$instance->doQuery($query, $parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static method to get the current driver name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public static function driver(): string
|
public static function driver(): string
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -85,6 +97,10 @@ class DB
|
||||||
return self::$instance->getCurrentDriver();
|
return self::$instance->getCurrentDriver();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get directly the PDO instance
|
||||||
|
* @return PDO
|
||||||
|
*/
|
||||||
public static function raw(): PDO
|
public static function raw(): PDO
|
||||||
{
|
{
|
||||||
if (self::$instance === null) {
|
if (self::$instance === null) {
|
||||||
|
@ -95,6 +111,7 @@ class DB
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Set the PDO connection string
|
||||||
* @param string $dsn
|
* @param string $dsn
|
||||||
* @param string|null $username
|
* @param string|null $username
|
||||||
* @param string|null $password
|
* @param string|null $password
|
||||||
|
|
|
@ -9,6 +9,10 @@ trait SingletonController
|
||||||
{
|
{
|
||||||
protected static $instance;
|
protected static $instance;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the controller instance
|
||||||
|
* @return Controller
|
||||||
|
*/
|
||||||
public static function instance(): Controller
|
public static function instance(): Controller
|
||||||
{
|
{
|
||||||
if (static::$instance === null) {
|
if (static::$instance === null) {
|
||||||
|
|
|
@ -6,6 +6,9 @@ namespace App\Web;
|
||||||
class Session
|
class Session
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start a session if is not already started in the current context
|
||||||
|
*/
|
||||||
public static function init(): void
|
public static function init(): void
|
||||||
{
|
{
|
||||||
if (session_status() === PHP_SESSION_NONE) {
|
if (session_status() === PHP_SESSION_NONE) {
|
||||||
|
@ -16,47 +19,84 @@ class Session
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroy the current session
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public static function destroy(): bool
|
public static function destroy(): bool
|
||||||
{
|
{
|
||||||
return session_destroy();
|
return session_destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all session stored values
|
||||||
|
*/
|
||||||
public static function clear(): void
|
public static function clear(): void
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
$_SESSION = [];
|
$_SESSION = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if session has a stored key
|
||||||
|
* @param $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public static function has($key): bool
|
public static function has($key): bool
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
return isset($_SESSION[$key]);
|
return isset($_SESSION[$key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the content of the current session
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public static function all(): array
|
public static function all(): array
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
return $_SESSION;
|
return $_SESSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returned a value given a key
|
||||||
|
* @param $key
|
||||||
|
* @param null $default
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
public static function get($key, $default = null)
|
public static function get($key, $default = null)
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
return self::has($key) ? $_SESSION[$key] : $default;
|
return self::has($key) ? $_SESSION[$key] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a key-value pair to the session
|
||||||
|
* @param $key
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
public static function set($key, $value): void
|
public static function set($key, $value): void
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
$_SESSION[$key] = $value;
|
$_SESSION[$key] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a flash alert
|
||||||
|
* @param $message
|
||||||
|
* @param string $type
|
||||||
|
*/
|
||||||
public static function alert($message, string $type = 'info'): void
|
public static function alert($message, string $type = 'info'): void
|
||||||
{
|
{
|
||||||
self::init();
|
self::init();
|
||||||
$_SESSION['_flash'] = [$type => $message];
|
$_SESSION['_flash'] = [$type => $message];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve flash alerts
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
public static function getAlert()
|
public static function getAlert()
|
||||||
{
|
{
|
||||||
$flash = self::get('_flash');
|
$flash = self::get('_flash');
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use App\Database\DB;
|
use App\Database\DB;
|
||||||
|
|
||||||
|
// Load the config
|
||||||
$config = array_replace_recursive([
|
$config = array_replace_recursive([
|
||||||
'app_name' => 'XBackBone',
|
'app_name' => 'XBackBone',
|
||||||
'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'],
|
'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'],
|
||||||
|
@ -15,12 +16,15 @@ $config = array_replace_recursive([
|
||||||
],
|
],
|
||||||
], require 'config.php');
|
], require 'config.php');
|
||||||
|
|
||||||
|
// Set flight parameters
|
||||||
Flight::set('flight.base_url', $config['base_url']);
|
Flight::set('flight.base_url', $config['base_url']);
|
||||||
Flight::set('flight.log_errors', false);
|
Flight::set('flight.log_errors', false);
|
||||||
Flight::set('config', $config);
|
Flight::set('config', $config);
|
||||||
|
|
||||||
|
// Set the database dsn
|
||||||
DB::setDsn($config['db']['connection'] . ':' . $config['db']['dsn'], $config['db']['username'], $config['db']['password']);
|
DB::setDsn($config['db']['connection'] . ':' . $config['db']['dsn'], $config['db']['username'], $config['db']['password']);
|
||||||
|
|
||||||
|
// Register the Twig instance
|
||||||
Flight::register('view', 'Twig_Environment',
|
Flight::register('view', 'Twig_Environment',
|
||||||
[new Twig_Loader_Filesystem('resources/templates'),
|
[new Twig_Loader_Filesystem('resources/templates'),
|
||||||
[
|
[
|
||||||
|
@ -32,10 +36,12 @@ Flight::register('view', 'Twig_Environment',
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Redirect back helper
|
||||||
Flight::register('redirectBack', function () {
|
Flight::register('redirectBack', function () {
|
||||||
Flight::redirect(Flight::request()->referrer);
|
Flight::redirect(Flight::request()->referrer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Map the render call to the Twig view instance
|
||||||
Flight::map('render', function (string $template, array $data = []) use (&$config) {
|
Flight::map('render', function (string $template, array $data = []) use (&$config) {
|
||||||
Flight::view()->addGlobal('config', $config);
|
Flight::view()->addGlobal('config', $config);
|
||||||
Flight::view()->addGlobal('request', Flight::request());
|
Flight::view()->addGlobal('request', Flight::request());
|
||||||
|
@ -45,6 +51,7 @@ Flight::map('render', function (string $template, array $data = []) use (&$confi
|
||||||
Flight::view()->display($template, $data);
|
Flight::view()->display($template, $data);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// The application error handler
|
||||||
Flight::map('error', function (Exception $exception) {
|
Flight::map('error', function (Exception $exception) {
|
||||||
if ($exception instanceof \App\Exceptions\AuthenticationException) {
|
if ($exception instanceof \App\Exceptions\AuthenticationException) {
|
||||||
Flight::redirect('/login');
|
Flight::redirect('/login');
|
||||||
|
@ -72,4 +79,5 @@ Flight::map('notFound', function () {
|
||||||
Flight::render('errors/404.twig');
|
Flight::render('errors/404.twig');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Load the application routes
|
||||||
require 'app/routes.php';
|
require 'app/routes.php';
|
|
@ -10,7 +10,8 @@
|
||||||
"monolog/monolog": "^1.23",
|
"monolog/monolog": "^1.23",
|
||||||
"php": ">=7.1",
|
"php": ">=7.1",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-gd": "*"
|
"ext-gd": "*",
|
||||||
|
"ext-pdo": "*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
Loading…
Reference in a new issue