Improve ConfigHelper::consolidate() to allow use of filters

This commit is contained in:
Kijin Sung 2016-05-19 21:02:39 +09:00
parent 557d338427
commit 0961253a74
2 changed files with 26 additions and 14 deletions

View file

@ -43,22 +43,32 @@ class ConfigHelper
*/
protected static function _parseConfigValue(array $value)
{
$filters = array();
$result = null;
foreach ($value as $option)
{
$option = array_map('trim', explode(':', $option, 2));
if (count($option) === 1)
{
$result = Config::get($option[0]);
if ($result !== null)
if (function_exists($option[0]))
{
return $result;
$filters[] = $option[0];
}
}
elseif ($result !== null)
{
continue;
}
elseif ($option[0] === 'common')
{
$result = Config::get($option[1]);
}
else
{
if (!isset(self::$_config_cache[$option[0]]))
{
self::$_config_cache[$option[0]] = getModel('module')->getModuleConfig($option[0]) ?: null;
self::$_config_cache[$option[0]] = getModel('module')->getModuleConfig($option[0]) ?: new stdClass;
}
$options = explode('.', $option[1]);
$temp = self::$_config_cache[$option[0]];
@ -77,13 +87,15 @@ class ConfigHelper
$temp = null;
}
}
if ($temp !== null)
{
return $temp;
}
$result = $temp;
}
}
return null;
foreach ($filters as $filter)
{
$result = $filter($result);
}
return $result;
}
}

View file

@ -8,15 +8,15 @@ class ConfigHelperTest extends \Codeception\TestCase\Test
{
$member_config = getModel('module')->getModuleConfig('member');
$consolidated = ConfigHelper::consolidate(array(
'dbtype' => array('db.type', 'member:nosuchconfig'),
'member' => array('no.such.config', 'member:enable_join'),
'nosuch' => array('no.such.config', 'member:no.such.config.either'),
'dbtype' => array('common:db.type', 'member:nosuchconfig'),
'member' => array('common:no.such.config', 'member:enable_join', 'tobool'),
'nosuch' => array('common:no.such.config', 'member:no.such.config.either', 'intval'),
'single' => 'member:identifier',
));
$this->assertEquals(config('db.type'), $consolidated['dbtype']);
$this->assertEquals($member_config->enable_join, $consolidated['member']);
$this->assertNull($consolidated['nosuch']);
$this->assertEquals(tobool($member_config->enable_join), $consolidated['member']);
$this->assertEquals(0, $consolidated['nosuch']);
$this->assertEquals($member_config->identifier, $consolidated['single']);
}
}