diff --git a/app/Controllers/Controller.php b/app/Controllers/Controller.php index 762260c..f6a6452 100644 --- a/app/Controllers/Controller.php +++ b/app/Controllers/Controller.php @@ -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') { diff --git a/app/Database/DB.php b/app/Database/DB.php index 186d763..f3b9391 100644 --- a/app/Database/DB.php +++ b/app/Database/DB.php @@ -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 diff --git a/app/Traits/SingletonController.php b/app/Traits/SingletonController.php index d6e69cb..210e5b6 100644 --- a/app/Traits/SingletonController.php +++ b/app/Traits/SingletonController.php @@ -9,6 +9,10 @@ trait SingletonController { protected static $instance; + /** + * Return the controller instance + * @return Controller + */ public static function instance(): Controller { if (static::$instance === null) { diff --git a/app/Web/Session.php b/app/Web/Session.php index 9e0a358..bfaf956 100644 --- a/app/Web/Session.php +++ b/app/Web/Session.php @@ -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'); diff --git a/bootstrap/app.php b/bootstrap/app.php index 41c6c55..fd8933e 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -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'; \ No newline at end of file diff --git a/composer.json b/composer.json index 1029bc9..6cbd0a0 100644 --- a/composer.json +++ b/composer.json @@ -10,7 +10,8 @@ "monolog/monolog": "^1.23", "php": ">=7.1", "ext-json": "*", - "ext-gd": "*" + "ext-gd": "*", + "ext-pdo": "*" }, "autoload": { "psr-4": {