Files
kiri-core/http-handler/Context.php
T

190 lines
3.7 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2021-09-18 16:54:39 +08:00
namespace Http\Handler;
2020-08-31 01:27:08 +08:00
2021-09-18 16:54:39 +08:00
use Http\Handler\Abstracts\BaseContext;
2020-08-31 01:27:08 +08:00
use Swoole\Coroutine;
/**
* Class Context
* @package Yoc\http
*/
class Context extends BaseContext
{
2021-07-12 18:51:41 +08:00
protected static array $_contents = [];
/**
* @param $id
* @param $context
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return mixed
*/
2021-08-03 18:18:09 +08:00
public static function setContext($id, $context, $coroutineId = null): mixed
2021-07-12 18:51:41 +08:00
{
if (Coroutine::getCid() === -1) {
return static::$_contents[$id] = $context;
}
2021-08-03 18:18:09 +08:00
return Coroutine::getContext($coroutineId)[$id] = $context;
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param int $value
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return bool|int
*/
2021-08-03 18:18:09 +08:00
public static function increment($id, int $value = 1, $coroutineId = null): bool|int
2021-07-12 18:51:41 +08:00
{
2021-08-03 18:18:09 +08:00
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
2021-08-09 11:41:36 +08:00
Coroutine::getContext($coroutineId)[$id] = 0;
2021-07-12 18:51:41 +08:00
}
2021-08-09 11:41:36 +08:00
return Coroutine::getContext($coroutineId)[$id] += $value;
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param int $value
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return bool|int
*/
2021-08-03 18:18:09 +08:00
public static function decrement($id, int $value = 1, $coroutineId = null): bool|int
2021-07-12 18:51:41 +08:00
{
2021-08-09 11:41:36 +08:00
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
Coroutine::getContext($coroutineId)[$id] = 0;
2021-07-12 18:51:41 +08:00
}
2021-08-09 11:41:36 +08:00
return Coroutine::getContext($coroutineId)[$id] -= $value;
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param null $default
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return mixed
*/
2021-08-03 18:18:09 +08:00
public static function getContext($id, $default = null, $coroutineId = null): mixed
2021-07-12 18:51:41 +08:00
{
if (Coroutine::getCid() === -1) {
return static::loadByStatic($id, $default);
}
2021-08-03 18:18:09 +08:00
return static::loadByContext($id, $default, $coroutineId);
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param null $default
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return mixed
*/
2021-08-03 18:18:09 +08:00
private static function loadByContext($id, $default = null, $coroutineId = null): mixed
2021-07-12 18:51:41 +08:00
{
2021-09-06 17:46:03 +08:00
return Coroutine::getContext($coroutineId)[$id] ?? $default;
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param null $default
* @return mixed
*/
private static function loadByStatic($id, $default = null): mixed
{
2021-09-06 17:46:03 +08:00
return static::$_contents[$id] ?? $default;
2021-07-12 18:51:41 +08:00
}
/**
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return mixed
*/
2021-08-03 18:18:09 +08:00
public static function getAllContext($coroutineId = null): mixed
2021-07-12 18:51:41 +08:00
{
if (Coroutine::getCid() === -1) {
2021-08-03 18:18:09 +08:00
return Coroutine::getContext($coroutineId) ?? [];
2021-07-12 18:51:41 +08:00
} else {
return static::$_contents ?? [];
}
}
/**
* @param string $id
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
*/
2021-08-03 18:18:09 +08:00
public static function remove(string $id, $coroutineId = null)
2021-07-12 18:51:41 +08:00
{
2021-08-03 18:18:09 +08:00
if (!static::hasContext($id, $coroutineId)) {
2021-07-12 18:51:41 +08:00
return;
}
if (Coroutine::getCid() === -1) {
unset(static::$_contents[$id]);
} else {
2021-08-03 18:18:09 +08:00
unset(Coroutine::getContext($coroutineId)[$id]);
2021-07-12 18:51:41 +08:00
}
}
/**
* @param $id
* @param null $key
2021-08-09 15:14:33 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return bool
*/
2021-08-03 18:18:09 +08:00
public static function hasContext($id, $key = null, $coroutineId = null): bool
2021-07-12 18:51:41 +08:00
{
if (Coroutine::getCid() === -1) {
return static::searchByStatic($id, $key);
}
2021-08-03 18:18:09 +08:00
return static::searchByCoroutine($id, $key, $coroutineId);
2021-07-12 18:51:41 +08:00
}
/**
* @param $id
* @param null $key
* @return bool
*/
private static function searchByStatic($id, $key = null): bool
{
if (!isset(static::$_contents[$id])) {
return false;
}
if (!empty($key) && !isset(static::$_contents[$id][$key])) {
return false;
}
return true;
}
/**
* @param $id
* @param null $key
2021-08-03 18:18:09 +08:00
* @param null $coroutineId
2021-07-12 18:51:41 +08:00
* @return bool
*/
2021-08-03 18:18:09 +08:00
private static function searchByCoroutine($id, $key = null, $coroutineId = null): bool
2021-07-12 18:51:41 +08:00
{
2021-08-03 18:18:09 +08:00
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
2021-07-12 18:51:41 +08:00
return false;
}
if ($key !== null) {
2021-08-03 18:18:09 +08:00
return isset((Coroutine::getContext($coroutineId)[$id] ?? [])[$key]);
2021-07-12 18:51:41 +08:00
}
return true;
}
/**
* @return bool
*/
public static function inCoroutine(): bool
{
return Coroutine::getCid() !== -1;
}
2020-08-31 01:27:08 +08:00
}