This commit is contained in:
2021-08-02 19:06:44 +08:00
parent 8a4c5dab54
commit 6bbd153c6a
3 changed files with 32 additions and 31 deletions
+7 -7
View File
@@ -34,13 +34,13 @@ defined('ASPECT_ERROR') or define('ASPECT_ERROR', 'Aspect annotation must implem
public function execute(mixed $class, mixed $method = ''): bool public function execute(mixed $class, mixed $method = ''): bool
{ {
// TODO: Change the autogenerated stub // TODO: Change the autogenerated stub
if (!in_array(IAspect::class, class_implements($this->aspect))) { // if (!in_array(IAspect::class, class_implements($this->aspect))) {
throw new Exception(ASPECT_ERROR . IAspect::class); // throw new Exception(ASPECT_ERROR . IAspect::class);
} // }
/** @var Aop $aop */ // /** @var Aop $aop */
$aop = Snowflake::getDi()->get(Aop::class); // $aop = Snowflake::getDi()->get(Aop::class);
//
$aop->aop_add([$class, $method], $this->aspect); // $aop->aop_add([$class, $method], $this->aspect);
return true; return true;
} }
+25 -22
View File
@@ -12,7 +12,6 @@ use HttpServer\Abstracts\HttpService;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionException; use ReflectionException;
use Snowflake\Aop;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\IAspect; use Snowflake\IAspect;
@@ -120,14 +119,14 @@ class Node extends HttpService
$manager = di(MiddlewareManager::class); $manager = di(MiddlewareManager::class);
if ($this->handler instanceof Closure) { if ($this->handler instanceof Closure) {
if (!empty($this->middleware)) { if (!empty($this->middleware)) {
$this->callback = $manager->closureMiddlewares($this->middleware, $this->createDispatch()); $this->callback = $manager->closureMiddlewares($this->middleware, $this->normalHandler($this->handler));
} else { } else {
$this->callback = $this->createDispatch(); $this->callback = $this->normalHandler($this->handler);
} }
} else { } else {
$manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware); $manager->addMiddlewares($this->handler[0], $this->handler[1], $this->middleware);
$this->callback = $manager->callerMiddlewares( $this->callback = $manager->callerMiddlewares(
$this->handler[0], $this->handler[1], $this->createDispatch() $this->handler[0], $this->handler[1], $this->aopHandler($this->getAop())
); );
} }
return $this; return $this;
@@ -157,26 +156,10 @@ class Node extends HttpService
/** /**
* @throws ReflectionException * @param IAspect|null $reflect
* @throws Exception
*/
private function createDispatch(): Closure
{
/** @var Aop $aop */
$aop = Snowflake::getDi()->get(Aop::class);
if ($this->handler instanceof Closure || !$aop->hasAop($this->handler)) {
return $this->normalHandler($this->handler);
} else {
return $this->aopHandler($aop->getAop($this->handler));
}
}
/**
* @param IAspect $reflect
* @return Closure * @return Closure
*/ */
private function aopHandler(IAspect $reflect): Closure private function aopHandler(?IAspect $reflect): Closure
{ {
$params = $this->_injectParameters; $params = $this->_injectParameters;
$handler = $this->handler; $handler = $this->handler;
@@ -186,6 +169,26 @@ class Node extends HttpService
} }
/**
* @throws ReflectionException
*/
private function getAop(): ?IAspect
{
[$controller, $action] = $this->handler;
$aspect = Snowflake::getDi()->getMethodAttribute($controller::class, $action);
if (empty($aspect)) {
return null;
}
foreach ($aspect as $value) {
if ($value instanceof IAspect) {
return $value;
}
}
return null;
}
/** /**
* @param $handler * @param $handler
* @return Closure * @return Closure
-2
View File
@@ -33,7 +33,6 @@ class Aop extends Component
if (!isset(static::$_aop[$alias])) { if (!isset(static::$_aop[$alias])) {
static::$_aop[$alias] = []; static::$_aop[$alias] = [];
} }
var_dump('add ' . $alias);
if (in_array($aspect, static::$_aop[$alias])) { if (in_array($aspect, static::$_aop[$alias])) {
return; return;
} }
@@ -47,7 +46,6 @@ class Aop extends Component
*/ */
public function hasAop($handler): bool public function hasAop($handler): bool
{ {
var_dump('check ' . $handler[0]::class . '::' . $handler[1]);
return isset(static::$_aop[$handler[0]::class . '::' . $handler[1]]); return isset(static::$_aop[$handler[0]::class . '::' . $handler[1]]);
} }