From c0dc531ebefd8912819f3b6c8bda1fed3c7e750c Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Wed, 31 Jan 2024 06:11:00 +0300 Subject: make it simple, but not simpler --- init.php | 94 +++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 31 deletions(-) (limited to 'init.php') diff --git a/init.php b/init.php index 6d9f5f0..b139624 100644 --- a/init.php +++ b/init.php @@ -2,68 +2,100 @@ error_reporting(E_ALL); ini_set('display_errors', 1); +ini_set('assert.exception', 1); date_default_timezone_set('Europe/Moscow'); mb_internal_encoding('UTF-8'); mb_regex_encoding('UTF-8'); -define('ROOT', __DIR__); +const APP_ROOT = __DIR__; define('START_TIME', microtime(true)); -set_include_path(get_include_path().PATH_SEPARATOR.ROOT); +set_include_path(get_include_path().PATH_SEPARATOR.APP_ROOT); spl_autoload_register(function($class) { - if (str_ends_with($class, 'Exception')) { - $path = ROOT.'/engine/exceptions/'.$class.'.php'; - } else if (in_array($class, ['MySQLConnection', 'SQLiteConnection', 'CommonDatabase'])) { - $path = ROOT.'/engine/database/'.$class.'.php'; - } else if (str_starts_with($class, 'handler\\')) { - $path = ROOT.'/'.str_replace('\\', '/', $class).'.php'; - } + static $libs = [ + 'lib/tags' => ['Tag', 'tags'], + 'lib/pages' => ['Page', 'pages'], + 'lib/posts' => ['Post', 'posts'], + 'lib/uploads' => ['Upload', 'uploads'], + 'engine/model' => ['model'], + 'engine/skin' => ['SkinContext'], + ]; - if (isset($path)) { - if (!is_file($path)) - return; + if (str_ends_with($class, 'Handler')) { + $path = APP_ROOT.'/handler/'.str_replace('\\', '/', $class).'.php'; } else { - foreach (['engine', 'lib', 'model'] as $dir) { - if (is_file($path = ROOT.'/'.$dir.'/'.$class.'.php')) + foreach ($libs as $lib_file => $class_names) { + if (in_array($class, $class_names)) { + $path = APP_ROOT.'/'.$lib_file.'.php'; break; + } } } + if (!isset($path)) + $path = APP_ROOT.'/lib/'.$class.'.php'; + + if (!is_file($path)) + return; + require_once $path; }); -$config = require_once 'config.php'; -if (file_exists(ROOT.'/config-local.php')) { - $config = array_replace($config, require 'config-local.php'); -} +if (!file_exists(APP_ROOT.'/config.yaml')) + die('Fatal: config.yaml not found'); + +$config = yaml_parse_file(APP_ROOT.'/config.yaml'); +if ($config === false) + die('Fatal: failed to parse config.yaml'); -// turn off errors output on production domains +// i know what i'm doing. do you? +umask($config['umask']); require_once 'functions.php'; +require_once 'engine/mysql.php'; +require_once 'engine/router.php'; +require_once 'engine/request.php'; +require_once 'engine/logging.php'; -if (PHP_SAPI == 'cli') { - $_SERVER['HTTP_HOST'] = $config['domain']; - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; -} else { - if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) - $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP']; +try { + if (is_cli()) { + verify_hostname($config['domain']); + $_SERVER['HTTP_HOST'] = $config['domain']; + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + } else { + verify_hostname(); + if (array_key_exists('HTTP_X_REAL_IP', $_SERVER)) + $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_REAL_IP']; + + require_once 'engine/strings.php'; + require_once 'engine/skin.php'; + require_once 'lib/admin.php'; + } +} catch (RuntimeException $e) { + die('Fatal error: '.$e->getMessage()); } -if (!$config['is_dev']) { - if (file_exists(ROOT.'/config-static.php')) +$__logger = is_dev() + ? new FileLogger(APP_ROOT.'/log/debug.log') + : new DatabaseLogger(); +$__logger->enable(); + +if (!is_dev()) { + if (file_exists(APP_ROOT.'/config-static.php')) $config['static'] = require_once 'config-static.php'; else die('confic-static.php not found'); -} -if (!$config['is_dev']) { + // turn off errors output on production domains error_reporting(0); ini_set('display_errors', 0); } -logging::setLogFile($config['log_file']); -logging::enable(); +if (!is_cli()) { + $__lang = Strings::getInstance(); + $__lang->load('main'); +} require 'vendor/autoload.php'; -- cgit v1.2.3