This commit is contained in:
2021-02-23 16:56:34 +08:00
parent 3cef44886d
commit 7ae8d4ffc2
10 changed files with 500 additions and 492 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ abstract class AbstractCollection extends Component implements \IteratorAggregat
*/ */
public function getModel(): ActiveRecord public function getModel(): ActiveRecord
{ {
return Snowflake::app()->getObject()->getConnection([$this->model], false); return Snowflake::app()->getObject()->get($this->model, false);
} }
+1 -1
View File
@@ -899,7 +899,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess
public static function populate(array $data): static public static function populate(array $data): static
{ {
$object = Snowflake::app()->getObject(); $object = Snowflake::app()->getObject();
$model = $object->getConnection([static::class], false); $model = $object->get(static::class);
$model->setAttributes($data); $model->setAttributes($data);
$model->setIsCreate(false); $model->setIsCreate(false);
$model->refresh(); $model->refresh();
+1 -1
View File
@@ -51,7 +51,7 @@ class CollectionIterator extends \ArrayIterator
protected function newModel($current): ActiveRecord protected function newModel($current): ActiveRecord
{ {
$object = Snowflake::app()->getObject(); $object = Snowflake::app()->getObject();
$model = $object->getConnection([$this->model], false); $model = $object->get($this->model, false);
return $model->setAttributes($current); return $model->setAttributes($current);
+3 -5
View File
@@ -180,10 +180,8 @@ class Connection extends Component
*/ */
public function masterInstance(): PDO public function masterInstance(): PDO
{ {
return $this->connections()->getConnection([ return $this->connections()->get([
'cds' => $this->cds, 'cds' => $this->cds, 'username' => $this->username, 'password' => $this->password
'username' => $this->username,
'password' => $this->password
], true); ], true);
} }
@@ -196,7 +194,7 @@ class Connection extends Component
if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) { if (empty($this->slaveConfig) || $this->slaveConfig['cds'] == $this->cds) {
return $this->masterInstance(); return $this->masterInstance();
} }
return $this->connections()->getConnection($this->slaveConfig, false); return $this->connections()->get($this->slaveConfig, false);
} }
+7 -6
View File
@@ -10,6 +10,7 @@ use HttpServer\Http\Context;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use PDO; use PDO;
use Redis; use Redis;
use Snowflake\Exception\ComponentException;
use Snowflake\Pool\Timeout; use Snowflake\Pool\Timeout;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
@@ -55,7 +56,7 @@ abstract class Pool extends Component
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
protected function get($name, array $callback): mixed protected function getFromChannel($name, array $callback): mixed
{ {
if (!Context::inCoroutine()) { if (!Context::inCoroutine()) {
return $this->createClient($name, $callback); return $this->createClient($name, $callback);
@@ -82,6 +83,7 @@ abstract class Pool extends Component
* @param $cds * @param $cds
* @param $coroutineName * @param $coroutineName
* @param false $isBefore * @param false $isBefore
* @throws ComponentException
*/ */
public function printClients($cds, $coroutineName, $isBefore = false) public function printClients($cds, $coroutineName, $isBefore = false)
{ {
@@ -89,7 +91,7 @@ abstract class Pool extends Component
} }
abstract public function createClient(string $name, array $config): mixed; abstract public function createClient(string $name, mixed $config): mixed;
/** /**
@@ -109,12 +111,10 @@ abstract class Pool extends Component
/** /**
* @param $name * @param string $name
* @param $time
* @param $client * @param $client
* @return mixed * @return mixed
* 检查连接可靠性 * 检查连接可靠性
* @throws Exception
*/ */
public function checkCanUse(string $name, mixed $client): mixed public function checkCanUse(string $name, mixed $client): mixed
{ {
@@ -133,9 +133,10 @@ abstract class Pool extends Component
/** /**
* @param array $config * @param array $config
* @param $isMaster * @param $isMaster
* @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, bool $isMaster) public function get(mixed $config, bool $isMaster): mixed
{ {
throw new Exception('Undefined system processing function.'); throw new Exception('Undefined system processing function.');
} }
+1 -1
View File
@@ -141,7 +141,7 @@ SCRIPT;
{ {
$connections = Snowflake::app()->getPool()->getRedis(); $connections = Snowflake::app()->getPool()->getRedis();
$client = $connections->getConnection($this->get_config(), true); $client = $connections->get($this->get_config(), true);
if (!($client instanceof \Redis)) { if (!($client instanceof \Redis)) {
throw new Exception('Redis connections more.'); throw new Exception('Redis connections more.');
} }
+8 -5
View File
@@ -7,6 +7,7 @@ use HttpServer\Http\Context;
use PDO; use PDO;
use Exception; use Exception;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Snowflake\Abstracts\Pool; use Snowflake\Abstracts\Pool;
use Swoole\Timer; use Swoole\Timer;
@@ -129,12 +130,12 @@ class Connection extends Pool
/** /**
* @param array $config * @param mixed $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, $isMaster = false): mixed public function get(mixed $config, $isMaster = false): mixed
{ {
$coroutineName = $this->name('mysql', $config['cds'], $isMaster); $coroutineName = $this->name('mysql', $config['cds'], $isMaster);
if (!isset($this->hasCreate[$coroutineName])) { if (!isset($this->hasCreate[$coroutineName])) {
@@ -143,7 +144,7 @@ class Connection extends Pool
if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { if (($pdo = Context::getContext($coroutineName)) instanceof PDO) {
return $pdo; return $pdo;
} }
$connections = $this->get($coroutineName, $config); $connections = $this->getFromChannel($coroutineName, $config);
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
$number > 0 && $connections->beginTransaction(); $number > 0 && $connections->beginTransaction();
} }
@@ -153,10 +154,11 @@ class Connection extends Pool
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return PDO * @return PDO
* @throws ComponentException
*/ */
public function createClient(string $name, array $config): PDO public function createClient(string $name, mixed $config): PDO
{ {
$this->printClients($config['cds'], $name, true); $this->printClients($config['cds'], $name, true);
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
@@ -181,6 +183,7 @@ class Connection extends Pool
* @param $cds * @param $cds
* @param $coroutineName * @param $coroutineName
* @param false $isBefore * @param false $isBefore
* @throws ComponentException
*/ */
public function printClients($cds, $coroutineName, $isBefore = false) public function printClients($cds, $coroutineName, $isBefore = false)
{ {
+6 -6
View File
@@ -32,23 +32,23 @@ class ObjectPool extends \Snowflake\Abstracts\Pool
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, bool $isMaster): mixed public function get(mixed $config, bool $isMaster = false): mixed
{ {
if (is_object($config[0])) { if (is_object($config)) {
$config[0] = get_class($config[0]) ; return $config;
} }
return $this->get(md5($config[0]), $config); return $this->getFromChannel(md5($config), $config);
} }
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return mixed * @return mixed
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public function createClient(string $name, array $config): mixed public function createClient(string $name, mixed $config): mixed
{ {
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
return Snowflake::createObject(array_shift($config)); return Snowflake::createObject(array_shift($config));
+6 -1
View File
@@ -37,7 +37,12 @@ class Pool extends \Snowflake\Abstracts\Pool
} }
public function createClient(string $name, array $config): mixed /**
* @param string $name
* @param mixed $config
* @return mixed
*/
public function createClient(string $name, mixed $config): mixed
{ {
// TODO: Implement createClient() method. // TODO: Implement createClient() method.
return null; return null;
+7 -6
View File
@@ -8,6 +8,7 @@ namespace Snowflake\Pool;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use Redis as SRedis; use Redis as SRedis;
use RedisException; use RedisException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\RedisConnectException; use Snowflake\Exception\RedisConnectException;
use Swoole\Coroutine; use Swoole\Coroutine;
use Exception; use Exception;
@@ -33,29 +34,29 @@ class Redis extends Pool
/** /**
* @param array $config * @param mixed $config
* @param bool $isMaster * @param bool $isMaster
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function getConnection(array $config, $isMaster = false): mixed public function get(mixed $config, $isMaster = false): mixed
{ {
$name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases']; $name = $config['host'] . ':' . $config['prefix'] . ':' . $config['databases'];
$coroutineName = $this->name('redis', 'redis:' . $name, $isMaster); $coroutineName = $this->name('redis', 'redis:' . $name, $isMaster);
if (($redis = Context::getContext($coroutineName)) instanceof \Redis) { if (($redis = Context::getContext($coroutineName)) instanceof \Redis) {
return $redis; return $redis;
} }
return Context::setContext($coroutineName, $this->get($coroutineName, $config)); return Context::setContext($coroutineName, $this->getFromChannel($coroutineName, $config));
} }
/** /**
* @param string $name * @param string $name
* @param array $config * @param mixed $config
* @return SRedis * @return SRedis
* @throws RedisConnectException * @throws RedisConnectException|ComponentException
*/ */
public function createClient(string $name, array $config): SRedis public function createClient(string $name, mixed $config): SRedis
{ {
$this->printClients($config['host'], $name, true); $this->printClients($config['host'], $name, true);
$redis = new SRedis(); $redis = new SRedis();