Begin rework on app structure

This commit is contained in:
Sky Johnson 2024-07-11 18:17:51 -05:00
parent 13c2f7fb4b
commit 2683a5b4bb
4 changed files with 61 additions and 6 deletions

View File

@ -1,6 +1,19 @@
<?php // index.php :: Primary program script, evil alien overlord, you decide.
// define our server path and bootstrap the request
const SERVER = '../server';
require_once SERVER.'/bootstrap.php';
if (!installed()) redirect('/install');
// check if the server has been installed
if (!installed()) redirect('/install');
// spin up our app container
$app = new App(DB);
// route the request
// routing follows a simple rule; the first segment of the URI is the
// module, and the module handles the rest
$route = App::$req->uri(0);
$routes = [
'home' => 'HomeModule::home',
];

View File

@ -14,4 +14,18 @@ class App
self::$req = new Request(); // the current request
self::$db = new Database($dbPath); // the database
}
public function route(string $uri, array $routes)
{
// check if the module exists
if (isset($routes[$module])) {
// if the module exists, call the module's handle method
$routes[$module]($this);
} else {
// if the module does not exist, render a 404 page
$this->render('404', [
'title' => '404',
]);
}
}
}

View File

@ -1,15 +1,34 @@
<?php // bootstrap.php :: Initialize all our boring stuff
<?php
// @todo use a flag for this
ini_set('display_errors', 'on');
error_reporting(E_ALL | E_STRICT);
$start = microtime(true);
define('START', microtime(true)); // start the timer for this execution
session_start();
session_start(); // initialize the session engine
// @todo move these to a settings config somewhere
const VERSION = '1.1.11';
const BUILD = '';
const DB = SERVER.'/database/dragon.db';
require_once SERVER.'/library.php';
require_once SERVER.'/database.php';
require_once SERVER.'/library.php'; // include our miscellaneous functions
// autoloader map
const MAP = [
// 'Class' => 'path/to/class.php',
// server-level classes
'App' => SERVER.'/app/app.php',
'Database' => SERVER.'/app/database.php',
'Request' => SERVER.'/app/request.php',
// modules
'HomeModule' => SERVER.'/modules/HomeModule.php',
];
// autoloader
spl_autoload_register(function($class) {
if (isset(MAP[$class])) require_once MAP[$class];
});

View File

@ -0,0 +1,9 @@
<?php
class HomeModule
{
public static function home(App $app)
{
echo 'Welcome to the home module!';
}
}