/** * @param string $name * @return mixed */ public function get($name) { if (ArrayUtils::hasInArray($this->conf, $name)) { return ArrayUtils::getInArray($this->conf, $name); } else { if ($this->redis) { $f = $this->getField($name); $v = $this->redis->hget($this->key, $f); if (!is_null($v)) { $v = unserialize($v); $this->conf[$f] = $v; return ArrayUtils::getInArray($this->conf, $name); } } } return null; }
public function testArrayUtils() { $arr = ['a' => 'val1', 'b' => ['bb1' => 'val2', 'bb2' => ['bbb1' => 'val3']], 'c' => ['a', 'b', 'c'], 'd' => true, 'e' => ['ee1' => null, 'ee2' => [1, 2]]]; $tests1 = ['a' => true, 'b' => true, 'b.bb1' => true, 'b.bb2.bbb1' => true, 'b.bb3' => false, 'c' => true, 'c.0' => true, 'c.3' => false, 'd' => true, 'd.a' => false, 'e' => true, 'e.ee1' => true, 'e.ee2.0' => true, 'e.ee3' => false, '.e' => false, 'e.' => false]; foreach ($tests1 as $t => $exp) { $this->assertEquals($exp, \Wwtg99\Config\Common\ArrayUtils::hasInArray($arr, $t), "Test {$t}"); } $tests2 = ['a' => 'val1', 'b.bb1' => 'val2', 'b.bb2.bbb1' => 'val3', 'b.bb3' => null, 'c' => ['a', 'b', 'c'], 'c.1' => 'b', 'd' => true, 'd.a' => null, 'e.ee1' => null, 'e.ee2' => [1, 2], 'e.ee2.0' => 1, 'e.ee3' => null, '.e' => null, 'e.' => null]; foreach ($tests2 as $t => $exp) { $this->assertEquals($exp, \Wwtg99\Config\Common\ArrayUtils::getInArray($arr, $t), "Test {$t}"); } $this->assertEquals('aa', \Wwtg99\Config\Common\ArrayUtils::getInArray($arr, 'f', 'aa'), "Test default value"); $this->assertEquals(null, \Wwtg99\Config\Common\ArrayUtils::getInArray($arr, 'e.ee1', 'aa'), "Test default value"); $arr = []; $this->assertEquals(['a' => 'val1'], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'a', 'val1')); $this->assertEquals(['a' => 'val1'], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'a.aa', 'val2')); $this->assertEquals(['a' => 'val1', 'b' => ['bb' => 'val2']], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'b.bb', 'val2')); $this->assertEquals(['a' => 'val1', 'b' => ['bb' => 'val2', 'bbb' => [1, 2]]], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'b.bbb', [1, 2])); $this->assertEquals(['a' => 'val1', 'b' => ['bb' => 'val2', 'bbb' => [1, 2, 3]]], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'b.bbb.2', 3)); $this->assertEquals(['a' => 'val1', 'b' => ['bb' => 'val2', 'bbb' => [1, 2, 3]], 'c' => null], \Wwtg99\Config\Common\ArrayUtils::setInArray($arr, 'c', null)); }
/** * @param string $name * @param $defval * @return mixed */ public function get($name, $defval = null) { if ($this->useCache) { return ArrayUtils::getInArray($this->conf, $name, $defval); } else { $re = ArrayUtils::hasInArray($this->conf, $name); if ($re) { return ArrayUtils::getInArray($this->conf, $name, $defval); } else { foreach ($this->sources as $source) { if ($source instanceof IConfigSource) { $re = $source->get($name); if (!is_null($re)) { $this->set($name, $re); return $re; } } } } } return $defval; }
/** * @param string $name * @return mixed */ public function get($name) { return ArrayUtils::getInArray($this->conf, $name); }