diff --git a/Database/Connection.php b/Database/Connection.php index fe2f3856..3effed50 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -101,6 +101,18 @@ class Connection extends Component return $this->getPdo($sql); } + + /** + * 初始化 Channel + */ + public function fill() + { + $connections = Snowflake::app()->connections; + $connections->initConnections($this->cds, true, $this->maxNumber); + $connections->initConnections($this->slaveConfig['cds'], false, $this->maxNumber); + } + + /** * @param $sql * @return PDO diff --git a/Database/DatabasesProviders.php b/Database/DatabasesProviders.php index 01669c11..e5ffb93b 100644 --- a/Database/DatabasesProviders.php +++ b/Database/DatabasesProviders.php @@ -55,6 +55,34 @@ class DatabasesProviders extends Providers } + /** + * @throws ConfigException + * @throws Exception + */ + public function createPool() + { + $databases = Config::get('databases', false, []); + if (empty($databases)) { + return; + } + $application = Snowflake::app(); + foreach ($databases as $name => $database) { + /** @var Connection $connection */ + $connection = $application->set('databases.' . $name, [ + 'class' => Connection::class, + 'id' => $database['id'], + 'cds' => $database['cds'], + 'username' => $database['username'], + 'password' => $database['password'], + 'tablePrefix' => $database['tablePrefix'], + 'maxNumber' => $database['maxNumber'], + 'slaveConfig' => $database['slaveConfig'] + ]); + $connection->fill(); + } + } + + /** * @param $name * @return array|mixed|null diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index 19ae572f..2894bd96 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -66,14 +66,13 @@ class Context extends BaseContext } if (!empty($key)) { if (!is_array(Coroutine::getContext()[$id])) { - Coroutine::getContext()[$id] = [$key => $context]; + return Coroutine::getContext()[$id] = [$key => $context]; } else { - Coroutine::getContext()[$id][$key] = $context; + return Coroutine::getContext()[$id][$key] = $context; } } else { - Coroutine::getContext()[$id] = $context; + return Coroutine::getContext()[$id] = $context; } - return $context; } /** @@ -102,8 +101,7 @@ class Context extends BaseContext if (!static::inCoroutine() || !static::hasContext($id)) { return false; } - $context = Coroutine::getContext()[$id]; - if (!isset($context[$key])) { + if (!isset(Coroutine::getContext()[$id][$key])) { return false; } return Coroutine::getContext()[$id][$key] -= 1; @@ -120,14 +118,18 @@ class Context extends BaseContext return null; } if (static::inCoroutine()) { - $array = Coroutine::getContext()[$id]; + if ($key === null) { + return Coroutine::getContext()[$id]; + } else { + return Coroutine::getContext()[$id][$key] ?? null; + } } else { - $array = static::$_requests[$id]; + if ($key === null) { + return static::$_requests[$id]; + } else { + return static::$_requests[$id][$key] ?? null; + } } - if (empty($key) || !is_array($array)) { - return $array; - } - return $array[$key]; } /** @@ -169,27 +171,19 @@ class Context extends BaseContext */ public static function hasContext($id, $key = null) { - if (static::inCoroutine()) { - $context = Coroutine::getContext(); - if (!isset($context[$id])) { - return false; - } - $data = $context[$id]; - } else { - if (!isset(static::$_requests[$id])) { - return false; - } - $data = static::$_requests[$id]; - } - if (empty($data)) { + if (!static::inCoroutine()) { return false; } - if (empty($key)) { - return true; - } else if (!is_array($data)) { + if (!isset(Coroutine::getContext()[$id])) { return false; } - return isset($data[$key]); + if (Coroutine::getContext()[$id] !== null) { + if ($key === null) { + return true; + } + return isset(Coroutine::getContext()[$id][$key]); + } + return false; } diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index d082ae4a..bc09db1f 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -321,8 +321,7 @@ class Request extends Application */ public function getMethod() { - $head = $this->headers->getHeader('request_method'); - return strtolower($head); + return strtolower($this->headers->getHeader('request_method')); } /** diff --git a/HttpServer/Http/Response.php b/HttpServer/Http/Response.php index 2e07f55c..f4b67e2e 100644 --- a/HttpServer/Http/Response.php +++ b/HttpServer/Http/Response.php @@ -189,6 +189,7 @@ class Response extends Application } $this->response->end($sendData); $this->response = null; + unset($this->response); return $sendData; } diff --git a/System/Abstracts/Config.php b/System/Abstracts/Config.php index c979d563..c0cfd145 100644 --- a/System/Abstracts/Config.php +++ b/System/Abstracts/Config.php @@ -54,9 +54,11 @@ class Config extends Component */ public static function get($key, $try = FALSE, $default = null) { - $explode = explode('.', $key); $instance = Snowflake::app()->config->getData(); - foreach ($explode as $index => $value) { + if (strpos($key, '.') === false) { + return isset($instance[$key]) ? $instance[$key] : $default; + } + foreach (explode('.', $key) as $index => $value) { if (empty($value)) { continue; } @@ -66,10 +68,10 @@ class Config extends Component } return $default; } - $instance = $instance[$value]; - if (!is_array($instance) && $index + 1 < count($explode)) { - throw new ConfigException(sprintf(self::ERROR_MESSAGE, $key)); + if (!is_array($instance[$value]) ) { + return $instance[$value]; } + $instance = $instance[$value]; } return empty($instance) ? $default : $instance; } diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 526a4399..5b8c54a7 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -58,7 +58,7 @@ abstract class Pool extends Component */ public function name($cds, $isMaster = false) { - return hash('sha1', $cds . ($isMaster ? 'master' : 'slave')); + return md5($cds . ($isMaster ? 'master' : 'slave')); } diff --git a/System/Application.php b/System/Application.php index 56cd1006..7c7a9256 100644 --- a/System/Application.php +++ b/System/Application.php @@ -38,17 +38,6 @@ class Application extends BaseApplication public $id = 'uniqueId'; - /** - * Application constructor. - * @param array $config - */ - public function __construct(array $config = []) - { -// instance_load(); - parent::__construct($config); - } - - /** * @throws ConfigException * @throws NotFindClassException diff --git a/System/Cache/Redis.php b/System/Cache/Redis.php index 55c851ef..dc13db7a 100644 --- a/System/Cache/Redis.php +++ b/System/Cache/Redis.php @@ -366,24 +366,15 @@ SCRIPT; */ public function get_config(): array { - $config = Config::get('cache.redis', false, [ + return Config::get('cache.redis', false, [ 'host' => '127.0.0.1', 'port' => '6379', 'prefix' => Config::get('id'), 'auth' => '', 'databases' => '0', 'read_timeout' => -1, - 'conn_timeout' => -1, + 'timeout' => -1, ]); - return [ - 'host' => $config['host'], - 'port' => $config['port'], - 'auth' => $config['auth'], - 'timeout' => $config['conn_timeout'], - 'databases' => $config['databases'], - 'read_timeout' => $config['read_timeout'], - 'prefix' => $config['prefix'], - ]; } } diff --git a/System/Di/Container.php b/System/Di/Container.php index 5e72e0a3..f439479f 100644 --- a/System/Di/Container.php +++ b/System/Di/Container.php @@ -131,14 +131,11 @@ class Container extends BaseObject if (empty($config)) { return $reflect->newInstanceArgs($dependencies ?? []); } - if (!empty($dependencies) && $reflect->implementsInterface('Snowflake\Abstracts\Configure')) { $dependencies[count($dependencies) - 1] = $config; return $reflect->newInstanceArgs($dependencies); } - if (!empty($config)) { - $this->_param[$class] = $config; - } + $this->_param[$class] = $config; $object = $reflect->newInstanceArgs($dependencies ?? []); foreach ($config as $key => $val) { $object->{$key} = $val; diff --git a/System/Pool/Redis.php b/System/Pool/Redis.php index 1d66a1fb..eeb66293 100644 --- a/System/Pool/Redis.php +++ b/System/Pool/Redis.php @@ -97,7 +97,7 @@ class Redis extends Pool $config['read_timeout'] = 10; } $redis->select($config['databases']); - $redis->setOption(SRedis::OPT_READ_TIMEOUT, -1); + $redis->setOption(SRedis::OPT_READ_TIMEOUT, $config['read_timeout']); $redis->setOption(SRedis::OPT_PREFIX, $config['prefix']); return $redis; }