show error, not exception
[bdrem.git] / src / bdrem / Config.php
index 04cf8882a3d3e23a6e313d59413d5c6c037cbcbf..a79b1b3c5b53ec21799689f9903d057a1189d448 100644 (file)
@@ -5,18 +5,39 @@ class Config
 {
     public $source;
     public $date;
-    public $daysPrev;
-    public $daysNext;
+    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;
+    }
+
+    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';
         }
 
-        throw new \Exception('No config file found');
+        //TODO: add ~/.config/bdrem.php
+
+        $this->cfgFiles[] = '/etc/bdrem.php';
     }
 
     protected function loadFile($filename)
@@ -40,6 +61,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 == '') {