This commit is contained in:
2021-09-06 11:26:22 +08:00
parent 4fac0621e7
commit 3088530d9d
3 changed files with 164 additions and 137 deletions
+10
View File
@@ -50,4 +50,14 @@ class LoggerAspect implements IAspect
} }
public function before(): void
{
// TODO: Implement before() method.
}
public function after(mixed $response): void
{
// TODO: Implement after() method.
}
} }
+9
View File
@@ -8,6 +8,15 @@ interface IAspect
{ {
public function before(): void;
/**
* @param mixed $response
*/
public function after(mixed $response): void;
/** /**
* @param mixed $handler * @param mixed $handler
* @param array $params * @param array $params
+29 -21
View File
@@ -6,28 +6,30 @@ use Annotation\Aspect;
use Closure; use Closure;
use Http\IInterface\MiddlewareInterface; use Http\IInterface\MiddlewareInterface;
use Kiri\Di\NoteManager; use Kiri\Di\NoteManager;
use Kiri\IAspect;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException;
use Throwable; use Throwable;
class Pipeline class Pipeline
{ {
protected $passable; protected mixed $passable;
protected $overall; protected mixed $overall;
protected $pipes = []; protected mixed $pipes = [];
protected $pipeline; protected mixed $pipeline;
protected $exceptionHandler; protected mixed $exceptionHandler;
/** /**
* 初始数据 * 初始数据
* @param $passable * @param $passable
* @return $this * @return $this
*/ */
public function send($passable) public function send($passable): static
{ {
$this->passable = $passable; $this->passable = $passable;
return $this; return $this;
@@ -50,7 +52,7 @@ class Pipeline
* @param $pipes * @param $pipes
* @return $this * @return $this
*/ */
public function through($pipes) public function through($pipes): static
{ {
if (empty($pipes)) return $this; if (empty($pipes)) return $this;
if (empty($this->pipes)) { if (empty($this->pipes)) {
@@ -65,10 +67,11 @@ class Pipeline
/** /**
* 执行 * 执行
* @param Closure $destination * @param callable $destination
* @return mixed * @return static
* @throws ReflectionException
*/ */
public function then(callable $destination) public function then(callable $destination): static
{ {
$parameters = $this->passable; $parameters = $this->passable;
if (!empty($this->overall)) { if (!empty($this->overall)) {
@@ -89,7 +92,7 @@ class Pipeline
/** /**
* @return $this * @return $this
*/ */
private function clear() private function clear(): static
{ {
$this->pipes = []; $this->pipes = [];
$this->passable = null; $this->passable = null;
@@ -101,18 +104,22 @@ class Pipeline
/** /**
* @param $destination * @param $destination
* @param $parameters * @param $parameters
* @return \Closure * @return Closure
* @throws ReflectionException
*/ */
private function aspect_caller($destination, $parameters) private function aspect_caller($destination, $parameters): Closure
{ {
[$controller, $action] = $destination; [$controller, $action] = $destination;
/** @var \Annotation\Aspect $aop */ /** @var Aspect $aop */
$aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action); $aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action);
if (!empty($aop)) { if (!empty($aop)) {
$aop = Kiri::getDi()->get($aop->aspect); $aop = Kiri::getDi()->get($aop->aspect);
$destination = static function () use ($aop, $destination, $parameters) { $destination = static function () use ($aop, $destination, $parameters) {
/** @var \Kiri\IAspect $aop */ /** @var IAspect $aop */
return $aop->invoke($destination, $parameters); $aop->before();
$data = $aop->invoke($destination, $parameters);
$aop->after($data);
return $data;
}; };
} }
return $destination; return $destination;
@@ -121,6 +128,7 @@ class Pipeline
/** /**
* @return mixed * @return mixed
* @throws \Exception
*/ */
public function interpreter(): mixed public function interpreter(): mixed
{ {
@@ -133,7 +141,7 @@ class Pipeline
* @param callable $handler * @param callable $handler
* @return $this * @return $this
*/ */
public function whenException($handler) public function whenException(callable $handler): static
{ {
$this->exceptionHandler = $handler; $this->exceptionHandler = $handler;
return $this; return $this;
@@ -141,7 +149,7 @@ class Pipeline
/** /**
* @return \Closure * @return Closure
*/ */
protected function carry(): Closure protected function carry(): Closure
{ {
@@ -158,11 +166,11 @@ class Pipeline
/** /**
* 异常处理 * 异常处理
* @param $passable * @param $passable
* @param $e * @param Throwable $e
* @return mixed * @return mixed
* @throws \Throwable * @throws Throwable
*/ */
protected function handleException($passable, Throwable $e) protected function handleException($passable, Throwable $e): mixed
{ {
if ($this->exceptionHandler) { if ($this->exceptionHandler) {
return call_user_func($this->exceptionHandler, $passable, $e); return call_user_func($this->exceptionHandler, $passable, $e);