Files
kiri-core/HttpServer/Http/Context.php
T

202 lines
3.6 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace HttpServer\Http;
use HttpServer\Abstracts\BaseContext;
use Swoole\Coroutine;
/**
* Class Context
* @package Yoc\http
*/
class Context extends BaseContext
{
protected static $_requests = [];
protected static $_response = [];
/**
* @param $id
* @param $context
* @param null $key
* @return mixed
*/
public static function setContext($id, $context, $key = null)
{
if (static::inCoroutine()) {
return self::setCoroutine($id, $context, $key);
} else {
return self::setStatic($id, $context, $key);
}
}
/**
* @param $id
* @param $context
* @param null $key
* @return mixed
*/
private static function setStatic($id, $context, $key = null)
{
if (!empty($key)) {
if (!is_array(static::$_requests[$id])) {
static::$_requests[$id] = [$key => $context];
} else {
static::$_requests[$id][$key] = $context;
}
} else {
static::$_requests[$id] = $context;
}
return $context;
}
/**
* @param $id
* @param $context
* @param null $key
* @return
*/
private static function setCoroutine($id, $context, $key = null)
{
if (!static::hasContext($id)) {
Coroutine::getContext()[$id] = [];
}
2020-09-15 19:09:12 +08:00
if (!empty($key)) {
if (!is_array(Coroutine::getContext()[$id])) {
2020-09-17 13:56:57 +08:00
return Coroutine::getContext()[$id] = [$key => $context];
2020-09-15 19:09:12 +08:00
} else {
2020-09-17 13:56:57 +08:00
return Coroutine::getContext()[$id][$key] = $context;
2020-09-15 19:09:12 +08:00
}
2020-08-31 01:27:08 +08:00
} else {
2020-09-17 13:56:57 +08:00
return Coroutine::getContext()[$id] = $context;
2020-08-31 01:27:08 +08:00
}
}
/**
* @param $id
* @param null $key
* @return false|mixed
*/
public static function autoIncr($id, $key = null)
{
if (!static::inCoroutine()) {
return false;
}
if (!isset(Coroutine::getContext()[$id][$key])) {
return false;
}
return Coroutine::getContext()[$id][$key] += 1;
}
/**
* @param $id
* @param null $key
* @return false|mixed
*/
public static function autoDecr($id, $key = null)
{
2020-09-15 18:48:22 +08:00
if (!static::inCoroutine() || !static::hasContext($id)) {
2020-08-31 01:27:08 +08:00
return false;
}
2020-09-17 13:56:57 +08:00
if (!isset(Coroutine::getContext()[$id][$key])) {
2020-08-31 01:27:08 +08:00
return false;
}
return Coroutine::getContext()[$id][$key] -= 1;
}
/**
* @param $id
* @param null $key
* @return mixed
*/
public static function getContext($id, $key = null)
{
2020-09-15 18:48:22 +08:00
if (!static::hasContext($id)) {
return null;
}
2020-08-31 01:27:08 +08:00
if (static::inCoroutine()) {
2020-09-17 13:56:57 +08:00
if ($key === null) {
return Coroutine::getContext()[$id];
} else {
return Coroutine::getContext()[$id][$key] ?? null;
}
2020-08-31 01:27:08 +08:00
} else {
2020-09-17 13:56:57 +08:00
if ($key === null) {
return static::$_requests[$id];
} else {
return static::$_requests[$id][$key] ?? null;
}
2020-08-31 01:27:08 +08:00
}
}
/**
* @return mixed
*/
public static function getAllContext()
{
if (static::inCoroutine()) {
return Coroutine::getContext() ?? [];
} else {
return static::$_requests ?? [];
}
}
/**
* @param $id
* @param null $key
*/
public static function deleteId($id, $key = null)
{
if (!static::hasContext($id, $key)) {
return;
}
if (static::inCoroutine()) {
if (!empty($key)) {
2020-09-15 18:48:22 +08:00
unset(Coroutine::getContext()[$id][$key]);
2020-08-31 01:27:08 +08:00
} else {
2020-09-15 18:48:22 +08:00
unset(Coroutine::getContext()[$id]);
2020-08-31 01:27:08 +08:00
}
} else {
unset(static::$_requests[$id]);
}
}
/**
* @param $id
* @param null $key
* @return mixed
*/
public static function hasContext($id, $key = null)
{
2020-09-17 13:56:57 +08:00
if (!static::inCoroutine()) {
2020-08-31 01:27:08 +08:00
return false;
}
2020-09-17 13:56:57 +08:00
if (!isset(Coroutine::getContext()[$id])) {
2020-08-31 01:27:08 +08:00
return false;
}
2020-09-17 13:56:57 +08:00
if (Coroutine::getContext()[$id] !== null) {
if ($key === null) {
return true;
}
return isset(Coroutine::getContext()[$id][$key]);
}
return false;
2020-08-31 01:27:08 +08:00
}
/**
* @return bool
*/
public static function inCoroutine()
{
return Coroutine::getCid() > 0;
}
}