Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
⏪ (Config) Add back getAll() and getSection() for Config Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jan 31, 2019
1 parent 2c571ed commit efec743
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions framework/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(array $config = [])
}
}

public function get(string $name)
public function get(string $name, $throw = true)
{
$setting = $this->cacheTable->get($name, $this->valueField);
// First Check config stored in RedisConnection Cache, If it exist , then just return the cached key
Expand All @@ -41,13 +41,29 @@ public function get(string $name)
$setting = app()->pdo->createCommand("SELECT `value` from `site_config` WHERE `name` = :name")
->bindParams(["name" => $name])->queryScalar();
// In this case (Load config From Database Failed) , A Exception should throw
if ($setting === false) throw $this->createNotFoundException($name);
if ($setting === false && $throw) throw $this->createNotFoundException($name);

$this->cacheTable->set($name, [$this->valueField => $setting]);
}
return $setting;
}

public function getAll()
{
$settings = [];
foreach ($this->cacheTable as $k => $v) {
$settings[$k] = $v[$this->valueField];
}
return $settings;
}

public function getSection($prefix = null)
{
return array_filter($this->getAll(), function ($k) use ($prefix) {
return strpos($k, $prefix) === 0;
}, ARRAY_FILTER_USE_KEY);
}

public function set(string $name, $value)
{
app()->pdo->createCommand("UPDATE `site_config` SET `value` = :val WHERE `name` = :name")->bindParams([
Expand Down

0 comments on commit efec743

Please sign in to comment.