改名
This commit is contained in:
@@ -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
@@ -8,11 +8,20 @@ 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
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function invoke(mixed $handler, array $params = []): mixed;
|
public function invoke(mixed $handler, array $params = []): mixed;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+144
-136
@@ -6,168 +6,176 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $middle
|
* @param $middle
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function overall($middle): static
|
public function overall($middle): static
|
||||||
{
|
{
|
||||||
$this->overall = $middle;
|
$this->overall = $middle;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调用栈
|
* 调用栈
|
||||||
* @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)) {
|
||||||
$this->pipes = is_array($pipes) ? $pipes : func_get_args();
|
$this->pipes = is_array($pipes) ? $pipes : func_get_args();
|
||||||
} else {
|
} else {
|
||||||
foreach ($pipes as $pipe) {
|
foreach ($pipes as $pipe) {
|
||||||
$this->pipes[] = $pipe;
|
$this->pipes[] = $pipe;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 执行
|
* 执行
|
||||||
* @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;
|
{
|
||||||
if (!empty($this->overall)) {
|
$parameters = $this->passable;
|
||||||
array_unshift($this->pipes, $this->overall);
|
if (!empty($this->overall)) {
|
||||||
}
|
array_unshift($this->pipes, $this->overall);
|
||||||
if (is_array($destination)) {
|
}
|
||||||
$destination = $this->aspect_caller($destination, $parameters);
|
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) {
|
$this->pipeline = array_reduce(array_reverse($this->pipes), $this->carry(),
|
||||||
return call_user_func($destination, ...$parameters);
|
static function () use ($destination, $parameters) {
|
||||||
}
|
return call_user_func($destination, ...$parameters);
|
||||||
);
|
}
|
||||||
return $this->clear();
|
);
|
||||||
}
|
return $this->clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
private function clear()
|
private function clear(): static
|
||||||
{
|
{
|
||||||
$this->pipes = [];
|
$this->pipes = [];
|
||||||
$this->passable = null;
|
$this->passable = null;
|
||||||
$this->overall = null;
|
$this->overall = null;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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;
|
{
|
||||||
/** @var \Annotation\Aspect $aop */
|
[$controller, $action] = $destination;
|
||||||
$aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action);
|
/** @var Aspect $aop */
|
||||||
if (!empty($aop)) {
|
$aop = NoteManager::getSpecify_annotation(Aspect::class, $controller::class, $action);
|
||||||
$aop = Kiri::getDi()->get($aop->aspect);
|
if (!empty($aop)) {
|
||||||
$destination = static function () use ($aop, $destination, $parameters) {
|
$aop = Kiri::getDi()->get($aop->aspect);
|
||||||
/** @var \Kiri\IAspect $aop */
|
$destination = static function () use ($aop, $destination, $parameters) {
|
||||||
return $aop->invoke($destination, $parameters);
|
/** @var IAspect $aop */
|
||||||
};
|
$aop->before();
|
||||||
}
|
$data = $aop->invoke($destination, $parameters);
|
||||||
return $destination;
|
$aop->after($data);
|
||||||
}
|
return $data;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return $destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
* @throws \Exception
|
||||||
public function interpreter(): mixed
|
*/
|
||||||
{
|
public function interpreter(): mixed
|
||||||
return call_user_func($this->pipeline, request());
|
{
|
||||||
}
|
return call_user_func($this->pipeline, request());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置异常处理器
|
* 设置异常处理器
|
||||||
* @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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Closure
|
* @return Closure
|
||||||
*/
|
*/
|
||||||
protected function carry(): Closure
|
protected function carry(): Closure
|
||||||
{
|
{
|
||||||
return static function ($stack, $pipe) {
|
return static function ($stack, $pipe) {
|
||||||
return static function ($passable) use ($stack, $pipe) {
|
return static function ($passable) use ($stack, $pipe) {
|
||||||
if ($pipe instanceof MiddlewareInterface) {
|
if ($pipe instanceof MiddlewareInterface) {
|
||||||
$pipe = [$pipe, 'OnHandler'];
|
$pipe = [$pipe, 'OnHandler'];
|
||||||
}
|
}
|
||||||
return $pipe($passable, $stack);
|
return $pipe($passable, $stack);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异常处理
|
* 异常处理
|
||||||
* @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);
|
||||||
}
|
}
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user