From 9fb8dd022abdf8d17ea76d30377f90d24fc19d5c Mon Sep 17 00:00:00 2001 From: wolfcode <37436228+wolf-leo@users.noreply.github.com> Date: Thu, 2 Jan 2025 17:23:13 +0800 Subject: [PATCH] refactor(admin): improve system log middleware - Remove unused imports and annotations- Simplify annotation reading process - Use ReflectionClass and ReflectionMethod directly-Handle annotations using getAttributes() method - Improve code readability and performance --- app/admin/middleware/SystemLog.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/app/admin/middleware/SystemLog.php b/app/admin/middleware/SystemLog.php index 3e67a85..a85ba3f 100644 --- a/app/admin/middleware/SystemLog.php +++ b/app/admin/middleware/SystemLog.php @@ -8,10 +8,7 @@ use app\admin\service\annotation\NodeAnnotation; use app\admin\service\SystemLogService; use app\common\traits\JumpTrait; use app\Request; -use ReflectionClass; use Closure; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\DocParser; use ReflectionException; class SystemLog @@ -67,19 +64,18 @@ class SystemLog $_ignore = (array)$annotation->ignore; if (in_array('log', array_map('strtolower', $_ignore))) return $response; } - - // 支持旧写法 - $reflectionClass = new \ReflectionClass($className); - $properties = $reflectionClass->getDefaultProperties(); - $ignoreLog = $properties['ignoreLog'] ?? []; - if (in_array($_action, $ignoreLog)) return $response; - $parser = new DocParser(); - $parser->setIgnoreNotImportedAnnotations(true); - $reader = new AnnotationReader($parser); - $controllerAnnotation = $reader->getClassAnnotation($reflectionClass, ControllerAnnotation::class); - $reflectionAction = $reflectionClass->getMethod($_action); - $nodeAnnotation = $reader->getMethodAnnotation($reflectionAction, NodeAnnotation::class); - $title = $controllerAnnotation->title . ' - ' . $nodeAnnotation->title; + $controllerTitle = $nodeTitle = ''; + $controllerAttributes = (new \ReflectionClass($className))->getAttributes(ControllerAnnotation::class); + $actionAttributes = $reflectionMethod->getAttributes(NodeAnnotation::class); + foreach ($controllerAttributes as $controllerAttribute) { + $controllerAnnotation = $controllerAttribute->newInstance(); + $controllerTitle = $controllerAnnotation->title ?? ''; + } + foreach ($actionAttributes as $actionAttribute) { + $actionAnnotation = $actionAttribute->newInstance(); + $nodeTitle = $actionAnnotation->title ?? ''; + } + $title = $controllerTitle . ' - ' . $nodeTitle; } }catch (\Throwable $exception) { }