From ad6aa3e7b5e23cefd8473fd74fa948734e03faa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 16 Sep 2021 14:19:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-helper/Route/MiddlewareManager.php | 156 ++++++++---------------- http-server/Service/Http.php | 2 + note/Route/Middleware.php | 2 +- 3 files changed, 57 insertions(+), 103 deletions(-) diff --git a/http-helper/Route/MiddlewareManager.php b/http-helper/Route/MiddlewareManager.php index 3a3f4501..e4436476 100644 --- a/http-helper/Route/MiddlewareManager.php +++ b/http-helper/Route/MiddlewareManager.php @@ -5,7 +5,6 @@ namespace Http\Route; use Closure; -use Http\IInterface\MiddlewareInterface; use Kiri\Abstracts\BaseObject; @@ -16,112 +15,65 @@ use Kiri\Abstracts\BaseObject; class MiddlewareManager extends BaseObject { - private static array $_middlewares = []; + private static array $_middlewares = []; - /** - * @param $class - * @param $method - * @param array|string $middlewares - */ - public static function addMiddlewares($class, $method, array|string $middlewares) - { - if (is_object($class)) { - $class = $class::class; - } - if (!isset(static::$_middlewares[$class . '::' . $method])) { - static::$_middlewares[$class . '::' . $method] = []; - } - - if (is_string($middlewares) && !in_array($middlewares, static::$_middlewares[$class . '::' . $method])) { - static::$_middlewares[$class . '::' . $method][] = $middlewares; - return; - } - foreach ($middlewares as $middleware) { - if (in_array($middleware, static::$_middlewares[$class . '::' . $method])) { - continue; - } - static::$_middlewares[$class . '::' . $method][] = $middleware; - } - } + /** + * @param $class + * @param $method + * @param array|string $middlewares + * @return bool + */ + public static function add($class, $method, array|string $middlewares): bool + { + if (is_object($class)) { + $class = $class::class; + } + if (!isset(static::$_middlewares[$class . '::' . $method])) { + static::$_middlewares[$class . '::' . $method] = []; + } + if (is_string($middlewares) && !in_array($middlewares, static::$_middlewares[$class . '::' . $method])) { + static::$_middlewares[$class . '::' . $method][] = $middlewares; + } else { + foreach ($middlewares as $middleware) { + if (in_array($middleware, static::$_middlewares[$class . '::' . $method])) { + continue; + } + static::$_middlewares[$class . '::' . $method][] = $middleware; + } + } + return true; + } - /** - * @param $handler - * @return mixed|null - */ - public static function get($handler) - { - if ($handler instanceof Closure) { - return null; - } - [$class, $method] = [$handler[0]::class, $handler[1]]; - if (!static::hasMiddleware($class, $method)) { - return null; - } - return static::$_middlewares[$class . '::' . $method]; - } + /** + * @param $handler + * @return mixed + */ + public static function get($handler): mixed + { + if ($handler instanceof Closure) { + return null; + } + [$class, $method] = [$handler[0]::class, $handler[1]]; + if (!static::hasMiddleware($class, $method)) { + return null; + } + return static::$_middlewares[$class . '::' . $method]; + } - /** - * @param $class - * @param $method - * @return bool - */ - public static function hasMiddleware($class, $method): bool - { - if (is_object($class)) { - $class = $class::class; - } - return isset(static::$_middlewares[$class . '::' . $method]); - } - - - /** - * @param $class - * @param $method - * @param $caller - * @return mixed - */ - public static function callerMiddlewares($class, $method, $caller): mixed - { - if (is_object($class)) { - $class = $class::class; - } - $middlewares = static::$_middlewares[$class . '::' . $method] ?? []; - if (empty($middlewares)) { - return $caller; - } - return array_reduce(array_reverse($middlewares), function ($stack, $pipe) { - return function ($passable) use ($stack, $pipe) { - if ($pipe instanceof MiddlewareInterface) { - $pipe = [$pipe, 'onHandler']; - } - return call_user_func($pipe, $passable, $stack); - }; - }, $caller); - } - - - /** - * @param $middlewares - * @param Closure $caller - * @return Closure - */ - public static function closureMiddlewares($middlewares, Closure $caller): Closure - { - if (empty($middlewares)) { - return $caller; - } - return array_reduce(array_reverse($middlewares), function ($stack, $pipe) { - return function ($passable) use ($stack, $pipe) { - if ($pipe instanceof MiddlewareInterface) { - $pipe = [$pipe, 'onHandler']; - } - return call_user_func($pipe, $passable, $stack); - }; - }, $caller); - } - + /** + * @param $class + * @param $method + * @return bool + */ + public static function hasMiddleware($class, $method): bool + { + if (is_object($class)) { + $class = $class::class; + } + return isset(static::$_middlewares[$class . '::' . $method]); + } } diff --git a/http-server/Service/Http.php b/http-server/Service/Http.php index 93dd4918..67d20f32 100644 --- a/http-server/Service/Http.php +++ b/http-server/Service/Http.php @@ -52,6 +52,8 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect } catch (Error | \Throwable $exception) { $psr7Response = $this->exceptionHandler->emit($exception, $this->response); } finally { + $request_time_float = $request->server['request_time_float'] - $request->server['request_time']; + $psr7Response->withHeader('Run-Time', microtime(true) - $request_time_float); $this->responseEmitter->sender($response, $psr7Response); $this->eventDispatch->dispatch(new OnAfterRequest()); } diff --git a/note/Route/Middleware.php b/note/Route/Middleware.php index c67bb573..3f563564 100644 --- a/note/Route/Middleware.php +++ b/note/Route/Middleware.php @@ -46,7 +46,7 @@ use Http\IInterface\MiddlewareInterface; */ public function execute(mixed $class, mixed $method = null): mixed { - MiddlewareManager::addMiddlewares($class, $method, $this->middleware); + MiddlewareManager::add($class, $method, $this->middleware); return parent::execute($class, $method); }