变更
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Di\Context;
|
||||
|
||||
class AsyncContext implements ContextInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 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 $id
|
||||
* @param int $value
|
||||
* @param int|null $coroutineId
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function increment($id, int $value = 1, ?int $coroutineId = null): bool|int
|
||||
{
|
||||
if (!isset(static::$context[$id])) {
|
||||
static::$context[$id] = 0;
|
||||
}
|
||||
return static::$context[$id] += $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param int $value
|
||||
* @param int|null $coroutineId
|
||||
* @return bool|int
|
||||
*/
|
||||
public static function decrement($id, int $value = 1, ?int $coroutineId = null): bool|int
|
||||
{
|
||||
if (!isset(static::$context[$id])) {
|
||||
static::$context[$id] = 0;
|
||||
}
|
||||
return static::$context[$id] -= $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user