PaperBag/Router.php

77 lines
2.2 KiB
PHP

<?php
const PRODUCTION = false;
const DEBUG = true;
if(DEBUG){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
// Composer autoload
require_once 'vendor/autoload.php';
// Application and Models autoload
spl_autoload_register(function ($class_name) {
if (file_exists(__DIR__ . "/application/" . $class_name . '.php')) {
include __DIR__ . "/application/" . $class_name . '.php';
// echo "Load class " . __DIR__ . "/application/" . $class_name . '.php' . "<br>\n";
}
elseif(file_exists(__DIR__ . "/models/" . $class_name . ".php")) {
include_once __DIR__."/models/Model.php";
include_once __DIR__ . "/models/" . $class_name . ".php";
// echo "Load class ".__DIR__."/models/".$class_name.'.php'."<br>\n";
}
});
// Define Twig
$twigSettings = PRODUCTION?['cache' => __dir__.'/tmp/']:[];
$loader = new Twig\Loader\FilesystemLoader(__dir__ . '/templates/'.Config::get('system', 'template', 'default')."/");
$twig = new Twig\Environment($loader, $twigSettings);
$filter = new Twig\TwigFilter('t', function ($string) {
return _(strip_tags($string));
});
$twig->addFilter($filter);
class Config {
private static $instance = null;
private $config;
private function __construct($file = 'config/config.ini'){
$this->config = parse_ini_file($file, true);
}
public static function get(string $category, string $config, $default = false){
// Singleton method to avoid reading the same file (too) many times
if (self::$instance == null){
self::$instance = new Config();
}
$conf = self::$instance;
if(!empty($conf->config[$category]) && !empty($conf->config[$category][$config])){
return $conf->config[$category][$config];
}
return $default;
}
}
function model($name){
return;
if(file_exists(__DIR__ . "/models/" . $name . ".php")){
include_once __DIR__ . "/models/Model.php";
include_once __DIR__ . "/models/" . $name . ".php";
}
else {
echo "FAULT<br>\n";
echo __DIR__ . "/models/" . $name . ".php". " doesn't exist";
}
}