Refactor PHP.

This commit is contained in:
Lars Jung 2015-05-06 23:51:12 +02:00
parent 55597ae1ba
commit ea24ae3b5f
15 changed files with 57 additions and 40 deletions

View file

@ -2,18 +2,27 @@
class Bootstrap {
public static function run() {
private static $classpaths = ["/inc", "/inc/core", "/inc/ext"];
normalized_require_once("config");
private $basepath;
$bs = new Bootstrap();
$bs->setup_php();
$bs->setup_app();
$bs->setup_admin();
$bs->setup_server();
$bs->setup_paths();
$bs->setup_cache();
$bs->setup_ext_cmds();
public function __construct($basepath) {
$this->basepath = $basepath;
}
public function run() {
spl_autoload_register([$this, "autoload"]);
$this->once("config");
$this->setup_php();
$this->setup_app();
$this->setup_admin();
$this->setup_server();
$this->setup_paths();
$this->setup_cache();
$this->setup_ext_cmds();
$app = new App();
if (Util::is_post_request()) {
@ -22,10 +31,29 @@ class Bootstrap {
} else {
$fallback = new Fallback($app);
define("FALLBACK", $fallback->get_html());
normalized_require_once("inc/page");
$this->once("inc/page");
}
}
public function autoload($class_name) {
$filename = "class-" . strtolower($class_name) . ".php";
foreach (Bootstrap::$classpaths as $path) {
$file = $this->basepath . $path . "/" . $filename;
if (file_exists($file)) {
require_once($file);
return true;
}
}
return false;
}
private function once($lib) {
require_once($this->basepath . "/" . $lib . ".php");
}
private function setup_php() {
@ -42,7 +70,6 @@ class Bootstrap {
define("HAS_PHP_JPEG", $has_php_jpeg);
}
private function setup_app() {
define("NAME", "{{pkg.name}}");
@ -50,7 +77,6 @@ class Bootstrap {
define("FILE_PREFIX", "_{{pkg.name}}");
}
private function setup_admin() {
session_start();
@ -59,7 +85,6 @@ class Bootstrap {
define("HAS_CUSTOM_PASSHASH", strcasecmp(PASSHASH, "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e") !== 0);
}
private function setup_server() {
$server_name = null;
@ -74,7 +99,6 @@ class Bootstrap {
define("HAS_SERVER", in_array($server_name, ["apache", "lighttpd", "nginx", "cherokee"]));
}
private function setup_paths() {
$script_name = getenv("SCRIPT_NAME");
@ -94,7 +118,6 @@ class Bootstrap {
define("INDEX_HREF", $index_href);
}
private function setup_cache() {
define("CACHE_HREF", Util::normalize_path(APP_HREF . "/cache", true));
@ -102,7 +125,6 @@ class Bootstrap {
define("HAS_WRITABLE_CACHE", @is_writable(CACHE_PATH));
}
private function setup_ext_cmds() {
define("CMDS_PATH", Util::normalize_path(CACHE_PATH . "/cmds.json", false));

View file

@ -1,4 +1,3 @@
- var app_href = "<?= APP_HREF; ?>"
- var fallback = "<?= FALLBACK; ?>"

View file

@ -0,0 +1,14 @@
<?php
define("MIN_PHP_VERSION", "5.4.0");
define("HAS_MIN_PHP_VERSION", version_compare(PHP_VERSION, MIN_PHP_VERSION) >= 0);
if (!HAS_MIN_PHP_VERSION) {
header("Content-type: application/json;charset=utf-8");
echo json_encode(array(
"err" => "ERR_PHP",
"msg" => "PHP " . MIN_PHP_VERSION . "+ required, only found " . PHP_VERSION,
"ver" => PHP_VERSION
));
exit;
}

View file

@ -1,26 +1,8 @@
<?php
define("MIN_PHP_VERSION", "5.4.0");
define("HAS_MIN_PHP_VERSION", version_compare(PHP_VERSION, MIN_PHP_VERSION) >= 0);
define("BASE_PATH", preg_replace("#[\\\\/]+#", "/", dirname(__FILE__)));
if (!HAS_MIN_PHP_VERSION) {
header("Content-type: application/json;charset=utf-8");
echo json_encode(array(
"err" => "ERR_PHP",
"msg" => "PHP " . MIN_PHP_VERSION . "+ required, only found " . PHP_VERSION,
"ver" => PHP_VERSION
));
exit;
}
require_once(BASE_PATH . "/inc/version-check.php");
require_once(BASE_PATH . "/inc/class-bootstrap.php");
function normalized_require_once($lib) {
require_once(preg_replace("#[\\\\/]+#", "/", dirname(__FILE__) . "/${lib}.php"));
}
function __autoload($class_name) {
normalized_require_once("inc/class-" . strtolower($class_name));
}
Bootstrap::run();
(new Bootstrap(BASE_PATH))->run();