PaperBag/Router.php

93 lines
2.8 KiB
PHP

<?php
const PRODUCTION = false;
// 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";
}elseif(file_exists(__DIR__ . "/models/Plan/" . $class_name . ".php")) {
include_once __DIR__."/models/Model.php";
include_once __DIR__ . "/models/Plan/" . $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) {
if(strstr($string, "\n")){
$lines = explode("\n", $string);
foreach($lines as $line){
$line = trim(strip_tags($line));
if(!empty($line)){
$string = str_replace($line, __($line), $string);
}
}
return $string;
}
else {
return __(strip_tags($string));
}
}, ['is_safe' => ['html']]);
$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;
}
}
define("DEBUG", Config::get('system', 'debug', false));
if(DEBUG){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
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";
}
}