mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-01-08 11:11:39 +09:00
Improve ConfigHelper::consolidate() to allow use of filters
This commit is contained in:
parent
557d338427
commit
0961253a74
2 changed files with 26 additions and 14 deletions
|
|
@ -43,22 +43,32 @@ class ConfigHelper
|
||||||
*/
|
*/
|
||||||
protected static function _parseConfigValue(array $value)
|
protected static function _parseConfigValue(array $value)
|
||||||
{
|
{
|
||||||
|
$filters = array();
|
||||||
|
$result = null;
|
||||||
|
|
||||||
foreach ($value as $option)
|
foreach ($value as $option)
|
||||||
{
|
{
|
||||||
$option = array_map('trim', explode(':', $option, 2));
|
$option = array_map('trim', explode(':', $option, 2));
|
||||||
if (count($option) === 1)
|
if (count($option) === 1)
|
||||||
{
|
{
|
||||||
$result = Config::get($option[0]);
|
if (function_exists($option[0]))
|
||||||
if ($result !== null)
|
|
||||||
{
|
{
|
||||||
return $result;
|
$filters[] = $option[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
elseif ($result !== null)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
elseif ($option[0] === 'common')
|
||||||
|
{
|
||||||
|
$result = Config::get($option[1]);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!isset(self::$_config_cache[$option[0]]))
|
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]);
|
$options = explode('.', $option[1]);
|
||||||
$temp = self::$_config_cache[$option[0]];
|
$temp = self::$_config_cache[$option[0]];
|
||||||
|
|
@ -77,13 +87,15 @@ class ConfigHelper
|
||||||
$temp = null;
|
$temp = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($temp !== null)
|
$result = $temp;
|
||||||
{
|
|
||||||
return $temp;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
foreach ($filters as $filter)
|
||||||
|
{
|
||||||
|
$result = $filter($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,15 +8,15 @@ class ConfigHelperTest extends \Codeception\TestCase\Test
|
||||||
{
|
{
|
||||||
$member_config = getModel('module')->getModuleConfig('member');
|
$member_config = getModel('module')->getModuleConfig('member');
|
||||||
$consolidated = ConfigHelper::consolidate(array(
|
$consolidated = ConfigHelper::consolidate(array(
|
||||||
'dbtype' => array('db.type', 'member:nosuchconfig'),
|
'dbtype' => array('common:db.type', 'member:nosuchconfig'),
|
||||||
'member' => array('no.such.config', 'member:enable_join'),
|
'member' => array('common:no.such.config', 'member:enable_join', 'tobool'),
|
||||||
'nosuch' => array('no.such.config', 'member:no.such.config.either'),
|
'nosuch' => array('common:no.such.config', 'member:no.such.config.either', 'intval'),
|
||||||
'single' => 'member:identifier',
|
'single' => 'member:identifier',
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEquals(config('db.type'), $consolidated['dbtype']);
|
$this->assertEquals(config('db.type'), $consolidated['dbtype']);
|
||||||
$this->assertEquals($member_config->enable_join, $consolidated['member']);
|
$this->assertEquals(tobool($member_config->enable_join), $consolidated['member']);
|
||||||
$this->assertNull($consolidated['nosuch']);
|
$this->assertEquals(0, $consolidated['nosuch']);
|
||||||
$this->assertEquals($member_config->identifier, $consolidated['single']);
|
$this->assertEquals($member_config->identifier, $consolidated['single']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue