From 00c089363f74c67005ae5b8604c2e5b0e70e0c78 Mon Sep 17 00:00:00 2001 From: whwyy Date: Tue, 12 Dec 2023 18:03:08 +0800 Subject: [PATCH] eee --- Context/AsyncContext.php | 155 ++++++++++++++-------------- Context/ContextInterface.php | 90 +++++++++-------- Context/CoroutineContext.php | 189 ++++++++++++++++++----------------- 3 files changed, 229 insertions(+), 205 deletions(-) diff --git a/Context/AsyncContext.php b/Context/AsyncContext.php index f0b87a9..e4517a5 100644 --- a/Context/AsyncContext.php +++ b/Context/AsyncContext.php @@ -9,83 +9,92 @@ class AsyncContext implements ContextInterface { - /** - * @var array - */ - private static array $context = []; + /** + * @var array + */ + private static array $context = []; - /** - * @param string $key - * @param mixed $value - * @param int|null $coroutineId - * @return mixed - */ - public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed - { - // TODO: Implement set() method. - return static::$context[$key] = $value; - } - /** - * @param string $key - * @param mixed|null $defaultValue - * @param int|null $coroutineId - * @return mixed - */ - public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed - { - // TODO: Implement get() method. - return static::$context[$key] ?? $defaultValue; - } + /** + * @return bool + */ + public static function inCoroutine(): bool + { + return false; + } - /** - * @param string $key - * @param int|null $coroutineId - * @return mixed - */ - public static function exists(string $key, ?int $coroutineId = null): bool - { - // TODO: Implement exists() method. - return isset(static::$context[$key]); - } + /** + * @param string $key + * @param mixed $value + * @param int|null $coroutineId + * @return mixed + */ + public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed + { + // TODO: Implement set() method. + return static::$context[$key] = $value; + } - /** - * @param string $key - * @param int|null $coroutineId - * @return void - */ - public static function remove(string $key, ?int $coroutineId = null): void - { - // TODO: Implement remove() method. - static::$context[$key] = null; - unset(static::$context[$key]); - } + /** + * @param string $key + * @param mixed|null $defaultValue + * @param int|null $coroutineId + * @return mixed + */ + public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed + { + // TODO: Implement get() method. + return static::$context[$key] ?? $defaultValue; + } - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int - { - if (!isset(static::$context[$id])) { - static::$context[$id] = 0; - } - return static::$context[$id] += $value; - } + /** + * @param string $key + * @param int|null $coroutineId + * @return mixed + */ + public static function exists(string $key, ?int $coroutineId = null): bool + { + // TODO: Implement exists() method. + return isset(static::$context[$key]); + } - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int - { - if (!isset(static::$context[$id])) { - static::$context[$id] = 0; - } - return static::$context[$id] -= $value; - } + /** + * @param string $key + * @param int|null $coroutineId + * @return void + */ + public static function remove(string $key, ?int $coroutineId = null): void + { + // TODO: Implement remove() method. + static::$context[$key] = null; + unset(static::$context[$key]); + } + + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int + { + if (!isset(static::$context[$id])) { + static::$context[$id] = 0; + } + return static::$context[$id] += $value; + } + + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int + { + if (!isset(static::$context[$id])) { + static::$context[$id] = 0; + } + return static::$context[$id] -= $value; + } } diff --git a/Context/ContextInterface.php b/Context/ContextInterface.php index b3735cd..0807aba 100644 --- a/Context/ContextInterface.php +++ b/Context/ContextInterface.php @@ -7,54 +7,60 @@ interface ContextInterface { - /** - * @param string $key - * @param mixed $value - * @param int|null $coroutineId - * @return mixed - */ - public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed; - - /** - * @param string $key - * @param mixed|null $defaultValue - * @param int|null $coroutineId - * @return mixed - */ - public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed; - - /** - * @param string $key - * @param int|null $coroutineId - * @return mixed - */ - public static function exists(string $key, ?int $coroutineId = null): bool; + /** + * @return bool + */ + public static function inCoroutine(): bool; - /** - * @param string $key - * @param int|null $coroutineId - * @return void - */ - public static function remove(string $key, ?int $coroutineId = null): void; + /** + * @param string $key + * @param mixed $value + * @param int|null $coroutineId + * @return mixed + */ + public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed; + + /** + * @param string $key + * @param mixed|null $defaultValue + * @param int|null $coroutineId + * @return mixed + */ + public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed; + + /** + * @param string $key + * @param int|null $coroutineId + * @return mixed + */ + public static function exists(string $key, ?int $coroutineId = null): bool; - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int; + /** + * @param string $key + * @param int|null $coroutineId + * @return void + */ + public static function remove(string $key, ?int $coroutineId = null): void; - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int; + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int; + + + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int; } diff --git a/Context/CoroutineContext.php b/Context/CoroutineContext.php index 3d2b1b1..4a00913 100644 --- a/Context/CoroutineContext.php +++ b/Context/CoroutineContext.php @@ -9,97 +9,106 @@ class CoroutineContext implements ContextInterface { - /** - * @param string $key - * @param mixed $value - * @param int|null $coroutineId - * @return mixed - */ - public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed - { - // TODO: Implement set() method. - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - return Coroutine::getContext($coroutineId)[$key] = $value; - } - - /** - * @param string $key - * @param mixed|null $defaultValue - * @param int|null $coroutineId - * @return mixed - */ - public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed - { - // TODO: Implement get() method. - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - return Coroutine::getContext($coroutineId)[$key] ?? $defaultValue; - } - - /** - * @param string $key - * @param int|null $coroutineId - * @return mixed - */ - public static function exists(string $key, ?int $coroutineId = null): bool - { - // TODO: Implement exists() method. - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - return isset(Coroutine::getContext($coroutineId)[$key]); - } - - /** - * @param string $key - * @param int|null $coroutineId - * @return void - */ - public static function remove(string $key, ?int $coroutineId = null): void - { - // TODO: Implement remove() method. - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - Coroutine::getContext($coroutineId)[$key] = null; - unset(Coroutine::getContext($coroutineId)[$key]); - } + /** + * @return bool + */ + public static function inCoroutine(): bool + { + return true; + } - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int - { - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - if (!isset(Coroutine::getContext($coroutineId)[$id])) { - Coroutine::getContext($coroutineId)[$id] = 0; - } - return Coroutine::getContext($coroutineId)[$id] += $value; - } + /** + * @param string $key + * @param mixed $value + * @param int|null $coroutineId + * @return mixed + */ + public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed + { + // TODO: Implement set() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return Coroutine::getContext($coroutineId)[$key] = $value; + } - /** - * @param string $id - * @param int $value - * @param int|null $coroutineId - * @return int - */ - public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int - { - if (is_null($coroutineId)) { - $coroutineId = Coroutine::getCid(); - } - if (!isset(Coroutine::getContext($coroutineId)[$id])) { - Coroutine::getContext($coroutineId)[$id] = 0; - } - return Coroutine::getContext($coroutineId)[$id] -= $value; - } + /** + * @param string $key + * @param mixed|null $defaultValue + * @param int|null $coroutineId + * @return mixed + */ + public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed + { + // TODO: Implement get() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return Coroutine::getContext($coroutineId)[$key] ?? $defaultValue; + } + + /** + * @param string $key + * @param int|null $coroutineId + * @return mixed + */ + public static function exists(string $key, ?int $coroutineId = null): bool + { + // TODO: Implement exists() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + return isset(Coroutine::getContext($coroutineId)[$key]); + } + + /** + * @param string $key + * @param int|null $coroutineId + * @return void + */ + public static function remove(string $key, ?int $coroutineId = null): void + { + // TODO: Implement remove() method. + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + Coroutine::getContext($coroutineId)[$key] = null; + unset(Coroutine::getContext($coroutineId)[$key]); + } + + + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int + { + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + if (!isset(Coroutine::getContext($coroutineId)[$id])) { + Coroutine::getContext($coroutineId)[$id] = 0; + } + return Coroutine::getContext($coroutineId)[$id] += $value; + } + + /** + * @param string $id + * @param int $value + * @param int|null $coroutineId + * @return int + */ + public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int + { + if (is_null($coroutineId)) { + $coroutineId = Coroutine::getCid(); + } + if (!isset(Coroutine::getContext($coroutineId)[$id])) { + Coroutine::getContext($coroutineId)[$id] = 0; + } + return Coroutine::getContext($coroutineId)[$id] -= $value; + } }