<?php
/**
 * This is the main file to be included on every page.
 * It will act as a front controller of our application.
 *   _____
 *  /     \
 * | () () |
 *  \  ^  /
 *   |||||
 *   |||||
 *
 * Tread carefully
 */

// PSR-4 like autoloader
spl_autoload_register(
    function ($className) {
        $path = __DIR__ . '/lib/' . str_replace('\\', '/', $className) . '.php';
        require $path;
    }
);

use App\Core\ {
    ErrorHandler,
    Config,
    Database,
    Session,
    User,
    App,
    AccessControl
};

new ErrorHandler();

$config = (new Config(__DIR__ . '/config.php'))->config;

$database = new Database($config['database']);

$session = new Session();

$user = new User($session, $database);

$app = new App(__DIR__, $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);

return $app;