add option to specify config file
[bdrem.git] / src / bdrem / Config.php
index 04cf8882a3d3e23a6e313d59413d5c6c037cbcbf..57d0cfd7d1995527cb8163b3bb0355756071e417 100644 (file)
@@ -3,20 +3,48 @@ namespace bdrem;
 
 class Config
 {
-    public $source;
     public $date;
     public $daysPrev;
     public $daysNext;
     public $locale;
+    public $renderer;
+    public $source;
+    public $stopOnEmpty;
+
+    public $cfgFiles = array();
+    public $cfgFileExists;
+
+
+
+    public function __construct()
+    {
+        $this->loadConfigFilePaths();
+    }
 
     public function load()
     {
-        $f = __DIR__ . '/../../data/bdrem.config.php';
-        if (file_exists($f)) {
-            return $this->loadFile($f);
+        foreach ($this->cfgFiles as $file) {
+            if (file_exists($file)) {
+                $this->cfgFileExists = true;
+                return $this->loadFile($file);
+            }
         }
+        $this->cfgFileExists = false;
+    }
 
-        throw new \Exception('No config file found');
+    protected function loadConfigFilePaths()
+    {
+        $pharFile = \Phar::running();
+        if ($pharFile == '') {
+            $this->cfgFiles[] = __DIR__ . '/../../data/bdrem.config.php';
+        } else {
+            //remove phar:// from the path
+            $this->cfgFiles[] = substr($pharFile, 7) . '.config.php';
+        }
+
+        //TODO: add ~/.config/bdrem.php
+
+        $this->cfgFiles[] = '/etc/bdrem.php';
     }
 
     protected function loadFile($filename)
@@ -24,7 +52,9 @@ class Config
         include $filename;
         $vars = get_defined_vars();
         foreach ($vars as $k => $value) {
-            $this->$k = $value;
+            if (!isset($this->$k) || $this->$k === null) {
+                $this->$k = $value;
+            }
         }
     }
 
@@ -40,6 +70,16 @@ class Config
         return new $class($settings[0]);
     }
 
+    public function setDate($date)
+    {
+        if ($date === null) {
+            $this->date = date('Y-m-d');
+        } else {
+            $dt = new \DateTime($date);
+            $this->date = $dt->format('Y-m-d');
+        }
+    }
+
     public function get($varname, $default = '')
     {
         if (!isset($this->$varname) || $this->$varname == '') {