Files
kiri-container/Context/CoroutineContext.php
T

106 lines
2.5 KiB
PHP
Raw Normal View History

2023-04-03 11:08:11 +08:00
<?php
2023-04-16 01:45:33 +08:00
declare(strict_types=1);
2023-04-03 11:08:11 +08:00
namespace Kiri\Di\Context;
use Swoole\Coroutine;
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.
2023-04-03 11:10:56 +08:00
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
2023-04-03 11:08:11 +08:00
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.
2023-04-03 11:10:56 +08:00
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
2023-04-03 11:08:11 +08:00
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.
2023-04-03 11:10:56 +08:00
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
2023-04-03 11:08:11 +08:00
Coroutine::getContext($coroutineId)[$key] = null;
unset(Coroutine::getContext($coroutineId)[$key]);
}
/**
2023-04-03 11:10:15 +08:00
* @param string $id
2023-04-03 11:08:11 +08:00
* @param int $value
* @param int|null $coroutineId
2023-04-03 11:17:15 +08:00
* @return int
2023-04-03 11:08:11 +08:00
*/
2023-04-03 11:17:15 +08:00
public static function increment(string $id, int $value = 1, ?int $coroutineId = null): int
2023-04-03 11:08:11 +08:00
{
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$id] += $value;
}
/**
2023-04-03 11:10:15 +08:00
* @param string $id
2023-04-03 11:08:11 +08:00
* @param int $value
* @param int|null $coroutineId
2023-04-03 11:17:15 +08:00
* @return int
2023-04-03 11:08:11 +08:00
*/
2023-04-03 11:17:15 +08:00
public static function decrement(string $id, int $value = 1, ?int $coroutineId = null): int
2023-04-03 11:08:11 +08:00
{
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$id] -= $value;
}
}