Big fat commit. Now with namespaces!

This commit is contained in:
William 2022-03-02 07:15:12 +01:00
parent 083b334a07
commit e89e49b4a1
10 changed files with 128 additions and 60 deletions

View File

@ -1,62 +1,66 @@
<?php <?php
/* /**
This is the main file to be included on every page. * This is the main file to be included on every page.
It will act as a front controller of our application. * It will act as a front controller of our application.
_____ * _____
/ \ * / \
| () () | * | () () |
\ ^ / * \ ^ /
||||| * |||||
||||| * |||||
*
Tread carefully * Tread carefully
*/ */
// Disable type coercion // disable type coercion
declare(strict_types=1); declare(strict_types=1);
// Autoloader // PSR-4 like autoloader
spl_autoload_register(function ($class_name) { spl_autoload_register(
require __DIR__ . '/core/' . $class_name . '.php'; function ($className) {
}); $path = __DIR__ . '/lib/' . str_replace('\\', '/', $className) . '.php';
require $path;
}
);
/* === App Setup === */ // imports
use App\Core\ {
ErrorHandler,
Config,
Database,
Session,
User,
App,
AccessControl
};
// Displays a custom page on error or exception // displays a custom page on error or exception
new ErrorHandler; new ErrorHandler();
// Grab configuration file // grab configuration file
$config = ( $config = (new Config(__DIR__ . '/config.php'))->config;
new Config(__DIR__ . '/config.php')
)->config;
// Start database connection // start database connection
$database = new Database($config['database']); $database = new Database($config['database']);
// Session wrapper // session wrapper
$session = new Session; $session = new Session();
// Handles current user session // handles current user session
$user = new User($session, $database); $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( $app = new App(__DIR__, $config, $database, $session, $user);
$config,
$database,
$session,
$user
);
// We will use $app instead // we will use $app instead
unset( unset($config, $database, $session, $user);
$config,
$database, /**
$session, * This is important!
$user * 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; return $app;

View File

@ -1,6 +1,12 @@
<?php <?php
// TODO: ... namespace App\Core;
use \Exception;
/**
* TODO: ...
*/
class AccessControl class AccessControl
{ {
public User $user; public User $user;

View File

@ -1,19 +1,29 @@
<?php <?php
namespace App\Core;
use \Exception;
/**
* Some framework thingy
*/
class App class App
{ {
public string $dir;
public array $config; public array $config;
public Database $database; public Database $database;
public Session $session; public Session $session;
public User $user; public User $user;
public function __construct( public function __construct(
string $dir,
array $config, array $config,
Database $database, Database $database,
Session $session, Session $session,
User $user User $user
) )
{ {
$this->dir = $dir;
$this->config = $config; $this->config = $config;
$this->database = $database; $this->database = $database;
$this->session = $session; $this->session = $session;
@ -24,7 +34,7 @@ class App
public function model(string $model, $injection = NULL): object public function model(string $model, $injection = NULL): object
{ {
// Require model file // Require model file
$path = __DIR__ . '/../model/' . $model . '.php'; $path = $this->dir . '/model/' . $model . '.php';
if (!file_exists($path)) if (!file_exists($path))
{ {
throw new Exception("Model does not exist"); throw new Exception("Model does not exist");
@ -44,7 +54,7 @@ class App
// Import variables into the current symbol table from an array // Import variables into the current symbol table from an array
extract($data); extract($data);
// Require view file // Require view file
$path = __DIR__ . '/../view/' . $view . '.php'; $path = $this->dir . '/view/' . $view . '.php';
if (!file_exists($path)) if (!file_exists($path))
{ {
throw new Exception("View does not exist"); throw new Exception("View does not exist");

View File

@ -1,8 +1,17 @@
<?php <?php
// TODO: ... this should validate the config and stuffs namespace App\Core;
use \Exception;
/**
* TODO: ... this should validate the config and stuffs
*/
class Config class Config
{ {
/**
* Should hold the validated config array
*/
public array $config; public array $config;
public function __construct(string $path) public function __construct(string $path)

View File

@ -1,7 +1,15 @@
<?php <?php
// Encapsulates a single connection to a database namespace App\Core;
// TODO: refactor and add different driver implementations
use \Exception;
use \PDO;
use \PDOException;
/**
* Encapsulates a single connection to a database.
* TODO: Refactor and add different driver implementations.
*/
class Database class Database
{ {
public PDO $conn; public PDO $conn;

View File

@ -1,14 +1,23 @@
<?php <?php
// Bit of a janky way to display a custom page on error or exception. namespace App\Core;
// But looks pretty cool and professional!
/**
* A bit of a janky way to display a custom page on error or exception.
* But looks pretty cool and professional!
*/
class ErrorHandler class ErrorHandler
{ {
public array $errors; // Holds error messages (even though for now we only display a maximum of 1) /**
* Holds error messages
*/
public array $errors;
public function __construct() public function __construct()
{ {
ob_Start(); // If an error were to occur, we clear the outputted data so we only show the error page instead of both. // if an error were to occur, we clear the outputted data so we only show the error page instead of both
ob_Start();
set_error_handler([$this, 'error']); set_error_handler([$this, 'error']);
set_exception_handler([$this, 'exception']); set_exception_handler([$this, 'exception']);
$this->errors = []; $this->errors = [];
@ -18,7 +27,6 @@ class ErrorHandler
{ {
$errstr = htmlspecialchars($errstr); $errstr = htmlspecialchars($errstr);
$this->errors[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>"; $this->errors[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
die();
} }
public function exception($exception): void public function exception($exception): void
@ -31,7 +39,10 @@ class ErrorHandler
if (!$this->errors) { if (!$this->errors) {
return; 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); http_response_code(500);
header_remove(); header_remove();

View File

@ -1,6 +1,12 @@
<?php <?php
// Handles anything to do with sessions namespace App\Core;
use \Exception;
/**
* Handles anything to do with sessions
*/
class Session class Session
{ {
public function __construct() public function __construct()
@ -17,7 +23,9 @@ class Session
return array_key_exists($key, $_SESSION); return array_key_exists($key, $_SESSION);
} }
// Returns mixed but php 7.4 DOES NOT SUPPORT THAT TYPE HINT >:(( /**
* Returns mixed but php 7.4 DOES NOT SUPPORT THAT TYPE HINT >:((
*/
public function get(string $key) public function get(string $key)
{ {
if ($this->has($key)) if ($this->has($key))

View File

@ -1,5 +1,13 @@
<?php <?php
namespace App\Core;
use \Exception;
use \PDO;
/**
* Represents the current user session
*/
class User class User
{ {
private const SESSION_KEY = 'UserClass'; private const SESSION_KEY = 'UserClass';

View File

@ -1,5 +1,7 @@
<?php <?php
use App\Core\Database as Database;
class Example class Example
{ {
public Database $database; public Database $database;

View File

@ -1,5 +1,7 @@
<?php <?php
use App\Core\Database as Database;
class Teamtable class Teamtable
{ {
public array $template = [ public array $template = [