This commit is contained in:
2021-09-27 16:17:02 +08:00
parent f2a6975754
commit 431e72421c
3 changed files with 37 additions and 16 deletions
+6 -15
View File
@@ -3,6 +3,7 @@
namespace Http\Handler\Abstracts;
use Annotation\Inject;
use Exception;
use Http\Handler\Handler as CHl;
use Http\Message\ServerRequest;
use Kiri\Core\Help;
@@ -38,7 +39,7 @@ abstract class Handler implements RequestHandlerInterface
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
* @throws \Exception
* @throws Exception
*/
protected function execute(ServerRequestInterface $request): ResponseInterface
{
@@ -48,7 +49,7 @@ abstract class Handler implements RequestHandlerInterface
$middleware = $this->middlewares[$this->offset];
if (!($middleware instanceof MiddlewareInterface)) {
throw new \Exception('get_implements_class($middleware) not found method process.');
throw new Exception('get_implements_class($middleware) not found method process.');
}
$this->offset++;
@@ -57,24 +58,14 @@ abstract class Handler implements RequestHandlerInterface
}
private function redecue()
{
return function ($stack, $pipe) {
return function ($passable) use ($stack, $pipe) {
return ([$pipe, 'process'])($passable, $stack);
};
};
}
/**
* @param ServerRequestInterface $request
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function dispatcher(ServerRequestInterface $request): mixed
{
$response = $this->aspectProxy->proxy($this->handler);
$response = call_user_func($this->handler->callback, ...$this->handler->params);
if (!($response instanceof ResponseInterface)) {
$response = $this->transferToResponse($response);
}
@@ -102,7 +93,7 @@ abstract class Handler implements RequestHandlerInterface
/**
* @param mixed $responseData
* @return \Server\Constrict\ResponseInterface
* @throws \Exception
* @throws Exception
*/
private function transferToResponse(mixed $responseData): ResponseInterface
{
+30
View File
@@ -2,8 +2,10 @@
namespace Http\Handler;
use Annotation\Aspect;
use Closure;
use Http\Handler\Abstracts\MiddlewareManager;
use Kiri\Di\NoteManager;
use Kiri\Events\EventProvider;
use Kiri\Kiri;
use Server\Events\OnAfterWorkerStart;
@@ -43,10 +45,38 @@ class Handler
return;
}
$this->_middlewares = MiddlewareManager::get($this->callback);
$aspect = NoteManager::getSpecify_annotation(Aspect::class, $this->callback[0], $this->callback[1]);
if (!is_null($aspect)) {
$this->recover($aspect);
}
});
}
/**
* @param Aspect $aspect
*/
public function recover(Aspect $aspect)
{
$aspect = Kiri::getDi()->get($aspect->aspect);
if (empty($aspect)) {
return;
}
$callback = $this->callback;
$params = $this->params;
$this->params = [];
$this->callback = static function () use ($aspect, $callback, $params) {
$aspect->before();
$result = $aspect->invoke([$callback, $callback[1]], $params);
$aspect->after($result);
return $result;
};
}
/**
* @param array|Closure $callback
* @throws \ReflectionException
+1 -1
View File
@@ -17,7 +17,7 @@ class AspectProxy extends AProxy implements ProxyInterface
/**
* @param \Http\Handler\Handler $executor
* @param Handler $executor
* @return mixed
*/
public function proxy(Handler $executor): mixed