Core lib of filesite.io, a small PHP Framework.
https://fsc.filesite.io
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.3 KiB
95 lines
3.3 KiB
3 years ago
|
<?php
|
||
|
/**
|
||
|
* Class App
|
||
|
*/
|
||
|
Class FSC {
|
||
|
public static $app = array();
|
||
|
protected static $start_time = 0;
|
||
|
|
||
|
//call function in controller
|
||
|
public static function run($config = []) { //--{{{
|
||
|
self::$start_time = microtime(true);
|
||
|
|
||
|
try {
|
||
|
self::loadController($config);
|
||
|
}catch(Exception $e) {
|
||
|
$content = htmlspecialchars($e->getMessage(), ENT_QUOTES);
|
||
|
$code = $e->getCode();
|
||
|
if (empty($code)) {$code = 500;}
|
||
|
$title = "HTTP/1.0 {$code} Internal Server Error";
|
||
|
header($title, true, $code);
|
||
|
|
||
|
//render error layout
|
||
|
$errors = compact('code', 'title', 'content');
|
||
|
$layoutName = !empty($config['error_layout']) ? $config['error_layout'] : 'error';
|
||
|
$error_layout = __DIR__ . "/../views/layout/{$layoutName}.php";
|
||
|
if (!empty($config['theme'])) {
|
||
|
$error_layout = __DIR__ . "/../themes/{$config['theme']}/views/layout/{$layoutName}.php";
|
||
|
}
|
||
|
|
||
|
ob_start();
|
||
|
include_once $error_layout;
|
||
|
|
||
|
$htmlCode = ob_get_contents();
|
||
|
ob_end_clean();
|
||
|
|
||
|
//enable gzip
|
||
|
ob_start('ob_gzhandler');
|
||
|
echo $htmlCode;
|
||
|
ob_end_flush();
|
||
|
}
|
||
|
} //--}}}
|
||
|
|
||
|
//parse url to controller and action name
|
||
|
protected static function getControllerAndAction($url, $config) { //--{{{
|
||
|
$arr = parse_url($url);
|
||
|
$path = !empty($arr['path']) ? $arr['path'] : '/';
|
||
|
|
||
|
@list(, $controller, $action) = explode('/', $path);
|
||
|
if (empty($controller)) {
|
||
|
$controller = !empty($config['defaultController']) ? $config['defaultController'] : 'site';
|
||
|
}else if(preg_match('/\w+\.\w+/i', $controller)) { //not controller but some file not exist
|
||
|
throw new Exception("File {$controller} not exist.", 404);
|
||
|
}else {
|
||
|
$controller = preg_replace('/\W/', '', $controller);
|
||
|
}
|
||
|
|
||
|
if (empty($action)) {
|
||
|
$action = 'index';
|
||
|
}else {
|
||
|
$action = preg_replace('/\W/', '', $action);
|
||
|
}
|
||
|
|
||
|
return compact('controller', 'action');
|
||
|
} //--}}}
|
||
|
|
||
|
protected static function loadController($config) { //--{{{
|
||
|
//parse url to controller and action
|
||
|
$requestUrl = $_SERVER['REQUEST_URI'];
|
||
|
$arr = self::getControllerAndAction($requestUrl, $config);
|
||
|
$controller = $arr['controller'];
|
||
|
$action = $arr['action'];
|
||
|
$start_time = self::$start_time;
|
||
|
|
||
|
//set parameters
|
||
|
self::$app = compact('config', 'controller', 'action', 'requestUrl', 'start_time');
|
||
|
|
||
|
//call class and function
|
||
|
$className = ucfirst($controller) . 'Controller';
|
||
|
$funName = 'action' . ucfirst($action);
|
||
|
$controllerFile = __DIR__ . "/../controller/{$className}.php";
|
||
|
if (file_exists($controllerFile)) {
|
||
|
require_once $controllerFile;
|
||
|
$cls = new $className();
|
||
|
if (method_exists($className, $funName)) {
|
||
|
$cls->$funName();
|
||
|
}else {
|
||
|
throw new Exception("Function {$funName} not exist in class {$className}.", 500);
|
||
|
}
|
||
|
}else {
|
||
|
throw new Exception("Controller {$className} not exist.", 500);
|
||
|
}
|
||
|
} //--}}}
|
||
|
|
||
|
}
|