From 9775d16db51391d3915dacad5d177f459601c785 Mon Sep 17 00:00:00 2001 From: xl Date: Wed, 6 Nov 2024 21:47:21 +0800 Subject: [PATCH] eee --- Context/CoroutineContext.php | 44 +++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/Context/CoroutineContext.php b/Context/CoroutineContext.php index 625de54..5ba8394 100644 --- a/Context/CoroutineContext.php +++ b/Context/CoroutineContext.php @@ -26,7 +26,11 @@ class CoroutineContext implements ContextInterface */ public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed { - return Coroutine::getContext()[$key] = $value; + // TODO: Implement set() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return Coroutine::getContext($coroutineId)[$key] = $value; } /** @@ -37,7 +41,11 @@ class CoroutineContext implements ContextInterface */ public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed { - return Coroutine::getContext()[$key] ?? $defaultValue; + // TODO: Implement get() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return Coroutine::getContext($coroutineId)[$key] ?? $defaultValue; } /** @@ -47,7 +55,11 @@ class CoroutineContext implements ContextInterface */ public static function exists(string $key, ?int $coroutineId = null): bool { - return isset(Coroutine::getContext()[$key]); + // TODO: Implement exists() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return isset(Coroutine::getContext($coroutineId)[$key]); } /** @@ -57,8 +69,12 @@ class CoroutineContext implements ContextInterface */ public static function remove(string $key, ?int $coroutineId = null): void { - Coroutine::getContext()[$key] = null; - unset(Coroutine::getContext()[$key]); + // TODO: Implement remove() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + Coroutine::getContext($coroutineId)[$key] = null; + unset(Coroutine::getContext($coroutineId)[$key]); } @@ -70,10 +86,13 @@ class CoroutineContext implements ContextInterface */ public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int { - if (!isset(Coroutine::getContext()[$id])) { - Coroutine::getContext()[$id] = 0; + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); } - return Coroutine::getContext()[$id] += $value; + if (!isset(Coroutine::getContext($coroutineId)[$id])) { + Coroutine::getContext($coroutineId)[$id] = 0; + } + return Coroutine::getContext($coroutineId)[$id] += $value; } /** @@ -84,9 +103,12 @@ class CoroutineContext implements ContextInterface */ public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int { - if (!isset(Coroutine::getContext()[$id])) { - Coroutine::getContext()[$id] = 0; + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); } - return Coroutine::getContext()[$id] -= $value; + if (!isset(Coroutine::getContext($coroutineId)[$id])) { + Coroutine::getContext($coroutineId)[$id] = 0; + } + return Coroutine::getContext($coroutineId)[$id] -= $value; } }