This commit is contained in:
2020-08-31 13:49:40 +08:00
parent 50cc15ea4b
commit 801c7dafc0
5 changed files with 125 additions and 10 deletions
+1 -1
View File
@@ -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);
}
/**
+23
View File
@@ -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
+66
View File
@@ -0,0 +1,66 @@
<?php
defined('CONNECT_HOST') or define('CONNECT_HOST', '');
defined('CONNECT_USER') or define('CONNECT_USER', '');
defined('CONNECT_PASS') or define('CONNECT_PASS', '');
return [
'cache' => [
'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
],
],
]
];
+17 -2
View File
@@ -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],
]);
}
}
+18 -7
View File
@@ -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'],
];
}