This commit is contained in:
as2252258@163.com
2021-05-02 05:00:06 +08:00
parent 7473193da6
commit ca6ff010ed
+189 -212
View File
@@ -13,239 +13,216 @@ use Swoole\Coroutine;
class Context extends BaseContext class Context extends BaseContext
{ {
protected static array $_contents = []; protected static array $_contents = [];
/**
* @param $id
* @param $context
* @param null $key
* @return mixed
*/
public static function setContext($id, $context, $key = null): mixed
{
return self::setCoroutine($id, $context, $key);
}
/** /**
* @param $id * @param $id
* @param $context * @param $context
* @param null $key * @param null $key
* @return mixed * @return array
*/ */
public static function setContext($id, $context, $key = null): mixed private static function setStatic($id, $context, $key = null): mixed
{ {
if (static::inCoroutine()) { if (empty($key)) {
return self::setCoroutine($id, $context, $key); return static::$_contents[$id] = $context;
} else { }
return self::setStatic($id, $context, $key); if (!is_array(static::$_contents[$id])) {
} static::$_contents[$id] = [$key => $context];
} } else {
static::$_contents[$id][$key] = $context;
}
return $context;
}
/** /**
* @param $id * @param $id
* @param $context * @param $context
* @param null $key * @param null $key
* @return array * @return mixed
*/ */
private static function setStatic($id, $context, $key = null): mixed private static function setCoroutine($id, $context, $key = null): mixed
{ {
if (empty($key)) { if (empty($key)) {
return static::$_contents[$id] = $context; return Coroutine::getContext()[$id] = $context;
} }
if (!is_array(static::$_contents[$id])) { if (!is_array(Coroutine::getContext()[$id])) {
static::$_contents[$id] = [$key => $context]; Coroutine::getContext()[$id] = [$key => $context];
} else { } else {
static::$_contents[$id][$key] = $context; Coroutine::getContext()[$id][$key] = $context;
} }
return $context; return $context;
} }
/** /**
* @param $id * @param $id
* @param $context * @param null $key
* @param null $key * @param int $value
* @return mixed * @return bool|int
*/ */
private static function setCoroutine($id, $context, $key = null): mixed public static function increment($id, $key = null, $value = 1): bool|int
{ {
if (empty($key)) { if (!isset(Coroutine::getContext()[$id][$key])) {
return Coroutine::getContext()[$id] = $context; return false;
} }
if (!is_array(Coroutine::getContext()[$id])) { return Coroutine::getContext()[$id][$key] += $value;
Coroutine::getContext()[$id] = [$key => $context]; }
} else {
Coroutine::getContext()[$id][$key] = $context;
}
return $context;
}
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @param int $value * @param int $value
* @return bool|int * @return bool|int
*/ */
public static function increment($id, $key = null, $value = 1): bool|int public static function decrement($id, $key = null, $value = 1): bool|int
{ {
if (!static::inCoroutine()) { if (!static::hasContext($id)) {
return false; return false;
} }
if (!isset(Coroutine::getContext()[$id][$key])) { if (!isset(Coroutine::getContext()[$id][$key])) {
return false; return false;
} }
return Coroutine::getContext()[$id][$key] += $value; return Coroutine::getContext()[$id][$key] -= $value;
} }
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @param int $value * @return mixed
* @return bool|int */
*/ public static function getContext($id, $key = null): mixed
public static function decrement($id, $key = null, $value = 1): bool|int {
{ return static::loadByContext($id, $key);
if (!static::inCoroutine() || !static::hasContext($id)) { }
return false;
}
if (!isset(Coroutine::getContext()[$id][$key])) {
return false;
}
return Coroutine::getContext()[$id][$key] -= $value;
}
/**
* @param $id
* @param null $key
* @return mixed
*/
public static function getContext($id, $key = null): mixed
{
if (!static::hasContext($id)) {
return null;
}
if (static::inCoroutine()) {
return static::loadByContext($id, $key);
} else {
return static::loadByStatic($id, $key);
}
}
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return mixed * @return mixed
*/ */
private static function loadByContext($id, $key = null): mixed private static function loadByContext($id, $key = null): mixed
{ {
$data = Coroutine::getContext()[$id] ?? null; $data = Coroutine::getContext()[$id] ?? null;
if ($data === null) { if ($data === null) {
return null; return null;
} }
if ($key !== null) { if ($key !== null) {
return $data[$key] ?? null; return $data[$key] ?? null;
} }
return $data; return $data;
} }
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return mixed * @return mixed
*/ */
private static function loadByStatic($id, $key = null): mixed private static function loadByStatic($id, $key = null): mixed
{ {
$data = static::$_contents[$id] ?? null; $data = static::$_contents[$id] ?? null;
if ($data === null) { if ($data === null) {
return null; return null;
} }
if ($key !== null) { if ($key !== null) {
return $data[$key] ?? null; return $data[$key] ?? null;
} }
return $data; return $data;
} }
/** /**
* @return mixed * @return mixed
*/ */
public static function getAllContext(): mixed public static function getAllContext(): mixed
{ {
if (static::inCoroutine()) { if (static::inCoroutine()) {
return Coroutine::getContext() ?? []; return Coroutine::getContext() ?? [];
} else { } else {
return static::$_contents ?? []; return static::$_contents ?? [];
} }
} }
/** /**
* @param string $id * @param string $id
* @param string|null $key * @param string|null $key
*/ */
public static function remove(string $id, string $key = null) public static function remove(string $id, string $key = null)
{ {
if (!static::hasContext($id, $key)) { if (!static::hasContext($id, $key)) {
return; return;
} }
if (static::inCoroutine()) { if (!empty($key)) {
unset(static::$_contents[$id]); unset(Coroutine::getContext()[$id][$key]);
return; } else {
} unset(Coroutine::getContext()[$id]);
if (!empty($key)) { }
unset(Coroutine::getContext()[$id][$key]); }
} else {
unset(Coroutine::getContext()[$id]);
}
}
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return bool * @return bool
*/ */
public static function hasContext($id, $key = null): bool public static function hasContext($id, $key = null): bool
{ {
if (!static::inCoroutine()) { return static::searchByCoroutine($id, $key);
return static::searchByStatic($id, $key); }
} else {
return static::searchByCoroutine($id, $key);
}
}
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return bool * @return bool
*/ */
private static function searchByStatic($id, $key = null): bool private static function searchByStatic($id, $key = null): bool
{ {
if (!isset(static::$_contents[$id])) { if (!isset(static::$_contents[$id])) {
return false; return false;
} }
if (!empty($key) && !isset(static::$_contents[$id][$key])) { if (!empty($key) && !isset(static::$_contents[$id][$key])) {
return false; return false;
} }
return true; return true;
} }
/** /**
* @param $id * @param $id
* @param null $key * @param null $key
* @return bool * @return bool
*/ */
private static function searchByCoroutine($id, $key = null): bool private static function searchByCoroutine($id, $key = null): bool
{ {
if (!isset(Coroutine::getContext()[$id])) { if (!isset(Coroutine::getContext()[$id])) {
return false; return false;
} }
if ($key !== null) { if ($key !== null) {
return isset((Coroutine::getContext()[$id] ?? [])[$key]); return isset((Coroutine::getContext()[$id] ?? [])[$key]);
} }
return true; return true;
} }
/** /**
* @return bool * @return bool
*/ */
public static function inCoroutine(): bool public static function inCoroutine(): bool
{ {
return Coroutine::getCid() > 0; return Coroutine::getCid() > 0;
} }
} }