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

Commit

Permalink
refactor(Config): Remove params $throw in Config()->get()
Browse files Browse the repository at this point in the history
1. Remove params `$throw` in Config()->get()
2. Fix behaviour of Config init so that it will not re-init in custom process
  • Loading branch information
Rhilip committed Aug 10, 2019
1 parent 01084c9 commit 706cc9a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
### Docs
- **template:** Add git commit hash in `CHANGELOG.md` (76bc527)

### Refactor
- **view:** Fix helper/username params (720f37e)


<a name="v0.1.5-alpha"></a>
## [v0.1.5-alpha] - 2019-08-09
Expand Down
4 changes: 2 additions & 2 deletions apps/middleware/AuthByCookiesMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function handle($callable, \Closure $next)
$controllerName . '_' . $action
)
);
$required_class = config('route.' . $route, false) ?: 1;
if ($curuser->getClass(true) < $required_class) {
$required_class = config('route.' . $route) ?: 1;
if ($curuser->getClass() < $required_class) {
return app()->response->setStatusCode(403); // FIXME redirect to /error may better
}

Expand Down
29 changes: 16 additions & 13 deletions framework/Component/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,28 @@ 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()->getWorker() == 0) {
$configs = app()->pdo->createCommand("SELECT `name`,`value` FROM `site_config`")->queryAll();
if ($this->cacheTable->count() == 0 && app()->getWorkerId() == 0) {
$configs = app()->pdo->createCommand('SELECT `name`,`value` FROM `site_config`')->queryAll();
foreach ($configs as $config) {
$this->cacheTable->set($config["name"], [$this->valueField => $config["value"]]);
$this->cacheTable->set($config['name'], [$this->valueField => $config['value']]);
}
println('Load Dynamic Site Config Success, Get ' . count($configs) . ' configs.');
}
}

public function get(string $name, bool $throw = true)
public function get(string $name)
{
$setting = $this->cacheTable->get($name, $this->valueField);
// First Check config stored in RedisConnection Cache, If it exist , then just return the cached key
if (false === $setting) {
// 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)
throw new ConfigException(sprintf("Dynamic Setting \"%s\" couldn't be found.", $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));
}

$this->cacheTable->set($name, [$this->valueField => $setting]);
}
Expand Down
12 changes: 3 additions & 9 deletions framework/Http/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,12 @@ public function setServ(\Swoole\Http\Server $serv): void
$this->_serv = $serv;
}

/**
* @return mixed
*/
public function getWorker()
public function getWorkerId():int
{
return $this->_worker;
return $this->_worker ?? -1;
}

/**
* @param mixed $worker
*/
public function setWorker($worker): void
public function setWorkerId(int $worker): void
{
$this->_worker = $worker;
}
Expand Down
2 changes: 1 addition & 1 deletion framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function onWorkerStart(\Swoole\Http\Server $server, int $workerId)
$config = require $this->virtualHost['configFile'];
$app = new Application($config);
$app->setServ($this->_server);
$app->setWorker($workerId);
$app->setWorkerId($workerId);
$app->loadAllComponents();

if ($workerId == 0) { // 将系统设置中的 Timer 添加到 worker #0 中
Expand Down
4 changes: 2 additions & 2 deletions framework/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function __($string, $avg = null, $lang = null)
}

if (!function_exists('config')) {
function config($config, $throw = true)
function config($config)
{
return app()->config->get($config, $throw);
return app()->config->get($config);
}
}

Expand Down

0 comments on commit 706cc9a

Please sign in to comment.