改名
This commit is contained in:
+8
-4
@@ -22,12 +22,15 @@ class Db
|
|||||||
{
|
{
|
||||||
use QueryTrait;
|
use QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
|
private static bool $_inTransaction = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function transactionsActive(): bool
|
public static function transactionsActive(): bool
|
||||||
{
|
{
|
||||||
return Context::getContext('begin:transaction') === true;
|
return static::$_inTransaction === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,7 +38,8 @@ class Db
|
|||||||
*/
|
*/
|
||||||
public static function beginTransaction()
|
public static function beginTransaction()
|
||||||
{
|
{
|
||||||
Context::setContext('begin:transaction', true);
|
static::$_inTransaction = true;
|
||||||
|
// Context::setContext('begin:transaction', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +53,7 @@ class Db
|
|||||||
$event = Snowflake::app()->getEvent();
|
$event = Snowflake::app()->getEvent();
|
||||||
$event->trigger(Connection::TRANSACTION_COMMIT);
|
$event->trigger(Connection::TRANSACTION_COMMIT);
|
||||||
$event->offName(Connection::TRANSACTION_COMMIT);
|
$event->offName(Connection::TRANSACTION_COMMIT);
|
||||||
Context::deleteContext('begin:transaction');
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +67,7 @@ class Db
|
|||||||
$event = Snowflake::app()->getEvent();
|
$event = Snowflake::app()->getEvent();
|
||||||
$event->trigger(Connection::TRANSACTION_ROLLBACK);
|
$event->trigger(Connection::TRANSACTION_ROLLBACK);
|
||||||
$event->offName(Connection::TRANSACTION_ROLLBACK);
|
$event->offName(Connection::TRANSACTION_ROLLBACK);
|
||||||
Context::deleteContext('begin:transaction');
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+223
-178
@@ -13,194 +13,239 @@ use Swoole\Coroutine;
|
|||||||
class Context extends BaseContext
|
class Context extends BaseContext
|
||||||
{
|
{
|
||||||
|
|
||||||
protected static array $_requests = [];
|
protected static array $_requests = [];
|
||||||
|
|
||||||
protected static array $_response = [];
|
protected static array $_response = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function setContext($id, $context, $key = null): mixed
|
public static function setContext($id, $context, $key = null): mixed
|
||||||
{
|
{
|
||||||
if (static::inCoroutine()) {
|
if (static::inCoroutine()) {
|
||||||
return self::setCoroutine($id, $context, $key);
|
return self::setCoroutine($id, $context, $key);
|
||||||
} else {
|
} else {
|
||||||
return self::setStatic($id, $context, $key);
|
return self::setStatic($id, $context, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private static function setStatic($id, $context, $key = null): mixed
|
private static function setStatic($id, $context, $key = null): mixed
|
||||||
{
|
{
|
||||||
if (!empty($key)) {
|
if (empty($key)) {
|
||||||
if (!is_array(static::$_requests[$id])) {
|
return static::$_requests[$id] = $context;
|
||||||
static::$_requests[$id] = [$key => $context];
|
}
|
||||||
} else {
|
if (!is_array(static::$_requests[$id])) {
|
||||||
static::$_requests[$id][$key] = $context;
|
return static::$_requests[$id] = [$key => $context];
|
||||||
}
|
} else {
|
||||||
} else {
|
return static::$_requests[$id][$key] = $context;
|
||||||
static::$_requests[$id] = $context;
|
}
|
||||||
}
|
}
|
||||||
return $context;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private static function setCoroutine($id, $context, $key = null): mixed
|
private static function setCoroutine($id, $context, $key = null): mixed
|
||||||
{
|
{
|
||||||
if (!static::hasContext($id)) {
|
if (empty($key)) {
|
||||||
Coroutine::getContext()[$id] = [];
|
return Coroutine::getContext()[$id] = $context;
|
||||||
}
|
}
|
||||||
if (!empty($key)) {
|
if (!is_array(Coroutine::getContext()[$id])) {
|
||||||
if (!is_array(Coroutine::getContext()[$id])) {
|
Coroutine::getContext()[$id] = [$key => $context];
|
||||||
Coroutine::getContext()[$id] = [$key => $context];
|
} else {
|
||||||
} else {
|
Coroutine::getContext()[$id][$key] = $context;
|
||||||
Coroutine::getContext()[$id][$key] = $context;
|
}
|
||||||
}
|
return $context;
|
||||||
} else {
|
}
|
||||||
Coroutine::getContext()[$id] = $context;
|
|
||||||
}
|
|
||||||
return $context;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @param int $value
|
||||||
*/
|
* @return bool|int
|
||||||
public static function autoIncr($id, $key = null): mixed
|
*/
|
||||||
{
|
public static function increment($id, $key = null, $value = 1): bool|int
|
||||||
if (!static::inCoroutine()) {
|
{
|
||||||
return false;
|
if (!static::inCoroutine()) {
|
||||||
}
|
return false;
|
||||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
}
|
||||||
return false;
|
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||||
}
|
return false;
|
||||||
return Coroutine::getContext()[$id][$key] += 1;
|
}
|
||||||
}
|
return Coroutine::getContext()[$id][$key] += $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @param int $value
|
||||||
*/
|
* @return bool|int
|
||||||
public static function autoDecr($id, $key = null): mixed
|
*/
|
||||||
{
|
public static function decrement($id, $key = null, $value = 1): bool|int
|
||||||
if (!static::inCoroutine() || !static::hasContext($id)) {
|
{
|
||||||
return false;
|
if (!static::inCoroutine() || !static::hasContext($id)) {
|
||||||
}
|
return false;
|
||||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
}
|
||||||
return false;
|
if (!isset(Coroutine::getContext()[$id][$key])) {
|
||||||
}
|
return false;
|
||||||
return Coroutine::getContext()[$id][$key] -= 1;
|
}
|
||||||
}
|
return Coroutine::getContext()[$id][$key] -= $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function getContext($id, $key = null): mixed
|
public static function getContext($id, $key = null): mixed
|
||||||
{
|
{
|
||||||
if (!static::hasContext($id)) {
|
if (!static::hasContext($id)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (static::inCoroutine()) {
|
if (static::inCoroutine()) {
|
||||||
if ($key === null) {
|
return static::loadByContext($id, $key);
|
||||||
return Coroutine::getContext()[$id];
|
} else {
|
||||||
} else {
|
return static::loadByStatic($id, $key);
|
||||||
return Coroutine::getContext()[$id][$key] ?? null;
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
if ($key === null) {
|
|
||||||
return static::$_requests[$id];
|
|
||||||
} else {
|
|
||||||
return static::$_requests[$id][$key] ?? null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public static function getAllContext(): mixed
|
|
||||||
{
|
|
||||||
if (static::inCoroutine()) {
|
|
||||||
return Coroutine::getContext() ?? [];
|
|
||||||
} else {
|
|
||||||
return static::$_requests ?? [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $id
|
|
||||||
* @param string|null $key
|
|
||||||
*/
|
|
||||||
public static function deleteContext(string $id, string $key = null)
|
|
||||||
{
|
|
||||||
if (!static::hasContext($id, $key)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (static::inCoroutine()) {
|
|
||||||
if (!empty($key)) {
|
|
||||||
unset(Coroutine::getContext()[$id][$key]);
|
|
||||||
} else {
|
|
||||||
unset(Coroutine::getContext()[$id]);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
unset(static::$_requests[$id]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $id
|
|
||||||
* @param null $key
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public static function hasContext($id, $key = null): bool
|
|
||||||
{
|
|
||||||
if (!static::inCoroutine()) {
|
|
||||||
if (!isset(static::$_requests[$id]) || empty(static::$_requests[$id])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!empty($key) && !isset(static::$_requests[$id][$key])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (!isset(Coroutine::getContext()[$id])) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (Coroutine::getContext()[$id] !== null) {
|
|
||||||
if ($key === null) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return isset(Coroutine::getContext()[$id][$key]);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @param $id
|
||||||
*/
|
* @param null $key
|
||||||
public static function inCoroutine(): bool
|
* @return mixed
|
||||||
{
|
*/
|
||||||
return Coroutine::getCid() > 0;
|
private static function loadByContext($id, $key = null): mixed
|
||||||
}
|
{
|
||||||
|
$data = Coroutine::getContext()[$id] ?? null;
|
||||||
|
if ($data === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ($key !== null) {
|
||||||
|
return $data[$key] ?? null;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param null $key
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
private static function loadByStatic($id, $key = null): mixed
|
||||||
|
{
|
||||||
|
$data = static::$_requests[$id] ?? null;
|
||||||
|
if ($data === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if ($key !== null) {
|
||||||
|
return $data[$key] ?? null;
|
||||||
|
}
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public static function getAllContext(): mixed
|
||||||
|
{
|
||||||
|
if (static::inCoroutine()) {
|
||||||
|
return Coroutine::getContext() ?? [];
|
||||||
|
} else {
|
||||||
|
return static::$_requests ?? [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $id
|
||||||
|
* @param string|null $key
|
||||||
|
*/
|
||||||
|
public static function remove(string $id, string $key = null)
|
||||||
|
{
|
||||||
|
if (!static::hasContext($id, $key)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (static::inCoroutine()) {
|
||||||
|
unset(static::$_requests[$id]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!empty($key)) {
|
||||||
|
unset(Coroutine::getContext()[$id][$key]);
|
||||||
|
} else {
|
||||||
|
unset(Coroutine::getContext()[$id]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param null $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function hasContext($id, $key = null): bool
|
||||||
|
{
|
||||||
|
if (!static::inCoroutine()) {
|
||||||
|
return static::searchByStatic($id, $key);
|
||||||
|
} else {
|
||||||
|
return static::searchByCoroutine($id, $key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param null $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private static function searchByStatic($id, $key = null): bool
|
||||||
|
{
|
||||||
|
if (!isset(static::$_requests[$id])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!empty($key) && !isset(static::$_requests[$id][$key])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $id
|
||||||
|
* @param null $key
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private static function searchByCoroutine($id, $key = null): bool
|
||||||
|
{
|
||||||
|
if (!isset(Coroutine::getContext()[$id])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if ($key !== null) {
|
||||||
|
return isset((Coroutine::getContext()[$id] ?? [])[$key]);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public static function inCoroutine(): bool
|
||||||
|
{
|
||||||
|
return Coroutine::getCid() > 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -154,7 +154,7 @@ abstract class Pool extends Component
|
|||||||
}
|
}
|
||||||
if (!Context::hasContext('create::client::ing::' . $name)) {
|
if (!Context::hasContext('create::client::ing::' . $name)) {
|
||||||
$this->push($name, $this->createClient($name, $callback));
|
$this->push($name, $this->createClient($name, $callback));
|
||||||
Context::deleteContext('create::client::ing::' . $name);
|
Context::remove('create::client::ing::' . $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ class Connection extends Pool
|
|||||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||||
Context::setContext('begin_' . $coroutineName, 0);
|
Context::setContext('begin_' . $coroutineName, 0);
|
||||||
}
|
}
|
||||||
Context::autoIncr('begin_' . $coroutineName);
|
Context::increment('begin_' . $coroutineName);
|
||||||
if (!Context::getContext('begin_' . $coroutineName) !== 0) {
|
if (!Context::getContext('begin_' . $coroutineName) !== 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -84,7 +84,7 @@ class Connection extends Pool
|
|||||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Context::autoDecr('begin_' . $coroutineName) > 0) {
|
if (Context::decrement('begin_' . $coroutineName) > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$connection = Context::getContext($coroutineName);
|
$connection = Context::getContext($coroutineName);
|
||||||
@@ -117,7 +117,7 @@ class Connection extends Pool
|
|||||||
if (!Context::hasContext('begin_' . $coroutineName)) {
|
if (!Context::hasContext('begin_' . $coroutineName)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (Context::autoDecr('begin_' . $coroutineName) > 0) {
|
if (Context::decrement('begin_' . $coroutineName) > 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
|
if (($connection = Context::getContext($coroutineName)) instanceof PDO) {
|
||||||
@@ -250,7 +250,7 @@ class Connection extends Pool
|
|||||||
*/
|
*/
|
||||||
public function remove($coroutineName)
|
public function remove($coroutineName)
|
||||||
{
|
{
|
||||||
Context::deleteContext($coroutineName);
|
Context::remove($coroutineName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ class Redis extends Pool
|
|||||||
*/
|
*/
|
||||||
public function remove(string $coroutineName)
|
public function remove(string $coroutineName)
|
||||||
{
|
{
|
||||||
Context::deleteContext($coroutineName);
|
Context::remove($coroutineName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user