Big fat commit. Now with namespaces!
This commit is contained in:
parent
083b334a07
commit
e89e49b4a1
92
app/inc.php
92
app/inc.php
@ -1,62 +1,66 @@
|
||||
<?php
|
||||
/*
|
||||
This is the main file to be included on every page.
|
||||
It will act as a front controller of our application.
|
||||
_____
|
||||
/ \
|
||||
| () () |
|
||||
\ ^ /
|
||||
|||||
|
||||
|||||
|
||||
|
||||
Tread carefully
|
||||
/**
|
||||
* This is the main file to be included on every page.
|
||||
* It will act as a front controller of our application.
|
||||
* _____
|
||||
* / \
|
||||
* | () () |
|
||||
* \ ^ /
|
||||
* |||||
|
||||
* |||||
|
||||
*
|
||||
* Tread carefully
|
||||
*/
|
||||
|
||||
// Disable type coercion
|
||||
// disable type coercion
|
||||
declare(strict_types=1);
|
||||
|
||||
// Autoloader
|
||||
spl_autoload_register(function ($class_name) {
|
||||
require __DIR__ . '/core/' . $class_name . '.php';
|
||||
});
|
||||
// PSR-4 like autoloader
|
||||
spl_autoload_register(
|
||||
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
|
||||
new ErrorHandler;
|
||||
// displays a custom page on error or exception
|
||||
new ErrorHandler();
|
||||
|
||||
// Grab configuration file
|
||||
$config = (
|
||||
new Config(__DIR__ . '/config.php')
|
||||
)->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
|
||||
// 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;
|
@ -1,6 +1,12 @@
|
||||
<?php
|
||||
|
||||
// TODO: ...
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
|
||||
/**
|
||||
* TODO: ...
|
||||
*/
|
||||
class AccessControl
|
||||
{
|
||||
public User $user;
|
@ -1,19 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
|
||||
/**
|
||||
* Some framework thingy
|
||||
*/
|
||||
class App
|
||||
{
|
||||
public string $dir;
|
||||
public array $config;
|
||||
public Database $database;
|
||||
public Session $session;
|
||||
public User $user;
|
||||
|
||||
public function __construct(
|
||||
string $dir,
|
||||
array $config,
|
||||
Database $database,
|
||||
Session $session,
|
||||
User $user
|
||||
)
|
||||
{
|
||||
$this->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");
|
@ -1,8 +1,17 @@
|
||||
<?php
|
||||
|
||||
// TODO: ... this should validate the config and stuffs
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
|
||||
/**
|
||||
* TODO: ... this should validate the config and stuffs
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* Should hold the validated config array
|
||||
*/
|
||||
public array $config;
|
||||
|
||||
public function __construct(string $path)
|
@ -1,7 +1,15 @@
|
||||
<?php
|
||||
|
||||
// Encapsulates a single connection to a database
|
||||
// TODO: refactor and add different driver implementations
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
use \PDO;
|
||||
use \PDOException;
|
||||
|
||||
/**
|
||||
* Encapsulates a single connection to a database.
|
||||
* TODO: Refactor and add different driver implementations.
|
||||
*/
|
||||
class Database
|
||||
{
|
||||
public PDO $conn;
|
@ -1,14 +1,23 @@
|
||||
<?php
|
||||
|
||||
// Bit of a janky way to display a custom page on error or exception.
|
||||
// But looks pretty cool and professional!
|
||||
namespace App\Core;
|
||||
|
||||
/**
|
||||
* A bit of a janky way to display a custom page on error or exception.
|
||||
* But looks pretty cool and professional!
|
||||
*/
|
||||
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()
|
||||
{
|
||||
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_exception_handler([$this, 'exception']);
|
||||
$this->errors = [];
|
||||
@ -18,7 +27,6 @@ class ErrorHandler
|
||||
{
|
||||
$errstr = htmlspecialchars($errstr);
|
||||
$this->errors[] = "<b>Error[$errno]:</b> $errstr in <b>$errfile</b> at line <b>$errline</b>";
|
||||
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();
|
||||
|
@ -1,6 +1,12 @@
|
||||
<?php
|
||||
|
||||
// Handles anything to do with sessions
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
|
||||
/**
|
||||
* Handles anything to do with sessions
|
||||
*/
|
||||
class Session
|
||||
{
|
||||
public function __construct()
|
||||
@ -17,7 +23,9 @@ class 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)
|
||||
{
|
||||
if ($this->has($key))
|
@ -1,5 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
use \Exception;
|
||||
use \PDO;
|
||||
|
||||
/**
|
||||
* Represents the current user session
|
||||
*/
|
||||
class User
|
||||
{
|
||||
private const SESSION_KEY = 'UserClass';
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Core\Database as Database;
|
||||
|
||||
class Example
|
||||
{
|
||||
public Database $database;
|
||||
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use App\Core\Database as Database;
|
||||
|
||||
class Teamtable
|
||||
{
|
||||
public array $template = [
|
||||
|
Reference in New Issue
Block a user