From 988a23fd024b065811185fca517939e59d561473 Mon Sep 17 00:00:00 2001 From: Daniel Rudolf Date: Sun, 6 Mar 2016 20:54:58 +0100 Subject: [PATCH] Modular config: Load config from any config/*.config.php Resolves #330 After loading the `config/config.php`, Pico proceeds with any existing `config/*.config.php` in alphabetical order. The file order is crucial: Config values which has been set already, cannot be overwritten by a succeeding file. This is also true for arrays, i.e. when specifying `$config['test'] = array('foo' => 'bar')` in `config/a.config.php` and `$config['test'] = array('baz' => 42)` in `config/b.config.php`, `$config['test']['baz']` will be undefined --- lib/Pico.php | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/Pico.php b/lib/Pico.php index 59e4eae..25e4938 100644 --- a/lib/Pico.php +++ b/lib/Pico.php @@ -506,7 +506,15 @@ class Pico } /** - * Loads the config.php from Pico::$configDir + * Loads the config.php and any *.config.php from Pico::$configDir + * + * After loading the {@path "config/config.php"}, Pico proceeds with any + * existing {@path "config/*.config.php"} in alphabetical order. The file + * order is crucial: Config values which has been set already, cannot be + * overwritten by a succeeding file. This is also true for arrays, + * i.e. when specifying `$config['test'] = array('foo' => 'bar')` in + * `config/a.config.php` and `$config['test'] = array('baz' => 42)` in + * `config/b.config.php`, `$config['test']['baz']` will be undefined! * * @see Pico::setConfig() * @see Pico::getConfig() @@ -514,20 +522,33 @@ class Pico */ protected function loadConfig() { - $config = null; + // scope isolated require() + $includeClosure = function ($configFile) { + require($configFile); + return (isset($config) && is_array($config)) ? $config : array(); + }; + if (PHP_VERSION_ID >= 50400) { + $includeClosure = $includeClosure->bindTo(null); + } + + // load main config file (config/config.php) + $this->config = is_array($this->config) ? $this->config : array(); if (file_exists($this->getConfigDir() . 'config.php')) { - // scope isolated require() - $includeClosure = function ($configFile) use (&$config) { - require($configFile); - }; - if (PHP_VERSION_ID >= 50400) { - $includeClosure = $includeClosure->bindTo(null); - } + $this->config += $includeClosure($this->getConfigDir() . 'config.php'); + } - $includeClosure($this->getConfigDir() . 'config.php'); + // merge $config of config/*.config.php files + $configFiles = glob($this->getConfigDir() . '?*.config.php', GLOB_MARK); + if ($configFiles) { + foreach ($configFiles as $configFile) { + if (substr($configFile, -1) !== '/') { + $this->config += $includeClosure($configFile); + } + } } - $defaultConfig = array( + // merge default config + $this->config += array( 'site_title' => 'Pico', 'base_url' => '', 'rewrite_url' => null, @@ -541,9 +562,6 @@ class Pico 'timezone' => '' ); - $this->config = is_array($this->config) ? $this->config : array(); - $this->config += is_array($config) ? $config + $defaultConfig : $defaultConfig; - if (empty($this->config['base_url'])) { $this->config['base_url'] = $this->getBaseUrl(); } else {