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

Commit

Permalink
refactor(Config): Add define of config key type and can add runtime c…
Browse files Browse the repository at this point in the history
…onfig
  • Loading branch information
Rhilip committed Aug 13, 2019
1 parent 4d573e2 commit d57aede
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 152 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- **template:** Add git commit hash in `CHANGELOG.md` (76bc527)

### Feat
- **Auth:** Add Auth By passkey support for special route (aff1f87)
- **Auth:** Use JWT to set cookies content (bf897c6)
- **Auth:** Sep Auth part from Site to new components (f36884e)
- **Auth/Login:** Add full Advanced Options support (6009dc8)
Expand Down
1 change: 0 additions & 1 deletion apps/config/httpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

'log' => [
'class' => Rid\Component\Log::class,
'level' => ['error', 'info', 'debug'],
'logDir' => 'logs',
'logRotate' => Rid\Component\Log::ROTATE_DAY,
'maxFileSize' => 1024, // bytes
Expand Down
72 changes: 43 additions & 29 deletions framework/Component/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,83 @@

use Rid\Base\Component;
use Rid\Exceptions\ConfigException;
use Swoole\Table;

class Config extends Component
{
/** @var \Swoole\Table */
/** @var Table */
private $cacheTable;

private $valueField = 'data';

public function onInitialize(array $config = [])
{
// Get \Swoole\Table object From \Server, So that we can share same dynamic config
$this->cacheTable = app()->getServ()->configTable;

if ($this->cacheTable->count() == 0 && app()->getWorkerId() == 0) {
$configs = app()->pdo->createCommand('SELECT `name`,`value` FROM `site_config`')->queryAll();
$configs = app()->pdo->createCommand('SELECT `name`, `value`, `type` FROM `site_config`')->queryAll();
foreach ($configs as $config) {
$this->cacheTable->set($config['name'], [$this->valueField => $config['value']]);
$this->load($config);
}
println('Load Dynamic Site Config Success, Get ' . count($configs) . ' configs.');
}
}

private function load($config)
{
$this->cacheTable->set($config['name'], ['value' => $config['value'], 'type' => $config['type']]);
}

public function get(string $name)
{
// First Check config stored in Swoole Table. If it exist , then just return the cached key
if (false === $setting = $this->cacheTable->get($name, $this->valueField)) {
// Deal with config with prefix `route.`
if (strpos($name,'route.') !== 0) {
// Get config From Database
$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 new ConfigException(sprintf('Dynamic Setting "%s" couldn\'t be found.', $name));
}
if (false === $setting_row = $this->cacheTable->get($name)) {
if (strpos($name, 'runtime.') === 0) return false; // Deal with config with prefix `runtime.`
if (strpos($name, 'route.') === 0) return 1; // Deal with config with prefix `route.`

// Get config From Database
$setting_row = app()->pdo->createCommand('SELECT `name`, `value`, `type` from `site_config` WHERE `name` = :name')
->bindParams(['name' => $name])->queryOne();

$this->cacheTable->set($name, [$this->valueField => $setting]);
// In this case (Load config From Database Failed) , A Exception should throw
if ($setting_row === false)
throw new ConfigException(sprintf('Dynamic Setting "%s" couldn\'t be found.', $name));

$this->load($setting_row);
}

$setting = $setting_row['value']; // Type String
if ($setting_row['type'] == 'json') $setting = json_decode($setting,true);
elseif ($setting_row['type'] == 'int') $setting = (int) $setting;
elseif ($setting_row['type'] == 'bool') $setting = (bool) $setting;

return $setting;
}

public function getAll()
public function getSection($prefix = null)
{
$settings = [];
foreach ($this->cacheTable as $k => $v) {
$settings[$k] = $v[$this->valueField];
if (!is_null($prefix) && strpos($k, $prefix) !== 0) continue;
$settings[$k] = $this->get($k);
}
return $settings;
}

public function getSection($prefix = null)
public function set(string $name, $value, $type = null)
{
return array_filter($this->getAll(), function ($k) use ($prefix) {
return strpos($k, $prefix) === 0;
}, ARRAY_FILTER_USE_KEY);
}
// Judge order: input -> pre-defined -> is_array so `json` -> default `string`
$type = $type ?? ($this->cacheTable->get($name, 'type') ?: (is_array($value) ? 'json' : 'string'));
$value = ($type == 'json') ? json_encode($value) : (string) $value; // array(json), bool, int -> string

public function set(string $name, $value)
{
app()->pdo->createCommand("UPDATE `site_config` SET `value` = :val WHERE `name` = :name")->bindParams([
"val" => $value, "name" => $name
])->execute();
return $this->flush($name);
$this->cacheTable->set($name, ['value' => $value, 'type' => $type]);
println(sprintf('Set new Dynamic Setting "%s", Type: "%s", Value: "%s".', $name, $type, $value));

// Update site_config if not a runtime setting
if (strpos($name, 'runtime.') === false) {
app()->pdo->createCommand('UPDATE `site_config` SET `value` = :val WHERE `name` = :name')->bindParams([
'val' => $value, 'name' => $name
])->execute();
}
}

public function flush($name)
Expand Down
7 changes: 4 additions & 3 deletions framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public function start()
// 欢迎信息
$this->welcome();

// rid-httpd 模式下,在此处创建全局的 \Swoole\Table
$configTable = new \Swoole\Table(2048);
$configTable->column('data', \Swoole\Table::TYPE_STRING, 256);
// 在此处创建全局的 \Swoole\Table
$configTable = new \Swoole\Table(4096);
$configTable->column('value' , \Swoole\Table::TYPE_STRING, 4096);
$configTable->column('type', \Swoole\Table::TYPE_STRING, 64);
$configTable->create();
$this->_server->configTable = $configTable;

Expand Down
1 change: 1 addition & 0 deletions framework/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function config(string $config)
function println($expression)
{
echo date('Y-m-d H:i:s') . ' ' . $expression . PHP_EOL;
app()->log->notice($expression);
}
}

Expand Down
Loading

0 comments on commit d57aede

Please sign in to comment.