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');
}
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;
}
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)
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);
}
}
+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);