This commit is contained in:
2020-09-09 11:05:28 +08:00
parent 45f31df319
commit f1cf5650ff
4 changed files with 28 additions and 27 deletions
+2 -1
View File
@@ -14,6 +14,7 @@ use HttpServer\Service\Http;
use Snowflake\Core\JSON; use Snowflake\Core\JSON;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Error; use Swoole\Error;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
@@ -40,7 +41,7 @@ class OnRequest extends Callback
if ($sRequest->is('favicon.ico')) { if ($sRequest->is('favicon.ico')) {
return $sResponse->send($sRequest->isNotFound(), 200); return $sResponse->send($sRequest->isNotFound(), 200);
} }
$sResponse->send(Snowflake::app()->router->dispatch(), 200); return Snowflake::app()->getRouter()->dispatch();
} catch (Error | \Throwable $exception) { } catch (Error | \Throwable $exception) {
$this->sendErrorMessage($sResponse ?? null, $exception, $response); $this->sendErrorMessage($sResponse ?? null, $exception, $response);
} finally { } finally {
@@ -142,10 +142,7 @@ class Annotation extends \Snowflake\Annotation\Annotation
if (!isset($annotation[1][2])) { if (!isset($annotation[1][2])) {
return; return;
} }
$explode = explode(',', $annotation[1][2]); $explode = explode(',', $annotation[1][2]);
[$keyName, $matchs] = $annotation;
foreach ($explode as $middleware) { foreach ($explode as $middleware) {
$middleware = 'App\Http\Interceptor\\' . $middleware; $middleware = 'App\Http\Interceptor\\' . $middleware;
if (!class_exists($middleware)) { if (!class_exists($middleware)) {
@@ -156,10 +153,6 @@ class Annotation extends \Snowflake\Annotation\Annotation
continue; continue;
} }
$node->addInterceptor([$middleware, 'Interceptor']); $node->addInterceptor([$middleware, 'Interceptor']);
continue;
$params = [$keyName, [$matchs[0], $matchs[1], $middleware]];
$node->addInterceptor($this->pop($this->getName(...$params)));
} }
} }
@@ -176,8 +169,6 @@ class Annotation extends \Snowflake\Annotation\Annotation
} }
$explode = explode(',', $annotation[1][2]); $explode = explode(',', $annotation[1][2]);
[$keyName, $matchs] = $annotation;
foreach ($explode as $middleware) { foreach ($explode as $middleware) {
$middleware = 'App\Http\After\\' . $middleware; $middleware = 'App\Http\After\\' . $middleware;
if (!class_exists($middleware)) { if (!class_exists($middleware)) {
@@ -204,8 +195,6 @@ class Annotation extends \Snowflake\Annotation\Annotation
} }
$explode = explode(',', $annotation[1][2]); $explode = explode(',', $annotation[1][2]);
[$keyName, $matchs] = $annotation;
foreach ($explode as $middleware) { foreach ($explode as $middleware) {
$middleware = 'App\Http\Limits\\' . $middleware; $middleware = 'App\Http\Limits\\' . $middleware;
if (!class_exists($middleware)) { if (!class_exists($middleware)) {
@@ -216,10 +205,6 @@ class Annotation extends \Snowflake\Annotation\Annotation
continue; continue;
} }
$node->addLimits([$middleware, 'next']); $node->addLimits([$middleware, 'next']);
continue;
$params = [$keyName, [$matchs[0], $matchs[1], $middleware]];
$node->addLimits($this->pop($this->getName(...$params)));
} }
} }
+1 -8
View File
@@ -52,14 +52,7 @@ class Middleware
public function getGenerate($node) public function getGenerate($node)
{ {
$last = function ($passable) use ($node) { $last = function ($passable) use ($node) {
$responseData = Dispatch::create($node->handler, $passable)->dispatch(); return Dispatch::create($node->handler, $passable)->dispatch();
Coroutine::defer(function () use ($node, $responseData) {
if ($node->hasAfter()) {
$node->afterDispatch($responseData);
}
});
return $responseData;
// response()->send($responseData, 200);
}; };
return $node->callback = Reduce::reduce($last, $this->annotation($node)); return $node->callback = Reduce::reduce($last, $this->annotation($node));
} }
+25 -3
View File
@@ -14,6 +14,7 @@ use Snowflake\Abstracts\Config;
use Snowflake\Core\JSON; use Snowflake\Core\JSON;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine;
/** /**
* Class Router * Class Router
@@ -26,6 +27,8 @@ class Router extends Application implements RouterInterface
public $groupTacks = []; public $groupTacks = [];
public $dir = 'App\\Http\\Controllers'; public $dir = 'App\\Http\\Controllers';
const NOT_FOUND = 'Page not found or method not allowed.';
/** @var string[] */ /** @var string[] */
public $methods = ['get', 'post', 'options', 'put', 'delete', 'receive']; public $methods = ['get', 'post', 'options', 'put', 'delete', 'receive'];
@@ -407,17 +410,36 @@ class Router extends Application implements RouterInterface
return $newPath; return $newPath;
} }
/** /**
* @return mixed * @return mixed|void
* @throws * @throws
*/ */
public function dispatch() public function dispatch()
{ {
$request = Context::getContext('request'); $request = Context::getContext('request');
if (!($node = $this->find_path($request))) { if (!($node = $this->find_path($request))) {
return JSON::to(404, 'Page not found or method not allowed.'); $response = JSON::to(404, self::NOT_FOUND);
response()->send($response, 200);
} else {
response()->send($response = $node->dispatch(), 200);
} }
return $node->dispatch(); $this->onAfter($response, $node);
}
/**
* @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);
});
} }