This commit is contained in:
2020-09-15 18:48:22 +08:00
parent 01c8bf2ba9
commit e857644012
+18 -12
View File
@@ -67,12 +67,13 @@ class Context extends BaseContext
if (empty($key)) { if (empty($key)) {
return Coroutine::getContext()[$id] = $context; return Coroutine::getContext()[$id] = $context;
} }
if (!is_array(Coroutine::getContext()[$id])) { $oldData = Coroutine::getContext()[$id];
Coroutine::getContext()[$id] = [$key => $context]; if (!is_array($oldData)) {
$oldData = [$key => $context];
} else { } else {
Coroutine::getContext()[$id][$key] = $context; $oldData[$key] = $context;
} }
return $context; return Coroutine::getContext()[$id] = $oldData;
} }
/** /**
@@ -98,10 +99,11 @@ class Context extends BaseContext
*/ */
public static function autoDecr($id, $key = null) public static function autoDecr($id, $key = null)
{ {
if (!static::inCoroutine()) { if (!static::inCoroutine() || !static::hasContext($id)) {
return false; return false;
} }
if (!isset(Coroutine::getContext()[$id][$key])) { $context = Coroutine::getContext()[$id];
if (!isset($context[$key])) {
return false; return false;
} }
return Coroutine::getContext()[$id][$key] -= 1; return Coroutine::getContext()[$id][$key] -= 1;
@@ -114,10 +116,13 @@ class Context extends BaseContext
*/ */
public static function getContext($id, $key = null) public static function getContext($id, $key = null)
{ {
if (!static::hasContext($id)) {
return null;
}
if (static::inCoroutine()) { if (static::inCoroutine()) {
$array = Coroutine::getContext()[$id] ?? null; $array = Coroutine::getContext()[$id];
} else { } else {
$array = static::$_requests[$id] ?? null; $array = static::$_requests[$id];
} }
if (empty($key) || !is_array($array)) { if (empty($key) || !is_array($array)) {
return $array; return $array;
@@ -148,9 +153,9 @@ class Context extends BaseContext
} }
if (static::inCoroutine()) { if (static::inCoroutine()) {
if (!empty($key)) { if (!empty($key)) {
Coroutine::getContext()[$id][$key] = null; unset(Coroutine::getContext()[$id][$key]);
} else { } else {
Coroutine::getContext()[$id] = null; unset(Coroutine::getContext()[$id]);
} }
} else { } else {
unset(static::$_requests[$id]); unset(static::$_requests[$id]);
@@ -165,10 +170,11 @@ class Context extends BaseContext
public static function hasContext($id, $key = null) public static function hasContext($id, $key = null)
{ {
if (static::inCoroutine()) { if (static::inCoroutine()) {
if (!isset(Coroutine::getContext()[$id])) { $context = Coroutine::getContext();
if (!isset($context[$id])) {
return false; return false;
} }
$data = Coroutine::getContext()[$id]; $data = $context[$id];
} else { } else {
if (!isset(static::$_requests[$id])) { if (!isset(static::$_requests[$id])) {
return false; return false;