This commit is contained in:
2021-09-24 17:46:20 +08:00
parent ab6ee6e95e
commit 7dbadf32e9
3 changed files with 39 additions and 36 deletions
+8
View File
@@ -2,11 +2,19 @@
namespace Http\Handler\Abstracts; namespace Http\Handler\Abstracts;
use Annotation\Inject;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
use Server\Constrict\ResponseInterface;
/**
*
*/
abstract class Middleware implements MiddlewareInterface abstract class Middleware implements MiddlewareInterface
{ {
#[Inject(ResponseInterface::class)]
public ResponseInterface $response;
} }
+1 -6
View File
@@ -20,14 +20,9 @@ class CoreMiddleware extends Middleware
*/ */
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{ {
// TODO: Implement process() method. $this->response->withAccessControlAllowOrigin('*')
\response()->withAccessControlAllowOrigin('*')
->withAccessControlRequestMethod($request->getAccessControlRequestMethod()) ->withAccessControlRequestMethod($request->getAccessControlRequestMethod())
->withAccessControlAllowHeaders($request->getAccessControlAllowHeaders()); ->withAccessControlAllowHeaders($request->getAccessControlAllowHeaders());
var_dump(get_called_class());
return $handler->handle($request); return $handler->handle($request);
} }
+30 -30
View File
@@ -5,15 +5,14 @@ namespace Server\Service;
use Annotation\Inject; use Annotation\Inject;
use Exception; use Exception;
use Http\Handler\Context;
use Http\Handler\Abstracts\HandlerManager; use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Abstracts\MiddlewareManager;
use Http\Handler\Context;
use Http\Handler\Dispatcher; use Http\Handler\Dispatcher;
use Http\Handler\Handler; use Http\Handler\Handler;
use Http\Handler\Router; use Http\Handler\Router;
use Http\Message\ContentType;
use Http\Message\ServerRequest; use Http\Message\ServerRequest;
use Http\Message\Stream; use Http\Message\Stream;
use Http\Handler\Abstracts\MiddlewareManager;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException; use Kiri\Exception\ConfigException;
use Kiri\Kiri; use Kiri\Kiri;
@@ -39,32 +38,32 @@ use Swoole\Server;
class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
{ {
use EventDispatchHelper; use EventDispatchHelper;
use ResponseHelper; use ResponseHelper;
/** @var Router|mixed */ /** @var Router|mixed */
#[Inject(Router::class)] #[Inject(Router::class)]
public Router $router; public Router $router;
/** /**
* @var ExceptionHandlerInterface * @var ExceptionHandlerInterface
*/ */
public ExceptionHandlerInterface $exceptionHandler; public ExceptionHandlerInterface $exceptionHandler;
/** /**
* @throws ConfigException * @throws ConfigException
*/ */
public function init() public function init()
{ {
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class); $exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) { if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
$exceptionHandler = ExceptionHandlerDispatcher::class; $exceptionHandler = ExceptionHandlerDispatcher::class;
} }
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler); $this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class); $this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
} }
/** /**
@@ -85,7 +84,7 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
public function onRequest(Request $request, Response $response): void public function onRequest(Request $request, Response $response): void
{ {
try { try {
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request); [$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
/** @var Handler $handler */ /** @var Handler $handler */
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod()); $handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
if (is_integer($handler)) { if (is_integer($handler)) {
@@ -96,11 +95,11 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
$PsrResponse = $this->handler($handler, $PsrRequest); $PsrResponse = $this->handler($handler, $PsrRequest);
} }
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$PsrResponse= $this->exceptionHandler->emit($throwable, $this->response); $PsrResponse = $this->exceptionHandler->emit($throwable, $this->response);
} finally { } finally {
$this->responseEmitter->sender($response, $PsrResponse); $this->responseEmitter->sender($response, $PsrResponse);
$this->eventDispatch->dispatch(new OnAfterRequest()); $this->eventDispatch->dispatch(new OnAfterRequest());
} }
} }
@@ -130,12 +129,13 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
$PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response()); $PsrResponse = Context::setContext(ResponseInterface::class, new \Http\Message\Response());
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request)); $PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
if ($PsrRequest->isMethod('OPTIONS')) {
$request->server['request_uri'] = '/*';
}
return [$PsrRequest, $PsrResponse]; return [$PsrRequest, $PsrResponse];
} }
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd