This commit is contained in:
2020-11-27 14:19:07 +08:00
parent 556b60f256
commit 92a04543ec
4 changed files with 23 additions and 21 deletions
-3
View File
@@ -40,9 +40,6 @@ class OnRequest extends Callback
Coroutine::defer(function () {
fire(Event::EVENT_AFTER_REQUEST, [$sRequest ?? null]);
});
if (Config::get('debug.enable', false, false)) {
function_exists('trackerHookMalloc') && trackerHookMalloc();
}
/** @var HRequest $sRequest */
[$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)];
if ($sRequest->is('favicon.ico')) {
+2 -2
View File
@@ -20,9 +20,9 @@ class JsonFormatter extends Application implements IFormatter
{
public $data;
public $status = 200;
public int $status = 200;
public $header = [];
public array $header = [];
/**
* @param $data
+2 -3
View File
@@ -51,10 +51,9 @@ class Middleware
*/
public function getGenerate(Node $node)
{
$last = function ($passable) use ($node) {
return $node->callback = Reduce::reduce(function ($passable) use ($node) {
return Dispatch::create($node->handler, $passable)->dispatch();
};
return $node->callback = Reduce::reduce($last, $this->annotation($node));
}, $this->annotation($node));
}
+19 -13
View File
@@ -10,8 +10,10 @@ use HttpServer\Http\Request;
use Exception;
use HttpServer\Application;
use HttpServer\Route\Annotation\Http;
use ReflectionException;
use Snowflake\Core\JSON;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
@@ -36,14 +38,14 @@ class Node extends Application
public array $rules = [];
/** @var ?Closure|?array */
public $handler;
public Closure|array|null $handler;
public string $htmlSuffix = '.html';
public bool $enableHtmlSuffix = false;
public array $namespace = [];
public array $middleware = [];
/** @var array|Closure */
public $callback = [];
/** @var array|Closure */
public Closure|array $callback = [];
private string $_alias = '';
@@ -71,7 +73,7 @@ class Node extends Application
} else {
$this->handler = $handler;
}
return $this;
return $this->restructure();
}
@@ -225,10 +227,10 @@ class Node extends Application
* @param Closure|array|string $handler
* @throws Exception
*/
public function addInterceptor($handler)
public function addInterceptor(Closure|string|array $handler)
{
$this->_interceptors[] = $handler;
$this->restructure();
}
@@ -236,9 +238,10 @@ class Node extends Application
* @param Closure|array|string $handler
* @throws Exception
*/
public function addAfter($handler)
public function addAfter(Closure|string|array $handler)
{
$this->_after[] = $handler;
$this->restructure();
}
@@ -246,10 +249,10 @@ class Node extends Application
* @param Closure|array|string $handler
* @throws Exception
*/
public function addLimits($handler)
public function addLimits(Closure|string|array $handler)
{
$this->_limits[] = $handler;
$this->restructure();
}
@@ -356,7 +359,7 @@ class Node extends Application
{
$limits = Snowflake::app()->getLimits();
$limits->addLimits($this->path, $limit, $duration, $isBindConsumer);
return $this;
return $this->restructure();
}
@@ -371,14 +374,17 @@ class Node extends Application
return;
}
$this->middleware = $this->each($middles, $_tmp);
$this->restructure();
}
/**
* @param string|\Closure $class
* @param array|Closure|string $class
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function addMiddleware($class)
public function addMiddleware(Closure|string|array $class)
{
if (!is_callable($class, true)) {
return;
@@ -391,6 +397,7 @@ class Node extends Application
$class = [$class, 'handler'];
}
$this->middleware[] = $class;
$this->restructure();
}
@@ -415,7 +422,6 @@ class Node extends Application
*/
public function dispatch()
{
$node = $this->restructure();
if (empty($node->callback)) {
return JSON::to(404, $node->_error ?? 'Page not found.');
}