This commit is contained in:
2023-04-03 11:08:11 +08:00
parent 73754f3bfb
commit f153fde08d
4 changed files with 258 additions and 196 deletions
+16 -196
View File
@@ -3,224 +3,44 @@
namespace Kiri\Di; namespace Kiri\Di;
use Kiri\Di\Context\AsyncContext;
use Kiri\Di\Context\ContextInterface;
use Kiri\Di\Context\CoroutineContext;
use Swoole\Coroutine; use Swoole\Coroutine;
/** /**
* Class Context * Class Context
* @package Yoc\http * @package Yoc\http
* @mixin ContextInterface
*/ */
class Context class Context
{ {
/** /**
* @var array * @param string $name
*/ * @param array $arguments
protected static array $_contents = [];
/**
* @param $id
* @param $context
* @param null $coroutineId
* @return mixed * @return mixed
*/ */
public static function setContext($id, $context, $coroutineId = null): mixed public static function __callStatic(string $name, array $arguments): mixed
{ {
if (is_null($coroutineId)) { // TODO: Implement __callStatic() method.
$coroutineId = Coroutine::getCid(); if (static::inCoroutine()) {
} return call_user_func([CoroutineContext::class, $name], ...$arguments);
if (Coroutine::getCid() !== -1) {
return Coroutine::getContext($coroutineId)[$id] = $context;
}
return static::$_contents[$id] = $context;
}
/**
* @param $id
* @param int $value
* @param null $coroutineId
* @return bool|int
*/
public static function increment($id, int $value = 1, $coroutineId = null): bool|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 $id
* @param int $value
* @param null $coroutineId
* @return bool|int
*/
public static function decrement($id, int $value = 1, $coroutineId = null): bool|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 $id
* @param null $default
* @param null $coroutineId
* @return mixed
*/
public static function getContext($id, $default = null, $coroutineId = null): mixed
{
if (Coroutine::getCid() === -1) {
return static::loadByStatic($id, $default);
}
return static::loadByContext($id, $default, $coroutineId);
}
/**
* @param $id
* @param null $default
* @param null $coroutineId
* @return mixed
*/
private static function loadByContext($id, $default = null, $coroutineId = null): mixed
{
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
return Coroutine::getContext($coroutineId)[$id] ?? $default;
}
/**
* @param $id
* @param null $default
* @return mixed
*/
private static function loadByStatic($id, $default = null): mixed
{
return static::$_contents[$id] ?? $default;
}
/**
* @param null $coroutineId
* @return Coroutine\Context|array
*/
public static function getAllContext($coroutineId = null): Coroutine\Context|array
{
if (Coroutine::getCid() === -1) {
return Coroutine::getContext((int)$coroutineId) ?? [];
} else { } else {
return static::$_contents ?? []; return call_user_func([AsyncContext::class, $name], ...$arguments);
} }
} }
/**
* @return void
*/
public static function clearAll(): void
{
if (Coroutine::getCid() === -1) {
static::$_contents = [];
}
}
/**
* @param string $id
* @param null $coroutineId
*/
public static function remove(string $id, $coroutineId = null)
{
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!static::hasContext($id, $coroutineId)) {
return;
}
if (Coroutine::getCid() === -1) {
static::$_contents[$id] = null;
unset(static::$_contents[$id]);
} else {
Coroutine::getContext($coroutineId)[$id] = null;
unset(Coroutine::getContext($coroutineId)[$id]);
}
}
/**
* @param $id
* @param null $key
* @param null $coroutineId
* @return bool
*/
public static function hasContext($id, $key = null, $coroutineId = null): bool
{
if (Coroutine::getCid() === -1) {
return static::searchByStatic($id, $key);
}
return static::searchByCoroutine($id, $key, $coroutineId);
}
/**
* @param $id
* @param null $key
* @return bool
*/
private static function searchByStatic($id, $key = null): bool
{
if (!isset(static::$_contents[$id])) {
return false;
}
$value = static::$_contents[$id];
if (!empty($key) && is_array($value)) {
return ($value[$key] ?? null) !== null;
}
return true;
}
/**
* @param $id
* @param null $key
* @param null $coroutineId
* @return bool
*/
private static function searchByCoroutine($id, $key = null, $coroutineId = null): bool
{
if (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
return false;
}
$value = Coroutine::getContext($coroutineId)[$id];
if ($key !== null && is_array($value)) {
return ($value[$key] ?? null) !== null;
}
return true;
}
/** /**
* @return bool * @return bool
*/ */
public static function inCoroutine(): bool public static function inCoroutine(): bool
{ {
return Coroutine::getCid() !== -1; return Coroutine::getCid() > -1;
} }
} }
+88
View File
@@ -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;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace Kiri\Di\Context;
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;
/**
* @param string $key
* @param int|null $coroutineId
* @return void
*/
public static function remove(string $key, ?int $coroutineId = null): void;
/**
* @param $id
* @param int $value
* @param $coroutineId
* @return bool|int
*/
public static function increment($id, int $value = 1, $coroutineId = null): bool|int;
/**
* @param $id
* @param int $value
* @param $coroutineId
* @return bool|int
*/
public static function decrement($id, int $value = 1, $coroutineId = null): bool|int;
}
+95
View File
@@ -0,0 +1,95 @@
<?php
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.
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.
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.
Coroutine::getContext($coroutineId)[$key] = null;
unset(Coroutine::getContext($coroutineId)[$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 (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$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 (is_null($coroutineId)) {
$coroutineId = Coroutine::getCid();
}
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
Coroutine::getContext($coroutineId)[$id] = 0;
}
return Coroutine::getContext($coroutineId)[$id] -= $value;
}
}