From: Andrey Kutejko Date: Wed, 17 Dec 2014 18:50:52 +0000 (+0200) Subject: settings class X-Git-Tag: 0.6~116 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=c1dc2996c8d81989e4d18425443b41f866a4204f;p=ipf.git settings class --- diff --git a/ipf.php b/ipf.php index 4e1fba6..3304c69 100644 --- a/ipf.php +++ b/ipf.php @@ -7,56 +7,27 @@ final class IPF return '0.5 beta'; } - public static $settings = array( - 'app_base' => '', - 'debug' => false, - 'media_url' => '/media/', - 'upload_url' => '/media/upload/', - 'static_url' => '/static/', - 'session_cookie_id' => 'sessionid', - 'dir_permission' => 0777, - 'file_permission' => 0666, - 'time_zone' => 'America/Toronto', - 'tmp' => '/tmp', - 'admin_title' => 'IPF Administration', - ); - - private static function applySettings($settings) - { - foreach ($settings as $key => $val) - IPF::$settings[strtolower($key)] = $val; - } + public static $settings = null; - private static function loadSettings() + private static function checkSettings($settings) { - $settings_file = IPF::$settings['project_path'].DIRECTORY_SEPARATOR.'settings.php'; - IPF::$settings['settings_file'] = $settings_file; - - if (!file_exists($settings_file)) - die('Configuration file does not exist: '.$settings_file); + $db = $settings->get('database'); - IPF::applySettings(require $settings_file); - - $settings_local_file = IPF::$settings['project_path'].DIRECTORY_SEPARATOR.'settings_local.php'; - if (file_exists($settings_local_file)) - IPF::applySettings(require $settings_local_file); - - if (!isset(IPF::$settings['database'])) + if (!$db) die('Please specify database parameters in settings file'); - if (!is_array(IPF::$settings['database'])) + if (!is_array($db)) die('Database must be array with keys: driver, host, port (optional), database, username, password'); - if (!is_string(IPF::$settings['tmp'])) - die('TMP must be string'); + $apps = $settings->get('applications'); - if (!isset(IPF::$settings['applications'])) + if (!$apps) die('Please specify application list'); - if (!is_array(IPF::$settings['applications'])) + if (!is_array($apps)) die('applications must be array of string'); - if (!isset(IPF::$settings['urls'])) + if (!$settings->get('urls')) die('Specify site url routes'); } @@ -72,12 +43,29 @@ final class IPF if (php_sapi_name() === 'cli-server' && IPF::requestedFileExists()) return false; - IPF::$settings['ipf_path'] = dirname(__FILE__); - IPF::$settings['project_path'] = $project_path; - IPF::$settings['document_root'] = $document_root; - - IPF::loadSettings(); - date_default_timezone_set(IPF::$settings['time_zone']); + self::$settings = IPF_Settings::create() + ->apply(array( + 'app_base' => '', + 'debug' => false, + 'media_url' => '/media/', + 'upload_url' => '/media/upload/', + 'static_url' => '/static/', + 'session_cookie_id' => 'sessionid', + 'dir_permission' => 0777, + 'file_permission' => 0666, + 'time_zone' => 'America/Toronto', + 'tmp' => '/tmp', + 'admin_title' => 'IPF Administration', + )) + ->set('ipf_path', dirname(__FILE__)) + ->set('project_path', $project_path) + ->set('document_root', $document_root) + ->applyFile($project_path.'/settings.php') + ->tryApplyFile($project_path.'/settings_local.php'); + + self::checkSettings(self::$settings); + + date_default_timezone_set(self::$settings->get('time_zone')); return true; } @@ -87,9 +75,7 @@ final class IPF public static function get($name, $default=null) { - if (isset(IPF::$settings[$name])) - return IPF::$settings[$name]; - return $default; + return self::$settings->get($name, $default); } private static function include_existing($filename) @@ -113,8 +99,8 @@ final class IPF array_pop($elts); $file = '/' . strtolower(implode(DIRECTORY_SEPARATOR, $elts)).'.php'; - self::include_existing(IPF::$settings['ipf_path'] . $file); - self::include_existing(IPF::$settings['project_path'] . $file); + self::include_existing(self::$settings->get('ipf_path') . $file); + self::include_existing(self::$settings->get('project_path') . $file); if (!function_exists($function)) throw new IPF_Exception('Impossible to load the function: '.$function.' in '.$file); @@ -135,6 +121,48 @@ final class IPF } } +class IPF_Settings +{ + private $settings = array(); + + static function create() + { + return new self; + } + + function set($name, $value) + { + $this->settings[strtolower($name)] = $value; + return $this; + } + + function get($name, $default=null) + { + $name = strtolower($name); + return array_key_exists($name, $this->settings) ? $this->settings[$name] : $default; + } + + function apply($settings) + { + foreach ($settings as $key => $val) + $this->set($key, $val); + return $this; + } + + function applyFile($file) + { + return $this->apply(include $file); + } + + function tryApplyFile($file) + { + if (file_exists($file)) + return $this->apply(include $file); + else + return $this; + } +} + function __($str) { $t = trim($str); diff --git a/ipf/auth/app.php b/ipf/auth/app.php index e7f89a2..4626c1e 100644 --- a/ipf/auth/app.php +++ b/ipf/auth/app.php @@ -6,7 +6,7 @@ class IPF_Auth_App extends IPF_Application function configure($settings) { - $this->userModel = \PFF\Arr::get($settings, 'auth_user_model', 'IPF\\Auth\\User'); + $this->userModel = $settings->get('auth_user_model', 'IPF\\Auth\\User'); \PFF\Container::set('auth', $this); }