load config file from different locations, including phar
authorChristian Weiske <cweiske@cweiske.de>
Tue, 25 Feb 2014 06:47:24 +0000 (07:47 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 25 Feb 2014 06:47:24 +0000 (07:47 +0100)
src/bdrem/Config.php
src/bdrem/UserInterface.php

index 65e353d..a79b1b3 100644 (file)
@@ -9,19 +9,37 @@ class Config
     public $daysNext = 7;
     public $locale;
     public $stopOnEmpty = false;
     public $daysNext = 7;
     public $locale;
     public $stopOnEmpty = false;
+
+    public $cfgFiles = array();
     public $cfgFileExists;
 
     public function load()
     {
     public $cfgFileExists;
 
     public function load()
     {
-        $f = __DIR__ . '/../../data/bdrem.config.php';
-        if (file_exists($f)) {
-            $this->cfgFileExists = true;
-            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;
     }
 
         $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';
+        }
+
+        //TODO: add ~/.config/bdrem.php
+
+        $this->cfgFiles[] = '/etc/bdrem.php';
+    }
+
     protected function loadFile($filename)
     {
         include $filename;
     protected function loadFile($filename)
     {
         include $filename;
index 4e27d5d..9fd4d69 100644 (file)
@@ -17,7 +17,10 @@ abstract class UserInterface
             $this->handleCommands($res);
 
             if (!$this->config->cfgFileExists) {
             $this->handleCommands($res);
 
             if (!$this->config->cfgFileExists) {
-                throw new \Exception('No config file found');
+                throw new \Exception(
+                    "No config file found. Looked at the following places:\n"
+                    . '- ' . implode ("\n- ", $this->config->cfgFiles)
+                );
             }
 
             $source = $this->config->loadSource();
             }
 
             $source = $this->config->loadSource();