show error, not exception
[bdrem.git] / src / bdrem / Config.php
index 076cd043fab05b85efba646d437024500786ab79..a79b1b3c5b53ec21799689f9903d057a1189d448 100644 (file)
@@ -4,25 +4,49 @@ 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;
+    }
+
+    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)
     {
         include $filename;
-        $this->source = $source;
-        $this->daysBefore = $daysBefore;
-        $this->daysAfter = $daysAfter;
+        $vars = get_defined_vars();
+        foreach ($vars as $k => $value) {
+            $this->$k = $value;
+        }
     }
 
     public function loadSource()
@@ -35,9 +59,24 @@ class Config
         $class = '\\bdrem\\Source_' . array_shift($settings);
 
         return new $class($settings[0]);
-        //$rm = new \ReflectionMethod($class, '__construct');
-        //return $rm->invokeArgs(null, $settings);
-        //return call_user_func_array($class . '::__construct', $settings);
+    }
+
+    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;
     }
 }
 ?>