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;
use Annotation\Inject;
use Psr\Http\Server\MiddlewareInterface;
use Server\Constrict\ResponseInterface;
/**
*
*/
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
{
// TODO: Implement process() method.
\response()->withAccessControlAllowOrigin('*')
$this->response->withAccessControlAllowOrigin('*')
->withAccessControlRequestMethod($request->getAccessControlRequestMethod())
->withAccessControlAllowHeaders($request->getAccessControlAllowHeaders());
var_dump(get_called_class());
return $handler->handle($request);
}
+30 -30
View File
@@ -5,15 +5,14 @@ namespace Server\Service;
use Annotation\Inject;
use Exception;
use Http\Handler\Context;
use Http\Handler\Abstracts\HandlerManager;
use Http\Handler\Abstracts\MiddlewareManager;
use Http\Handler\Context;
use Http\Handler\Dispatcher;
use Http\Handler\Handler;
use Http\Handler\Router;
use Http\Message\ContentType;
use Http\Message\ServerRequest;
use Http\Message\Stream;
use Http\Handler\Abstracts\MiddlewareManager;
use Kiri\Abstracts\Config;
use Kiri\Exception\ConfigException;
use Kiri\Kiri;
@@ -39,32 +38,32 @@ use Swoole\Server;
class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
{
use EventDispatchHelper;
use ResponseHelper;
use EventDispatchHelper;
use ResponseHelper;
/** @var Router|mixed */
#[Inject(Router::class)]
public Router $router;
/** @var Router|mixed */
#[Inject(Router::class)]
public Router $router;
/**
* @var ExceptionHandlerInterface
*/
public ExceptionHandlerInterface $exceptionHandler;
/**
* @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);
}
/**
* @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);
}
/**
@@ -85,7 +84,7 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
public function onRequest(Request $request, Response $response): void
{
try {
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
[$PsrRequest, $PsrResponse] = $this->initRequestResponse($request);
/** @var Handler $handler */
$handler = HandlerManager::get($request->server['request_uri'], $request->getMethod());
if (is_integer($handler)) {
@@ -96,11 +95,11 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
$PsrResponse = $this->handler($handler, $PsrRequest);
}
} catch (\Throwable $throwable) {
$PsrResponse= $this->exceptionHandler->emit($throwable, $this->response);
$PsrResponse = $this->exceptionHandler->emit($throwable, $this->response);
} finally {
$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());
$PsrRequest = Context::setContext(RequestInterface::class, ServerRequest::createServerRequest($request));
if ($PsrRequest->isMethod('OPTIONS')) {
$request->server['request_uri'] = '/*';
}
return [$PsrRequest, $PsrResponse];
}
/**
* @param Server $server
* @param int $fd