Added config manager package

This commit is contained in:
Alejandro Celaya 2016-07-19 16:30:48 +02:00
parent 5536d05e99
commit 5eefaf3071
3 changed files with 13 additions and 23 deletions

View File

@ -1,7 +1,7 @@
{
"name": "acelaya/url-shortener",
"name": "shlinkio/shlink",
"type": "project",
"homepage": "https://github.com/acelaya/url-shortener",
"homepage": "http://shlink.io",
"license": "MIT",
"authors": [
{
@ -19,6 +19,8 @@
"zendframework/zend-stdlib": "^2.7",
"zendframework/zend-servicemanager": "^3.0",
"zendframework/zend-paginator": "^2.6",
"zendframework/zend-config": "^2.6",
"mtymek/expressive-config-manager": "^0.4",
"doctrine/orm": "^2.5",
"guzzlehttp/guzzle": "^6.2",
"acelaya/zsm-annotated-services": "^0.2.0",

View File

@ -3,7 +3,7 @@
return [
'debug' => false,
'config_cache_enabled' => false,
'config_cache_enabled' => true,
'zend-expressive' => [
'error_handler' => [

View File

@ -1,6 +1,6 @@
<?php
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\Glob;
use Zend\Expressive\ConfigManager\ConfigManager;
use Zend\Expressive\ConfigManager\ZendConfigProvider;
/**
* Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
@ -11,22 +11,10 @@ use Zend\Stdlib\Glob;
* Obviously, if you use closures in your config you can't cache it.
*/
$cachedConfigFile = 'data/cache/app_config.php';
return call_user_func(function () {
$configManager = new ConfigManager([
new ZendConfigProvider('config/autoload/{{,*.}global,{,*.}local}.php')
], 'data/cache/app_config.php');
$config = [];
if (is_file($cachedConfigFile)) {
// Try to load the cached config
$config = include $cachedConfigFile;
} else {
// Load configuration from autoload path
foreach (Glob::glob('config/autoload/{{,*.}global,{,*.}local}.php', Glob::GLOB_BRACE) as $file) {
$config = ArrayUtils::merge($config, include $file);
}
// Cache config if enabled
if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
file_put_contents($cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
}
}
return $config;
return $configManager->getMergedConfig();
});