From 801c7dafc0f0e9126c9a8d6300882a8cc0beb87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 31 Aug 2020 13:49:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Database/Base/BaseActiveRecord.php | 2 +- Database/Connection.php | 23 ++++++++++ Database/databases.php | 66 ++++++++++++++++++++++++++++ system/Abstracts/BaseApplication.php | 19 +++++++- system/Cache/Redis.php | 25 ++++++++--- 5 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 Database/databases.php diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index e66602dc..55bf4488 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -766,7 +766,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess */ public static function setDatabaseConnect($bsName) { - return Snowflake::$app->{$bsName}; + return Snowflake::get()->db->instance($bsName); } /** diff --git a/Database/Connection.php b/Database/Connection.php index 80f3a866..dba6a5fe 100644 --- a/Database/Connection.php +++ b/Database/Connection.php @@ -14,8 +14,10 @@ use Database\Mysql\Schema; use Database\Orm\Select; use Exception; use PDO; +use Snowflake\Config; use Snowflake\Event; use Snowflake\Exception\ComponentException; +use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; /** @@ -185,6 +187,27 @@ class Connection extends Component } + /** + * @param $name + * @return Connection + * @throws ConfigException + */ + public function instance($name) + { + $config = Config::get('databases.' . $name, true); + + $this->cds = $config['cds']; + $this->username = $config['username']; + $this->password = $config['password']; + + $this->tablePrefix = $config['tablePrefix']; + + $this->slaveConfig = $config['slaveConfig']; + + return $this; + } + + /** * @return PDO * @throws Exception diff --git a/Database/databases.php b/Database/databases.php new file mode 100644 index 00000000..16b9c541 --- /dev/null +++ b/Database/databases.php @@ -0,0 +1,66 @@ + [ + + 'file' => [ + 'path' => strpos(null, 'data') + ], + + 'redis' => [ + 'host' => '127.0.0.1', + 'port' => '6379', + 'prefix' => 'cache_', + 'auth' => '', + 'databases' => '0', + 'timeout' => -1, + 'read_timeout' => -1, + ], + ], + 'databases' => [ + 'db' => [ + 'id' => 'db', + 'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS, + 'tablePrefix' => 'aircraftwar_', + 'maxNumber' => 100, + 'slaveConfig' => [ + 'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS + ], + ], + 'server' => [ + 'id' => 'server', + 'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS, + 'tablePrefix' => 'master_', + 'maxNumber' => 100, + 'slaveConfig' => [ + 'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS + ], + ], + 'game' => [ + 'id' => 'game', + 'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS, + 'maxNumber' => 100, + 'tablePrefix' => 'game_', + 'slaveConfig' => [ + 'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST, + 'username' => CONNECT_USER, + 'password' => CONNECT_PASS + ], + ], + ] +]; diff --git a/system/Abstracts/BaseApplication.php b/system/Abstracts/BaseApplication.php index 0c8181fc..844a73ec 100644 --- a/system/Abstracts/BaseApplication.php +++ b/system/Abstracts/BaseApplication.php @@ -15,6 +15,7 @@ use HttpServer\Http\Response; use HttpServer\Route\Router; use HttpServer\Server; use Snowflake\Annotation\Annotation; +use Snowflake\Cache\Redis; use Snowflake\Config; use Snowflake\Di\Service; use Snowflake\Error\ErrorHandler; @@ -26,6 +27,8 @@ use Snowflake\Pool\RedisClient; use Snowflake\Processes; use Snowflake\Snowflake; use Snowflake\Event; +use Database\Connection as DbConnection; +use Snowflake\Pool\Pool as SPool; /** * Class BaseApplication @@ -35,8 +38,9 @@ use Snowflake\Event; * @property Event $event * @property Router $router * @property Processes $processes - * @property \Snowflake\Pool\Pool $pool + * @property SPool $pool * @property Server $servers + * @property DbConnection $db * @property Connection $connections * @property Logger $logger */ @@ -204,6 +208,15 @@ abstract class BaseApplication extends Service } + /** + * @return mixed + * @throws ComponentException + */ + public function getRedis() + { + return $this->get('redis'); + } + /** * @param $ip * @return bool @@ -223,14 +236,16 @@ abstract class BaseApplication extends Service 'event' => ['class' => Event::class], 'annotation' => ['class' => Annotation::class], 'processes' => ['class' => Processes::class], + 'db' => ['class' => DbConnection::class], 'connections' => ['class' => Connection::class], 'redis_connections' => ['class' => RedisClient::class], - 'pool' => ['class' => \Snowflake\Pool\Pool::class], + 'pool' => ['class' => SPool::class], 'response' => ['class' => Response::class], 'request' => ['class' => Request::class], 'config' => ['class' => Config::class], 'logger' => ['class' => Logger::class], 'router' => ['class' => Router::class], + 'redis' => ['class' => Redis::class], ]); } } diff --git a/system/Cache/Redis.php b/system/Cache/Redis.php index 4c70f5d2..250b048c 100644 --- a/system/Cache/Redis.php +++ b/system/Cache/Redis.php @@ -12,6 +12,7 @@ use Exception; use Snowflake\Abstracts\Component; use Snowflake\Config; use Snowflake\Event; +use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; use Swoole\Coroutine; @@ -331,17 +332,27 @@ class Redis extends Component /** * @return array + * @throws ConfigException */ public function get_config(): array { + $config = Config::get('cache.redis', false, [ + 'host' => '127.0.0.1', + 'port' => '6379', + 'prefix' => Config::get('id'), + 'auth' => '', + 'databases' => '0', + 'timeout' => -1, + 'read_timeout' => -1, + ]); return [ - 'host' => env('REDIS.HOST', $this->host), - 'port' => env('REDIS.PORT', $this->port), - 'auth' => env('REDIS.PASSWORD', $this->auth), - 'timeout' => env('REDIS.TIMEOUT', 2), - 'databases' => env('REDIS.DATABASE', $this->databases), - 'read_timeout' => env('REDIS.READ_TIMEOUT', 10), - 'prefix' => env('REDIS.PREFIX', 'system:'), + 'host' => $config['host'], + 'port' => $config['port'], + 'auth' => $config['auth'], + 'timeout' => $config['conn_timeout'], + 'databases' => $config['databases'], + 'read_timeout' => $config['read_timeout'], + 'prefix' => $config['prefix'], ]; }