改名
This commit is contained in:
@@ -39,17 +39,18 @@ class OnRequest extends Callback
|
|||||||
/** @var HRequest $sRequest */
|
/** @var HRequest $sRequest */
|
||||||
[$sRequest, $sResponse] = static::setContext($request, $response);
|
[$sRequest, $sResponse] = static::setContext($request, $response);
|
||||||
if ($sRequest->is('favicon.ico')) {
|
if ($sRequest->is('favicon.ico')) {
|
||||||
return $sResponse->send($sRequest->isNotFound(), 200);
|
$params = $sResponse->send($sRequest->isNotFound(), 200);
|
||||||
|
} else {
|
||||||
|
$params = Snowflake::app()->getRouter()->dispatch();
|
||||||
}
|
}
|
||||||
return Snowflake::app()->getRouter()->dispatch();
|
|
||||||
} catch (Error | \Throwable $exception) {
|
} catch (Error | \Throwable $exception) {
|
||||||
$this->sendErrorMessage($sResponse ?? null, $exception, $response);
|
$params = $this->sendErrorMessage($sResponse ?? null, $exception, $response);
|
||||||
} finally {
|
} finally {
|
||||||
$events = Snowflake::app()->getEvent();
|
$events = Snowflake::app()->getEvent();
|
||||||
if (!$events->exists(Event::EVENT_AFTER_REQUEST)) {
|
if (!$events->exists(Event::EVENT_AFTER_REQUEST)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$events->trigger(Event::EVENT_AFTER_REQUEST, [$request]);
|
$events->trigger(Event::EVENT_AFTER_REQUEST, [$sRequest, $params]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,16 +59,17 @@ class OnRequest extends Callback
|
|||||||
* @param $sResponse
|
* @param $sResponse
|
||||||
* @param $exception
|
* @param $exception
|
||||||
* @param $response
|
* @param $response
|
||||||
|
* @return false|int|mixed|string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function sendErrorMessage($sResponse, $exception, $response)
|
protected function sendErrorMessage($sResponse, $exception, $response)
|
||||||
{
|
{
|
||||||
|
$params = $this->format($exception);
|
||||||
if (empty($sResponse)) {
|
if (empty($sResponse)) {
|
||||||
$response->status(200);
|
$sResponse = \response();
|
||||||
$response->end($exception->getMessage());
|
$sResponse->response = $response;
|
||||||
} else {
|
|
||||||
$sResponse->send($this->format($exception), 200);
|
|
||||||
}
|
}
|
||||||
|
return $sResponse->send($params, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ class Response extends Application
|
|||||||
$event = Snowflake::app()->event;
|
$event = Snowflake::app()->event;
|
||||||
$event->trigger('CONSOLE_END');
|
$event->trigger('CONSOLE_END');
|
||||||
|
|
||||||
return 'ok';
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,7 +171,8 @@ class Response extends Application
|
|||||||
$response->header('Content-Type', $this->getContentType());
|
$response->header('Content-Type', $this->getContentType());
|
||||||
$response->header('Access-Control-Allow-Origin', '*');
|
$response->header('Access-Control-Allow-Origin', '*');
|
||||||
$response->header('Run-Time', $this->getRuntime());
|
$response->header('Run-Time', $this->getRuntime());
|
||||||
return $response->end($sendData);
|
$response->end($sendData);
|
||||||
|
return $sendData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ use Exception;
|
|||||||
use HttpServer\Application;
|
use HttpServer\Application;
|
||||||
use HttpServer\Route\Annotation\Annotation;
|
use HttpServer\Route\Annotation\Annotation;
|
||||||
use Snowflake\Core\JSON;
|
use Snowflake\Core\JSON;
|
||||||
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -85,26 +86,6 @@ class Node extends Application
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasAfter()
|
|
||||||
{
|
|
||||||
return count($this->_after) > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $response
|
|
||||||
* @return mixed|null
|
|
||||||
*/
|
|
||||||
public function afterDispatch($response)
|
|
||||||
{
|
|
||||||
$callback = Reduce::after($this->_after);
|
|
||||||
return $callback(\request(), $response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -114,15 +95,6 @@ class Node extends Application
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getAfters()
|
|
||||||
{
|
|
||||||
return $this->_after;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -230,7 +202,11 @@ class Node extends Application
|
|||||||
*/
|
*/
|
||||||
public function addAfter($handler)
|
public function addAfter($handler)
|
||||||
{
|
{
|
||||||
$this->_after[] = $handler;
|
$event = Snowflake::app()->getEvent();
|
||||||
|
if ($event->exists(Event::EVENT_AFTER_REQUEST, $handler)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$event->on(Event::EVENT_AFTER_REQUEST, $handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -419,27 +419,9 @@ class Router extends Application implements RouterInterface
|
|||||||
{
|
{
|
||||||
$request = Context::getContext('request');
|
$request = Context::getContext('request');
|
||||||
if (!($node = $this->find_path($request))) {
|
if (!($node = $this->find_path($request))) {
|
||||||
$response = JSON::to(404, self::NOT_FOUND);
|
return JSON::to(404, self::NOT_FOUND);
|
||||||
response()->send($response, 200);
|
|
||||||
} else {
|
|
||||||
response()->send($response = $node->dispatch(), 200);
|
|
||||||
}
|
}
|
||||||
$this->onAfter($response, $node);
|
return $node->dispatch();
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @param null|Node $node
|
|
||||||
*/
|
|
||||||
private function onAfter($data, $node = null)
|
|
||||||
{
|
|
||||||
Coroutine::defer(function () use ($node, $data) {
|
|
||||||
if (!$node->hasAfter()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$node->afterDispatch($data);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user