Files
kiri-core/HttpServer/Events/OnRequest.php
T

121 lines
2.9 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Events;
2021-03-29 16:27:12 +08:00
use Annotation\Aspect;
use Database\InjectProperty;
2020-09-02 11:38:47 +08:00
use Exception;
2020-09-04 01:05:33 +08:00
use HttpServer\Abstracts\Callback;
2020-12-16 17:48:01 +08:00
use HttpServer\Exception\ExitException;
2020-08-31 01:27:08 +08:00
use HttpServer\Http\Request as HRequest;
use HttpServer\Http\Response as HResponse;
2020-12-17 10:12:54 +08:00
use ReflectionException;
2021-03-09 10:24:04 +08:00
use Snowflake\Core\Json;
2021-03-16 10:53:40 +08:00
use Snowflake\Error\Logger;
2020-09-02 15:45:52 +08:00
use Snowflake\Event;
2020-11-27 14:57:58 +08:00
use Snowflake\Exception\ComponentException;
2020-12-17 10:12:54 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2021-02-24 14:32:10 +08:00
use Swoole\Coroutine;
2020-08-31 01:27:08 +08:00
use Swoole\Error;
use Swoole\Http\Request;
use Swoole\Http\Response;
2020-09-02 11:38:47 +08:00
/**
* Class OnRequest
* @package HttpServer\Events
*/
class OnRequest extends Callback
2020-08-31 01:27:08 +08:00
{
2021-03-16 10:53:40 +08:00
public Event $event;
public Logger $logger;
/**
* @throws Exception
*/
2021-03-29 17:00:53 +08:00
public function init()
2021-03-16 14:46:57 +08:00
{
$this->event = Snowflake::app()->getEvent();
$this->logger = Snowflake::app()->getLogger();
}
2021-03-16 10:53:40 +08:00
2020-08-31 01:27:08 +08:00
/**
* @param Request $request
* @param Response $response
2020-09-02 18:43:23 +08:00
* @return void
2020-09-02 11:38:47 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
2021-01-07 14:09:59 +08:00
public function onHandler(Request $request, Response $response): mixed
2020-08-31 01:27:08 +08:00
{
try {
2021-03-16 14:37:52 +08:00
/** @var HRequest $request */
[$request, $response] = OnRequest::createContext($request, $response);
2021-03-16 10:53:40 +08:00
2021-03-16 14:46:57 +08:00
$this->event->dispatch(Event::EVENT_BEFORE_REQUEST, [$request]);
2021-03-16 10:53:40 +08:00
2021-03-16 14:37:52 +08:00
$result = $request->dispatch();
2021-03-16 14:46:57 +08:00
$this->event->dispatch(Event::EVENT_AFTER_REQUEST, [$request, $result]);
2021-03-16 14:37:52 +08:00
return $result;
2021-01-04 18:21:37 +08:00
} catch (ExitException | Error | \Throwable $exception) {
2021-03-30 11:56:29 +08:00
$this->addError($exception, 'throwable');
2021-02-25 14:42:26 +08:00
return $this->sendErrorMessage($request, $response, $exception);
2021-03-16 10:53:40 +08:00
} finally {
2021-03-16 14:46:57 +08:00
$this->event->trigger(Event::SYSTEM_RESOURCE_RELEASES);
$this->logger->insert();
2020-09-02 15:45:52 +08:00
}
}
2020-11-27 14:26:34 +08:00
2020-12-17 10:12:54 +08:00
/**
* @param $request
* @param $response
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
2021-03-16 14:37:52 +08:00
public static function createContext($request, $response): array
2020-12-17 10:12:54 +08:00
{
return [HRequest::create($request), HResponse::create($response)];
}
2020-09-10 13:42:08 +08:00
/**
2021-02-25 14:42:26 +08:00
* @param $sRequest
* @param $sResponse
2020-09-02 15:45:52 +08:00
* @param $exception
2020-12-16 16:59:33 +08:00
* @return bool|string
2020-09-02 15:45:52 +08:00
* @throws Exception
*/
2021-02-25 14:42:26 +08:00
protected function sendErrorMessage($sRequest, $sResponse, $exception): bool|string
2020-09-02 15:45:52 +08:00
{
2021-03-30 12:03:05 +08:00
$this->addError($exception,'throwable');
2021-02-25 14:42:26 +08:00
if ($sResponse instanceof Response) {
2021-02-25 14:53:55 +08:00
[$sRequest, $sResponse] = [HRequest::create($sRequest), HResponse::create($sResponse)];
2021-02-25 14:42:26 +08:00
}
2020-12-16 16:59:33 +08:00
2021-03-16 14:46:57 +08:00
$this->event->dispatch(Event::EVENT_AFTER_REQUEST, [$sRequest, $exception]);
2021-03-16 14:45:37 +08:00
2021-03-16 14:38:32 +08:00
$headers = $sRequest->headers->get('access-control-request-headers');
$methods = $sRequest->headers->get('access-control-request-method');
2020-12-16 16:59:33 +08:00
$sResponse->addHeader('Access-Control-Allow-Origin', '*');
2021-03-16 14:38:32 +08:00
$sResponse->addHeader('Access-Control-Allow-Headers', $headers);
$sResponse->addHeader('Access-Control-Request-Method', $methods);
2020-12-16 16:59:33 +08:00
2021-03-16 14:37:52 +08:00
if (!($exception instanceof ExitException)) {
return $sResponse->send(\logger()->exception($exception), 200);
} else {
return $sResponse->send($exception->getMessage(), 200);
}
2020-08-31 01:27:08 +08:00
}
}