load config file from different locations, including phar
[bdrem.git] / src / bdrem / Config.php
index 5d34f7607fe25409c2b4059a7a9ac3319cf5d89c..a79b1b3c5b53ec21799689f9903d057a1189d448 100644 (file)
@@ -4,28 +4,48 @@ namespace bdrem;
 class Config
 {
     public $source;
-    public $daysBefore;
-    public $daysAfter;
+    public $date;
+    public $daysPrev = 3;
+    public $daysNext = 7;
     public $locale;
+    public $stopOnEmpty = false;
+
+    public $cfgFiles = array();
+    public $cfgFileExists;
 
     public function load()
     {
-        $f = __DIR__ . '/../../data/bdrem.config.php';
-        if (file_exists($f)) {
-            return $this->loadFile($f);
+        $this->loadConfigFilePaths();
+        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)
     {
         include $filename;
-        $this->source = $source;
-        $this->daysBefore = $daysBefore;
-        $this->daysAfter = $daysAfter;
-        if (isset($locale)) {
-            $this->locale = $locale;
+        $vars = get_defined_vars();
+        foreach ($vars as $k => $value) {
+            $this->$k = $value;
         }
     }
 
@@ -40,5 +60,23 @@ 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 == '') {
+            return $default;
+        }
+        return $this->$varname;
+    }
 }
 ?>