Doc blocks

This commit is contained in:
Sergio Brighenti 2018-10-13 18:25:48 +02:00
parent 2e1a6dcced
commit 732cf7ff48
6 changed files with 88 additions and 1 deletions

View file

@ -15,6 +15,7 @@ abstract class Controller
{
/**
* Check if the current user is logged in
* @throws AuthenticationException
*/
protected function checkLogin(): void
@ -34,6 +35,7 @@ abstract class Controller
}
/**
* Check if the current user is an admin
* @throws AuthenticationException
* @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
{
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];
}
/**
* Get a filesystem instance
* @return Filesystem
*/
protected function getStorage(): Filesystem
{
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
{
if (Flight::request()->scheme === 'HTTP/2.0') {

View file

@ -50,6 +50,7 @@ class DB
}
/**
* Get the PDO instance
* @return PDO
*/
public function getPdo(): PDO
@ -58,6 +59,7 @@ class DB
}
/**
* Get the current PDO driver
* @return string
*/
public function getCurrentDriver(): string
@ -65,6 +67,12 @@ class DB
return $this->currentDriver;
}
/**
* Perform a query
* @param string $query
* @param array $parameters
* @return bool|\PDOStatement|string
*/
public static function query(string $query, $parameters = [])
{
@ -75,6 +83,10 @@ class DB
return self::$instance->doQuery($query, $parameters);
}
/**
* Static method to get the current driver name
* @return string
*/
public static function driver(): string
{
@ -85,6 +97,10 @@ class DB
return self::$instance->getCurrentDriver();
}
/**
* Get directly the PDO instance
* @return PDO
*/
public static function raw(): PDO
{
if (self::$instance === null) {
@ -95,6 +111,7 @@ class DB
}
/**
* Set the PDO connection string
* @param string $dsn
* @param string|null $username
* @param string|null $password

View file

@ -9,6 +9,10 @@ trait SingletonController
{
protected static $instance;
/**
* Return the controller instance
* @return Controller
*/
public static function instance(): Controller
{
if (static::$instance === null) {

View file

@ -6,6 +6,9 @@ namespace App\Web;
class Session
{
/**
* Start a session if is not already started in the current context
*/
public static function init(): void
{
if (session_status() === PHP_SESSION_NONE) {
@ -16,47 +19,84 @@ class Session
}
}
/**
* Destroy the current session
* @return bool
*/
public static function destroy(): bool
{
return session_destroy();
}
/**
* Clear all session stored values
*/
public static function clear(): void
{
self::init();
$_SESSION = [];
}
/**
* Check if session has a stored key
* @param $key
* @return bool
*/
public static function has($key): bool
{
self::init();
return isset($_SESSION[$key]);
}
/**
* Get the content of the current session
* @return array
*/
public static function all(): array
{
self::init();
return $_SESSION;
}
/**
* Returned a value given a key
* @param $key
* @param null $default
* @return mixed
*/
public static function get($key, $default = null)
{
self::init();
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
{
self::init();
$_SESSION[$key] = $value;
}
/**
* Set a flash alert
* @param $message
* @param string $type
*/
public static function alert($message, string $type = 'info'): void
{
self::init();
$_SESSION['_flash'] = [$type => $message];
}
/**
* Retrieve flash alerts
* @return array
*/
public static function getAlert()
{
$flash = self::get('_flash');

View file

@ -2,6 +2,7 @@
use App\Database\DB;
// Load the config
$config = array_replace_recursive([
'app_name' => 'XBackBone',
'base_url' => isset($_SERVER['HTTPS']) ? 'https://' . $_SERVER['HTTP_HOST'] : 'http://' . $_SERVER['HTTP_HOST'],
@ -15,12 +16,15 @@ $config = array_replace_recursive([
],
], require 'config.php');
// Set flight parameters
Flight::set('flight.base_url', $config['base_url']);
Flight::set('flight.log_errors', false);
Flight::set('config', $config);
// Set the database dsn
DB::setDsn($config['db']['connection'] . ':' . $config['db']['dsn'], $config['db']['username'], $config['db']['password']);
// Register the Twig instance
Flight::register('view', 'Twig_Environment',
[new Twig_Loader_Filesystem('resources/templates'),
[
@ -32,10 +36,12 @@ Flight::register('view', 'Twig_Environment',
]
);
// Redirect back helper
Flight::register('redirectBack', function () {
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::view()->addGlobal('config', $config);
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);
});
// The application error handler
Flight::map('error', function (Exception $exception) {
if ($exception instanceof \App\Exceptions\AuthenticationException) {
Flight::redirect('/login');
@ -72,4 +79,5 @@ Flight::map('notFound', function () {
Flight::render('errors/404.twig');
});
// Load the application routes
require 'app/routes.php';

View file

@ -10,7 +10,8 @@
"monolog/monolog": "^1.23",
"php": ">=7.1",
"ext-json": "*",
"ext-gd": "*"
"ext-gd": "*",
"ext-pdo": "*"
},
"autoload": {
"psr-4": {