This commit is contained in:
2021-10-25 18:39:30 +08:00
parent 4be5809230
commit e0f548a021
15 changed files with 34 additions and 212 deletions
@@ -1,13 +0,0 @@
<?php
declare(strict_types=1);
namespace Http\Handler\Abstracts;
use Swoole\Coroutine;
abstract class BaseContext
{
protected static array $pool = [];
}
+4 -4
View File
@@ -2,8 +2,9 @@
namespace Http\Handler\Abstracts;
use Annotation\Inject;
use Exception;
use Http\Constrict\ResponseInterface as HttpResponseInterface;
use Http\Handler\Handler as CHl;
use Http\Message\ServerRequest;
use Kiri\Core\Help;
@@ -84,12 +85,11 @@ abstract class Handler implements RequestHandlerInterface
/**
* @param mixed $responseData
* @return \Server\Constrict\ResponseInterface
* @throws Exception
* @return ResponseInterface
*/
private function transferToResponse(mixed $responseData): ResponseInterface
{
$interface = response()->withStatus(200);
$interface = di(HttpResponseInterface::class)->withStatus(200);
if (!$interface->hasContentType()) {
$interface->withContentType('application/json;charset=utf-8');
}
-189
View File
@@ -1,189 +0,0 @@
<?php
namespace Http\Handler;
use Http\Handler\Abstracts\BaseContext;
use Swoole\Coroutine;
/**
* Class Context
* @package Yoc\http
*/
class Context extends BaseContext
{
protected static array $_contents = [];
/**
* @param $id
* @param $context
* @param null $coroutineId
* @return mixed
*/
public static function setContext($id, $context, $coroutineId = null): mixed
{
if (Coroutine::getCid() === -1) {
return static::$_contents[$id] = $context;
}
return Coroutine::getContext($coroutineId)[$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 (!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 (!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
{
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 mixed
*/
public static function getAllContext($coroutineId = null): mixed
{
if (Coroutine::getCid() === -1) {
return Coroutine::getContext($coroutineId) ?? [];
} else {
return static::$_contents ?? [];
}
}
/**
* @param string $id
* @param null $coroutineId
*/
public static function remove(string $id, $coroutineId = null)
{
if (!static::hasContext($id, $coroutineId)) {
return;
}
if (Coroutine::getCid() === -1) {
unset(static::$_contents[$id]);
} else {
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;
}
if (!empty($key) && !isset(static::$_contents[$id][$key])) {
return false;
}
return true;
}
/**
* @param $id
* @param null $key
* @param null $coroutineId
* @return bool
*/
private static function searchByCoroutine($id, $key = null, $coroutineId = null): bool
{
if (!isset(Coroutine::getContext($coroutineId)[$id])) {
return false;
}
if ($key !== null) {
return isset((Coroutine::getContext($coroutineId)[$id] ?? [])[$key]);
}
return true;
}
/**
* @return bool
*/
public static function inCoroutine(): bool
{
return Coroutine::getCid() !== -1;
}
}
-123
View File
@@ -1,123 +0,0 @@
<?php
namespace Http;
use Annotation\Inject;
use Exception;
use Http\Constrict\RequestInterface;
use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Context;
use Http\Handler\Dispatcher;
use Http\Handler\Handler;
use Http\Handler\Router;
use Http\Message\ServerRequest;
use Http\Message\Stream;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Http\Message\ServerRequestInterface;
use Http\Abstracts\ExceptionHandlerInterface;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Http\Events\OnAfterRequest;
use Http\Abstracts\EventDispatchHelper;
use Http\Abstracts\ResponseHelper;
use Http\Constrict\ResponseEmitter;
use Http\Constrict\ResponseInterface;
/**
*
*/
class Http implements OnRequestInterface
{
use EventDispatchHelper;
use ResponseHelper;
/** @var Router|mixed */
#[Inject(Router::class)]
public Router $router;
/**
* @var ExceptionHandlerInterface
*/
public ExceptionHandlerInterface $exceptionHandler;
/**
* @throws ConfigException
*/
public function init()
{
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
}
/**
* @param Request $request
* @param Response $response
* @throws Exception
*/
public function onRequest(Request $request, Response $response): void
{
try {
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
/** @var Handler $handler */
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
if (is_integer($handler)) {
$PsrResponse->withStatus($handler)->withBody(new Stream('Allow Method[' . $request->getMethod() . '].'));
} else if (is_null($handler)) {
$PsrResponse->withStatus(404)->withBody(new Stream('Page not found.'));
} else {
$PsrResponse = $this->handler($handler, $PsrRequest);
}
} catch (\Throwable $throwable) {
$PsrResponse = $this->exceptionHandler->emit($throwable, $this->response);
} finally {
$this->responseEmitter->sender($response, $PsrResponse);
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}
/**
* @param Handler $handler
* @param $PsrRequest
* @return ResponseInterface
* @throws Exception
*/
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
{
$dispatcher = new Dispatcher($handler, $handler->_middlewares);
return $dispatcher->handle($PsrRequest);
}
/**
* @param Request $request
* @return array<ServerRequestInterface, ResponseInterface>
* @throws Exception
*/
private function initRequestResponse(Request $request): array
{
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
if ($PsrRequest->isMethod('OPTIONS')) {
$request->server['request_uri'] = '/*';
}
return [$PsrRequest, $PsrResponse];
}
}
+21 -14
View File
@@ -5,30 +5,27 @@ namespace Http;
use Annotation\Inject;
use Exception;
use Http\Abstracts\EventDispatchHelper;
use Http\Abstracts\ExceptionHandlerInterface;
use Http\Abstracts\ResponseHelper;
use Http\Constrict\RequestInterface;
use Http\Constrict\ResponseEmitter;
use Http\Constrict\ResponseInterface;
use Http\Events\OnAfterRequest;
use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Context;
use Http\Handler\Dispatcher;
use Http\Handler\Handler;
use Http\Handler\Router;
use Http\Message\ServerRequest;
use Http\Message\Stream;
use Kiri\Abstracts\Config;
use Kiri\Di\ContainerInterface;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
use Psr\Http\Message\ServerRequestInterface;
use Http\Abstracts\ExceptionHandlerInterface;
use Server\Context;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Http\Events\OnAfterRequest;
use Http\Abstracts\EventDispatchHelper;
use Http\Abstracts\ResponseHelper;
use Http\Constrict\ResponseEmitter;
use Http\Constrict\ResponseInterface;
/**
*
*/
@@ -49,6 +46,16 @@ class Server implements OnRequestInterface
public ExceptionHandlerInterface $exceptionHandler;
/**
* @param ContainerInterface $container
*/
public function __construct(protected ContainerInterface $container)
{
$this->container->setBindings(RequestInterface::class, Constrict\Request::class);
$this->container->setBindings(ResponseInterface::class, Constrict\Response::class);
}
/**
* @throws ConfigException
*/
@@ -58,8 +65,8 @@ class Server implements OnRequestInterface
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
$this->exceptionHandler = $this->container->get($exceptionHandler);
$this->responseEmitter = $this->container->get(ResponseEmitter::class);
}
@@ -110,7 +117,7 @@ class Server implements OnRequestInterface
*/
private function initRequestResponse(Request $request): array
{
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
$PsrResponse = Context::setContext(ResponseInterface::class, new Message\Response());
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
if ($PsrRequest->isMethod('OPTIONS')) {