From 706cc9a042c916aa10a2ce4c41166d37ba301f6e Mon Sep 17 00:00:00 2001 From: Rhilip Date: Sat, 10 Aug 2019 11:19:41 +0800 Subject: [PATCH] refactor(Config): Remove params `$throw` in Config()->get() 1. Remove params `$throw` in Config()->get() 2. Fix behaviour of Config init so that it will not re-init in custom process --- CHANGELOG.md | 3 +++ apps/middleware/AuthByCookiesMiddleware.php | 4 +-- framework/Component/Config.php | 29 ++++++++++++--------- framework/Http/Application.php | 12 +++------ framework/Http/HttpServer.php | 2 +- framework/functions.php | 4 +-- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 065e4be..2a6ce1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ ### Docs - **template:** Add git commit hash in `CHANGELOG.md` (76bc527) +### Refactor +- **view:** Fix helper/username params (720f37e) + ## [v0.1.5-alpha] - 2019-08-09 diff --git a/apps/middleware/AuthByCookiesMiddleware.php b/apps/middleware/AuthByCookiesMiddleware.php index bfeaec3..2014f3e 100644 --- a/apps/middleware/AuthByCookiesMiddleware.php +++ b/apps/middleware/AuthByCookiesMiddleware.php @@ -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 } diff --git a/framework/Component/Config.php b/framework/Component/Config.php index 40b43ae..fe4f698 100644 --- a/framework/Component/Config.php +++ b/framework/Component/Config.php @@ -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]); } diff --git a/framework/Http/Application.php b/framework/Http/Application.php index fa0031a..fd64e5d 100644 --- a/framework/Http/Application.php +++ b/framework/Http/Application.php @@ -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; } diff --git a/framework/Http/HttpServer.php b/framework/Http/HttpServer.php index c45f010..28e3fc6 100644 --- a/framework/Http/HttpServer.php +++ b/framework/Http/HttpServer.php @@ -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 中 diff --git a/framework/functions.php b/framework/functions.php index b5108d7..97704cb 100644 --- a/framework/functions.php +++ b/framework/functions.php @@ -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); } }