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.
}
}
+10 -1
View File
@@ -8,11 +8,20 @@ interface IAspect
{
public function before(): void;
/**
* @param mixed $response
*/
public function after(mixed $response): void;
/**
* @param mixed $handler
* @param array $params
* @return mixed
*/
public function invoke(mixed $handler, array $params = []): mixed;
public function invoke(mixed $handler, array $params = []): mixed;
}
+144 -136
View File
@@ -6,168 +6,176 @@ 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)
{
$this->passable = $passable;
return $this;
}
/**
* 初始数据
* @param $passable
* @return $this
*/
public function send($passable): static
{
$this->passable = $passable;
return $this;
}
/**
* @param $middle
* @return $this
*/
public function overall($middle): static
{
$this->overall = $middle;
return $this;
}
/**
* @param $middle
* @return $this
*/
public function overall($middle): static
{
$this->overall = $middle;
return $this;
}
/**
* 调用栈
* @param $pipes
* @return $this
*/
public function through($pipes)
{
if (empty($pipes)) return $this;
if (empty($this->pipes)) {
$this->pipes = is_array($pipes) ? $pipes : func_get_args();
} else {
foreach ($pipes as $pipe) {
$this->pipes[] = $pipe;
}
}
return $this;
}
/**
* 调用栈
* @param $pipes
* @return $this
*/
public function through($pipes): static
{
if (empty($pipes)) return $this;
if (empty($this->pipes)) {
$this->pipes = is_array($pipes) ? $pipes : func_get_args();
} else {
foreach ($pipes as $pipe) {
$this->pipes[] = $pipe;
}
}
return $this;
}
/**
* 执行
* @param Closure $destination
* @return mixed
*/
public function then(callable $destination)
{
$parameters = $this->passable;
if (!empty($this->overall)) {
array_unshift($this->pipes, $this->overall);
}
if (is_array($destination)) {
$destination = $this->aspect_caller($destination, $parameters);
}
$this->pipeline = array_reduce(array_reverse($this->pipes), $this->carry(),
static function () use ($destination, $parameters) {
return call_user_func($destination, ...$parameters);
}
);
return $this->clear();
}
/**
* 执行
* @param callable $destination
* @return static
* @throws ReflectionException
*/
public function then(callable $destination): static
{
$parameters = $this->passable;
if (!empty($this->overall)) {
array_unshift($this->pipes, $this->overall);
}
if (is_array($destination)) {
$destination = $this->aspect_caller($destination, $parameters);
}
$this->pipeline = array_reduce(array_reverse($this->pipes), $this->carry(),
static function () use ($destination, $parameters) {
return call_user_func($destination, ...$parameters);
}
);
return $this->clear();
}
/**
* @return $this
*/
private function clear()
{
$this->pipes = [];
$this->passable = null;
$this->overall = null;
return $this;
}
/**
* @return $this
*/
private function clear(): static
{
$this->pipes = [];
$this->passable = null;
$this->overall = null;
return $this;
}
/**
* @param $destination
* @param $parameters
* @return \Closure
*/
private function aspect_caller($destination, $parameters)
{
[$controller, $action] = $destination;
/** @var \Annotation\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);
};
}
return $destination;
}
/**
* @param $destination
* @param $parameters
* @return Closure
* @throws ReflectionException
*/
private function aspect_caller($destination, $parameters): Closure
{
[$controller, $action] = $destination;
/** @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 IAspect $aop */
$aop->before();
$data = $aop->invoke($destination, $parameters);
$aop->after($data);
return $data;
};
}
return $destination;
}
/**
* @return mixed
*/
public function interpreter(): mixed
{
return call_user_func($this->pipeline, request());
}
/**
* @return mixed
* @throws \Exception
*/
public function interpreter(): mixed
{
return call_user_func($this->pipeline, request());
}
/**
* 设置异常处理器
* @param callable $handler
* @return $this
*/
public function whenException($handler)
{
$this->exceptionHandler = $handler;
return $this;
}
/**
* 设置异常处理器
* @param callable $handler
* @return $this
*/
public function whenException(callable $handler): static
{
$this->exceptionHandler = $handler;
return $this;
}
/**
* @return \Closure
*/
protected function carry(): Closure
{
return static function ($stack, $pipe) {
return static function ($passable) use ($stack, $pipe) {
if ($pipe instanceof MiddlewareInterface) {
$pipe = [$pipe, 'OnHandler'];
}
return $pipe($passable, $stack);
};
};
}
/**
* @return Closure
*/
protected function carry(): Closure
{
return static function ($stack, $pipe) {
return static function ($passable) use ($stack, $pipe) {
if ($pipe instanceof MiddlewareInterface) {
$pipe = [$pipe, 'OnHandler'];
}
return $pipe($passable, $stack);
};
};
}
/**
* 异常处理
* @param $passable
* @param $e
* @return mixed
* @throws \Throwable
*/
protected function handleException($passable, Throwable $e)
{
if ($this->exceptionHandler) {
return call_user_func($this->exceptionHandler, $passable, $e);
}
throw $e;
}
/**
* 异常处理
* @param $passable
* @param Throwable $e
* @return mixed
* @throws Throwable
*/
protected function handleException($passable, Throwable $e): mixed
{
if ($this->exceptionHandler) {
return call_user_func($this->exceptionHandler, $passable, $e);
}
throw $e;
}
}