改名
This commit is contained in:
+1
-48
@@ -4,7 +4,7 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
|
||||
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Http\Handler\Context;
|
||||
use Server\Context;
|
||||
use Http\Handler\Router;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Config;
|
||||
@@ -18,9 +18,6 @@ use Kiri\Exception\ConfigException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Server\Constrict\Request;
|
||||
use Server\Constrict\Response;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Server\ServerManager;
|
||||
use Swoole\WebSocket\Server;
|
||||
use Http\Message\Response as Par7Response;
|
||||
@@ -732,19 +729,6 @@ if (!function_exists('get_file_extension')) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('request')) {
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
* @throws Exception
|
||||
*/
|
||||
function request(): Request
|
||||
{
|
||||
return Kiri::getDi()->get(Request::class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('storage')) {
|
||||
|
||||
/**
|
||||
@@ -816,22 +800,6 @@ if (!function_exists('name')) {
|
||||
|
||||
}
|
||||
|
||||
if (!function_exists('response')) {
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
* @throws
|
||||
*/
|
||||
function response(): Response
|
||||
{
|
||||
if (!Context::hasContext(ResponseInterface::class)) {
|
||||
Context::setContext(ResponseInterface::class, new Par7Response());
|
||||
}
|
||||
return di(ResponseInterface::class);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('zero_full')) {
|
||||
function zero_full(int $data = 1, int $length = 10): string
|
||||
@@ -841,21 +809,6 @@ if (!function_exists('zero_full')) {
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('redirect')) {
|
||||
|
||||
|
||||
/**
|
||||
* @param $url
|
||||
* @return int
|
||||
*/
|
||||
function redirect($url): int
|
||||
{
|
||||
return response()->redirect($url);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
if (!function_exists('env')) {
|
||||
|
||||
/**
|
||||
|
||||
@@ -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');
|
||||
}
|
||||
|
||||
@@ -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
@@ -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')) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Http\Handler\Abstracts;
|
||||
namespace Server\Abstracts;
|
||||
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace Http\Handler;
|
||||
namespace Server;
|
||||
|
||||
use Http\Handler\Abstracts\BaseContext;
|
||||
use Server\Abstracts\BaseContext;
|
||||
use Swoole\Coroutine;
|
||||
|
||||
/**
|
||||
@@ -2,14 +2,12 @@
|
||||
|
||||
namespace Kiri\Cache\Base;
|
||||
|
||||
use Http\Handler\Context;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\RedisConnectException;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Pool\StopHeartbeatCheck;
|
||||
use RedisException;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Context;
|
||||
use Swoole\Timer;
|
||||
|
||||
|
||||
|
||||
@@ -10,8 +10,6 @@ declare(strict_types=1);
|
||||
namespace Kiri\Cache;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Swoole\Coroutine\System;
|
||||
|
||||
@@ -11,9 +11,7 @@ namespace Kiri\Cache;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Events\OnWorkerStop;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Core\Json;
|
||||
@@ -49,7 +47,6 @@ class Redis extends Component
|
||||
|
||||
$length = Config::get('connections.pool.max', 10);
|
||||
|
||||
$this->eventProvider->on(OnAfterRequest::class, [$this, 'release'], 0);
|
||||
$this->eventProvider->on(OnWorkerExit::class, [$this, 'destroy'], 0);
|
||||
|
||||
$connections->initConnections('Redis:' . $config['host'], true, $length);
|
||||
|
||||
@@ -14,7 +14,6 @@ use Closure;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\BaseObject;
|
||||
use Kiri\Abstracts\Logger;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionClass;
|
||||
@@ -22,10 +21,6 @@ use ReflectionException;
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
use ReflectionProperty;
|
||||
use Server\Constrict\Request;
|
||||
use Server\Constrict\Response;
|
||||
use Server\Constrict\RequestInterface;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Class Container
|
||||
@@ -62,8 +57,6 @@ class Container extends BaseObject implements ContainerInterface
|
||||
|
||||
/** @var array|string[] */
|
||||
private array $_interfaces = [
|
||||
RequestInterface::class => Request::class,
|
||||
ResponseInterface::class => Response::class,
|
||||
LoggerInterface::class => Logger::class
|
||||
];
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Kiri\Di;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Kiri\Pool;
|
||||
use Closure;
|
||||
use Database\Mysql\PDO;
|
||||
use Exception;
|
||||
use Http\Handler\Context;
|
||||
use Server\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Error;
|
||||
|
||||
@@ -5,7 +5,7 @@ namespace Kiri\Pool;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Http\Handler\Context;
|
||||
use Server\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace Kiri\Pool;
|
||||
use Annotation\Inject;
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Http\Handler\Context;
|
||||
use Server\Context;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Exception\ConfigException;
|
||||
|
||||
@@ -9,7 +9,7 @@ interface ProxyInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param \Http\Handler\Handler $executor
|
||||
* @param Handler $executor
|
||||
* @return mixed
|
||||
*/
|
||||
public function proxy(Handler $executor): mixed;
|
||||
|
||||
Reference in New Issue
Block a user