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 array $params
+29 -21
View File
@@ -6,28 +6,30 @@ use Annotation\Aspect;
use Closure;
use Http\IInterface\MiddlewareInterface;
use Kiri\Di\NoteManager;
use Kiri\IAspect;
use Kiri\Kiri;
use ReflectionException;
use Throwable;
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
* @return $this
*/
public function send($passable)
public function send($passable): static
{
$this->passable = $passable;
return $this;
@@ -50,7 +52,7 @@ class Pipeline
* @param $pipes
* @return $this
*/
public function through($pipes)
public function through($pipes): static
{
if (empty($pipes)) return $this;
if (empty($this->pipes)) {
@@ -65,10 +67,11 @@ class Pipeline
/**
* 执行
* @param Closure $destination
* @return mixed
* @param callable $destination
* @return static
* @throws ReflectionException
*/
public function then(callable $destination)
public function then(callable $destination): static
{
$parameters = $this->passable;
if (!empty($this->overall)) {
@@ -89,7 +92,7 @@ class Pipeline
/**
* @return $this
*/
private function clear()
private function clear(): static
{
$this->pipes = [];
$this->passable = null;
@@ -101,18 +104,22 @@ class Pipeline
/**
* @param $destination
* @param $parameters
* @return \Closure
* @return Closure
* @throws ReflectionException
*/
private function aspect_caller($destination, $parameters)
private function aspect_caller($destination, $parameters): Closure
{
[$controller, $action] = $destination;
/** @var \Annotation\Aspect $aop */
/** @var Aspect $aop */
$aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action);
if (!empty($aop)) {
$aop = Kiri::getDi()->get($aop->aspect);
$destination = static function () use ($aop, $destination, $parameters) {
/** @var \Kiri\IAspect $aop */
return $aop->invoke($destination, $parameters);
/** @var IAspect $aop */
$aop->before();
$data = $aop->invoke($destination, $parameters);
$aop->after($data);
return $data;
};
}
return $destination;
@@ -121,6 +128,7 @@ class Pipeline
/**
* @return mixed
* @throws \Exception
*/
public function interpreter(): mixed
{
@@ -133,7 +141,7 @@ class Pipeline
* @param callable $handler
* @return $this
*/
public function whenException($handler)
public function whenException(callable $handler): static
{
$this->exceptionHandler = $handler;
return $this;
@@ -141,7 +149,7 @@ class Pipeline
/**
* @return \Closure
* @return Closure
*/
protected function carry(): Closure
{
@@ -158,11 +166,11 @@ class Pipeline
/**
* 异常处理
* @param $passable
* @param $e
* @param Throwable $e
* @return mixed
* @throws \Throwable
* @throws Throwable
*/
protected function handleException($passable, Throwable $e)
protected function handleException($passable, Throwable $e): mixed
{
if ($this->exceptionHandler) {
return call_user_func($this->exceptionHandler, $passable, $e);