This commit is contained in:
2021-01-04 18:21:37 +08:00
parent 4c8e81f417
commit aa7b663967
3 changed files with 72 additions and 25 deletions
+24 -25
View File
@@ -4,12 +4,15 @@ declare(strict_types=1);
namespace HttpServer\Events;
use Annotation\Route\After;
use Exception;
use HttpServer\Abstracts\Callback;
use HttpServer\Events\Utility\AfterRequest;
use HttpServer\Exception\ExitException;
use HttpServer\Http\Request as HRequest;
use HttpServer\Http\Response as HResponse;
use ReflectionException;
use Snowflake\Async;
use Snowflake\Core\Json;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
@@ -36,38 +39,25 @@ class OnRequest extends Callback
*/
public function onHandler(Request $request, Response $response)
{
Coroutine::defer([$this, 'onAfter']);
try {
/** @var HRequest $sRequest */
[$sRequest, $sResponse] = $this->create($request, $response);
if ($sRequest->is('favicon.ico')) {
$sResponse->send($sRequest->isNotFound(), 200);
[$req, $rep] = static::create($request, $response);
if ($req->is('favicon.ico')) {
send(null, 404);
} else {
Snowflake::app()->getRouter()->dispatch();
router()->dispatch();
}
} catch (ExitException | Error | \Throwable $exception) {
if ($exception instanceof ExitException) {
send($exception->getMessage(), $exception->getCode());
} else {
$this->sendErrorMessage($exception);
}
} catch (ExitException $exception) {
send($exception->getMessage(), $exception->getCode());
} catch (Error | \Throwable $exception) {
$this->sendErrorMessage($exception);
} finally {
$logger = Snowflake::app()->getLogger();
$request = get_object_vars($request);
$logger->write(Json::encode($request), 'request');
$this->onAfter();
}
}
/**
* @throws ComponentException
*/
public function onAfter()
{
fire(Event::EVENT_AFTER_REQUEST);
}
/**
* @param $request
* @param $response
@@ -75,12 +65,21 @@ class OnRequest extends Callback
* @throws NotFindClassException
* @throws ReflectionException
*/
private function create($request, $response): array
public static function create($request, $response): array
{
return [HRequest::create($request), HResponse::create($response)];
}
/**
* @throws ComponentException
*/
public function onAfter()
{
fire(Event::EVENT_AFTER_REQUEST);
}
/**
* @param $response
* @throws Exception