From e89e49b4a17135ccf30c98bc8d9ec402528430f1 Mon Sep 17 00:00:00 2001 From: William Date: Wed, 2 Mar 2022 07:15:12 +0100 Subject: [PATCH] Big fat commit. Now with namespaces! --- app/inc.php | 96 ++++++++++---------- app/{core => lib/App/Core}/AccessControl.php | 8 +- app/{core => lib/App/Core}/App.php | 14 ++- app/{core => lib/App/Core}/Config.php | 11 ++- app/{core => lib/App/Core}/Database.php | 12 ++- app/{core => lib/App/Core}/ErrorHandler.php | 23 +++-- app/{core => lib/App/Core}/Session.php | 12 ++- app/{core => lib/App/Core}/User.php | 8 ++ app/model/Example.php | 2 + app/model/Teamtable.php | 2 + 10 files changed, 128 insertions(+), 60 deletions(-) rename app/{core => lib/App/Core}/AccessControl.php (95%) rename app/{core => lib/App/Core}/App.php (86%) rename app/{core => lib/App/Core}/Config.php (62%) rename app/{core => lib/App/Core}/Database.php (82%) rename app/{core => lib/App/Core}/ErrorHandler.php (73%) rename app/{core => lib/App/Core}/Session.php (92%) rename app/{core => lib/App/Core}/User.php (96%) diff --git a/app/inc.php b/app/inc.php index fdf16eb..9765dba 100644 --- a/app/inc.php +++ b/app/inc.php @@ -1,62 +1,66 @@ config; +// grab configuration file +$config = (new Config(__DIR__ . '/config.php'))->config; -// Start database connection +// start database connection $database = new Database($config['database']); -// Session wrapper -$session = new Session; +// session wrapper +$session = new Session(); -// Handles current user session -$user = new User($session, $database); +// handles current user session +$user = new User($session, $database); -// THIS IS IMPORTANT!! -// Without it, everyone will have access to any page without having to be logged in. -// Decides if the user is allowed to view current page -new AccessControl($user, $config['root_url']); -$app = new App( - $config, - $database, - $session, - $user -); +$app = new App(__DIR__, $config, $database, $session, $user); -// We will use $app instead -unset( - $config, - $database, - $session, - $user -); +// we will use $app instead +unset($config, $database, $session, $user); + +/** + * This is important! + * Without it, everyone will have access to any page without having to be logged in. + * + * Decides if the user is allowed to view current page. + */ +new AccessControl($app->user, $app->config['root_url']); return $app; \ No newline at end of file diff --git a/app/core/AccessControl.php b/app/lib/App/Core/AccessControl.php similarity index 95% rename from app/core/AccessControl.php rename to app/lib/App/Core/AccessControl.php index 0d3f1c9..4cdd5e6 100644 --- a/app/core/AccessControl.php +++ b/app/lib/App/Core/AccessControl.php @@ -1,6 +1,12 @@ dir = $dir; $this->config = $config; $this->database = $database; $this->session = $session; @@ -24,7 +34,7 @@ class App public function model(string $model, $injection = NULL): object { // Require model file - $path = __DIR__ . '/../model/' . $model . '.php'; + $path = $this->dir . '/model/' . $model . '.php'; if (!file_exists($path)) { throw new Exception("Model does not exist"); @@ -44,7 +54,7 @@ class App // Import variables into the current symbol table from an array extract($data); // Require view file - $path = __DIR__ . '/../view/' . $view . '.php'; + $path = $this->dir . '/view/' . $view . '.php'; if (!file_exists($path)) { throw new Exception("View does not exist"); diff --git a/app/core/Config.php b/app/lib/App/Core/Config.php similarity index 62% rename from app/core/Config.php rename to app/lib/App/Core/Config.php index d72c8ff..0fda980 100644 --- a/app/core/Config.php +++ b/app/lib/App/Core/Config.php @@ -1,8 +1,17 @@ errors = []; @@ -18,7 +27,6 @@ class ErrorHandler { $errstr = htmlspecialchars($errstr); $this->errors[] = "Error[$errno]: $errstr in $errfile at line $errline"; - die(); } public function exception($exception): void @@ -31,7 +39,10 @@ class ErrorHandler if (!$this->errors) { return; } - ob_end_clean(); // Remove current output to be replaced by error page + + // remove current output to be replaced by error page + ob_end_clean(); + http_response_code(500); header_remove(); diff --git a/app/core/Session.php b/app/lib/App/Core/Session.php similarity index 92% rename from app/core/Session.php rename to app/lib/App/Core/Session.php index 084cfff..509290c 100644 --- a/app/core/Session.php +++ b/app/lib/App/Core/Session.php @@ -1,6 +1,12 @@ :(( + /** + * Returns mixed but php 7.4 DOES NOT SUPPORT THAT TYPE HINT >:(( + */ public function get(string $key) { if ($this->has($key)) diff --git a/app/core/User.php b/app/lib/App/Core/User.php similarity index 96% rename from app/core/User.php rename to app/lib/App/Core/User.php index e56f9ed..07d8b07 100644 --- a/app/core/User.php +++ b/app/lib/App/Core/User.php @@ -1,5 +1,13 @@