]> git.andy128k.dev Git - ipf.git/commitdiff
settings class
authorAndrey Kutejko <andy128k@gmail.com>
Wed, 17 Dec 2014 18:50:52 +0000 (20:50 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Wed, 17 Dec 2014 18:50:52 +0000 (20:50 +0200)
ipf.php
ipf/auth/app.php

diff --git a/ipf.php b/ipf.php
index 4e1fba6ed396732ce3dc22ddb295f8788f00253f..3304c6971a8e734ef19276783514e24f604b8cd5 100644 (file)
--- 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);
index e7f89a22e992726614e14467bf43181838e4f0cd..4626c1e2fdb7bb4308627b15b210a8c759fde3f5 100644 (file)
@@ -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);
     }