public function testLazySet()
 {
     $this->conf->lazySet('db.options.tables.author', function ($len) {
         return ['columns' => ['id' => 'INT(9) UNSIGNED NOT NULL PRIMARY KEY', 'name' => "VARCHAR({$len}) NOT NULL"]];
     }, [50]);
     $this->assertInstanceOf(ConfigRepository::LAZY_VAL_CLASS, $this->conf->asArray()['db']['options']['tables']['author']);
     $this->assertEquals('VARCHAR(50) NOT NULL', $this->conf->get('db.options.tables.author.columns.name'));
     $this->assertEquals('VARCHAR(50) NOT NULL', $this->conf->asArray()['db']['options']['tables']['author']['columns']['name']);
     $this->conf->lazySet('db.options.tables.book', function () {
         return ['columns' => ['id' => 'INT(9) UNSIGNED NOT NULL PRIMARY KEY', 'title' => 'VARCHAR(50) NOT NULL', 'author' => 'VARCHAR(50)']];
     }, null, true);
     $this->assertInstanceOf(ConfigRepository::LAZY_VAL_CLASS, $this->conf->asArray()['db']['options']['tables']['book']);
     $this->assertEquals('VARCHAR(50)', $this->conf->get('db.options.tables.book.columns.author'));
     $this->assertInstanceOf(ConfigRepository::LAZY_VAL_CLASS, $this->conf->asArray()['db']['options']['tables']['book']);
 }
Ejemplo n.º 2
0
var_export($conf->get('db.charset', 'UTF-8'));
// 'UTF-8'
echo PHP_EOL;
// remove
$conf->remove('db.password');
var_export($conf->get('db.password'));
// NULL
echo PHP_EOL;
// set deep item
$conf->set('db.options.attr.' . PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
var_export($conf->get('db.options.attr', []));
// array(3 => 2,)
echo PHP_EOL;
// lazy set
$conf->lazySet('some.heavy.setting', function ($filename) {
    echo "loading from [{$filename}]...";
    return [['name' => 'ABC']];
}, ['heavy.dat']);
var_export($conf->get('some.heavy.setting'));
// loading from [heavy.dat]...array(0 => array('name' => 'ABC',),)
echo PHP_EOL;
var_export($conf->get('some.heavy.setting'));
// array(0 => array('name' => 'ABC',),)
echo PHP_EOL;
// lazy default value
$func = function () use($conf) {
    echo 'load default.';
    return $conf->get('defaults.timeout', 200);
};
var_export($conf->get('db.options.attr.' . PDO::ATTR_TIMEOUT, $func));
// load default.200
echo PHP_EOL;