This commit is contained in:
2023-12-12 18:03:08 +08:00
parent 1d3c255be8
commit 00c089363f
3 changed files with 229 additions and 205 deletions
+82 -73
View File
@@ -9,83 +9,92 @@ class AsyncContext implements ContextInterface
{ {
/** /**
* @var array * @var array
*/ */
private static array $context = []; 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 * @return bool
* @param mixed|null $defaultValue */
* @param int|null $coroutineId public static function inCoroutine(): bool
* @return mixed {
*/ return false;
public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed }
{
// TODO: Implement get() method.
return static::$context[$key] ?? $defaultValue;
}
/** /**
* @param string $key * @param string $key
* @param int|null $coroutineId * @param mixed $value
* @return mixed * @param int|null $coroutineId
*/ * @return mixed
public static function exists(string $key, ?int $coroutineId = null): bool */
{ public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed
// TODO: Implement exists() method. {
return isset(static::$context[$key]); // TODO: Implement set() method.
} return static::$context[$key] = $value;
}
/** /**
* @param string $key * @param string $key
* @param int|null $coroutineId * @param mixed|null $defaultValue
* @return void * @param int|null $coroutineId
*/ * @return mixed
public static function remove(string $key, ?int $coroutineId = null): void */
{ public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed
// TODO: Implement remove() method. {
static::$context[$key] = null; // TODO: Implement get() method.
unset(static::$context[$key]); return static::$context[$key] ?? $defaultValue;
} }
/** /**
* @param string $id * @param string $key
* @param int $value * @param int|null $coroutineId
* @param int|null $coroutineId * @return mixed
* @return int */
*/ public static function exists(string $key, ?int $coroutineId = null): bool
public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int {
{ // TODO: Implement exists() method.
if (!isset(static::$context[$id])) { return isset(static::$context[$key]);
static::$context[$id] = 0; }
}
return static::$context[$id] += $value;
}
/** /**
* @param string $id * @param string $key
* @param int $value * @param int|null $coroutineId
* @param int|null $coroutineId * @return void
* @return int */
*/ public static function remove(string $key, ?int $coroutineId = null): void
public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int {
{ // TODO: Implement remove() method.
if (!isset(static::$context[$id])) { static::$context[$key] = null;
static::$context[$id] = 0; unset(static::$context[$key]);
} }
return static::$context[$id] -= $value;
} /**
* @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;
}
} }
+48 -42
View File
@@ -7,54 +7,60 @@ interface ContextInterface
{ {
/** /**
* @param string $key * @return bool
* @param mixed $value */
* @param int|null $coroutineId public static function inCoroutine(): bool;
* @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 $key * @param string $key
* @param int|null $coroutineId * @param mixed $value
* @return void * @param int|null $coroutineId
*/ * @return mixed
public static function remove(string $key, ?int $coroutineId = null): void; */
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 string $key
* @param int $value * @param int|null $coroutineId
* @param int|null $coroutineId * @return void
* @return int */
*/ public static function remove(string $key, ?int $coroutineId = null): void;
public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int;
/** /**
* @param string $id * @param string $id
* @param int $value * @param int $value
* @param int|null $coroutineId * @param int|null $coroutineId
* @return int * @return int
*/ */
public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): 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;
} }
+99 -90
View File
@@ -9,97 +9,106 @@ class CoroutineContext implements ContextInterface
{ {
/** /**
* @param string $key * @return bool
* @param mixed $value */
* @param int|null $coroutineId public static function inCoroutine(): bool
* @return mixed {
*/ return true;
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]);
}
/** /**
* @param string $id * @param string $key
* @param int $value * @param mixed $value
* @param int|null $coroutineId * @param int|null $coroutineId
* @return int * @return mixed
*/ */
public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int public static function set(string $key, mixed $value, ?int $coroutineId = null): mixed
{ {
if (is_null($coroutineId)) { // TODO: Implement set() method.
$coroutineId = Coroutine::getCid(); if (is_null($coroutineId)) {
} $coroutineId = Coroutine::getCid();
if (!isset(Coroutine::getContext($coroutineId)[$id])) { }
Coroutine::getContext($coroutineId)[$id] = 0; return Coroutine::getContext($coroutineId)[$key] = $value;
} }
return Coroutine::getContext($coroutineId)[$id] += $value;
}
/** /**
* @param string $id * @param string $key
* @param int $value * @param mixed|null $defaultValue
* @param int|null $coroutineId * @param int|null $coroutineId
* @return int * @return mixed
*/ */
public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int public static function get(string $key, mixed $defaultValue = null, ?int $coroutineId = null): mixed
{ {
if (is_null($coroutineId)) { // TODO: Implement get() method.
$coroutineId = Coroutine::getCid(); if (is_null($coroutineId)) {
} $coroutineId = Coroutine::getCid();
if (!isset(Coroutine::getContext($coroutineId)[$id])) { }
Coroutine::getContext($coroutineId)[$id] = 0; return Coroutine::getContext($coroutineId)[$key] ?? $defaultValue;
} }
return Coroutine::getContext($coroutineId)[$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.
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;
}
} }