From 17610c00c8ef6b589b6c3015fc9a667b949db016 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Mon, 14 Apr 2014 18:20:46 +0200 Subject: [PATCH] load config file for .phar files --- src/phancap/Config.php | 45 +++++++++++++++++++++++++++++++++++++++--- www/setup.php | 31 +++++++++++++++++++++++++---- 2 files changed, 69 insertions(+), 7 deletions(-) diff --git a/src/phancap/Config.php b/src/phancap/Config.php index 5034b0f..69b3f0a 100644 --- a/src/phancap/Config.php +++ b/src/phancap/Config.php @@ -15,6 +15,19 @@ class Config */ public $cacheDirUrl; + + /** + * List of config file paths that were tried to load + * @var array + */ + public $cfgFiles = array(); + + /** + * If a configuration file could be found + * @var boolean + */ + public $cfgFileExists; + /** * Credentials for access * @@ -77,18 +90,44 @@ class Config $this->timestampMaxAge = Options::validateAge($this->timestampMaxAge); $this->screenshotMaxAge = Options::validateAge($this->screenshotMaxAge); $this->screenshotMinAge = Options::validateAge($this->screenshotMinAge); + + $this->loadConfigFilePaths(); } public function load() { - $cfgFile = __DIR__ . '/../../data/phancap.config.php'; - if (file_exists($cfgFile)) { - $this->loadFile($cfgFile); + $this->cfgFileExists = false; + foreach ($this->cfgFiles as $file) { + if (file_exists($file)) { + $this->cfgFileExists = true; + $this->loadFile($file); + break; + } } $this->setupCheck(); } + /** + * Load possible configuration file paths into $this->cfgFiles. + * + * @return void + */ + protected function loadConfigFilePaths() + { + $pharFile = \Phar::running(); + if ($pharFile == '') { + $this->cfgFiles[] = __DIR__ . '/../../data/phancap.config.php'; + } else { + //remove phar:// from the path + $this->cfgFiles[] = substr($pharFile, 7) . '.config.php'; + } + + //TODO: add ~/.config/phancap.php + + $this->cfgFiles[] = '/etc/phancap.php'; + } + protected function loadFile($filename) { include $filename; diff --git a/www/setup.php b/www/setup.php index cbba4dd..5a9c389 100644 --- a/www/setup.php +++ b/www/setup.php @@ -29,6 +29,21 @@ try { count($config->access) . ' users may access the API' ); } + + foreach ($config->cfgFiles as $cfgFile) { + $messages[][] = array( + 'info', 'Possible config file: ' . $cfgFile + ); + } + if ($config->cfgFileExists) { + $messages[][] = array( + 'ok', 'Configuration file loaded' + ); + } else { + $messages[][] = array( + 'info', 'No configuration file found' + ); + } } catch (\Exception $e) { $messages[][] = array('err', $e->getMessage()); } @@ -58,18 +73,26 @@ $out = << phancap setup check -- 2.30.2