From 67a3d287ada8c0b5fe27631da43158dca1f5d417 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 19 Nov 2022 15:25:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=89=93=E5=8C=85=E5=92=8C?= =?UTF-8?q?=E6=89=93=E5=8C=85=E5=90=8E=E7=9A=84=E8=8A=82=E7=82=B9=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E9=80=BB=E8=BE=91=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/service/NodeService.php | 2 +- app/admin/service/node/Node.php | 52 +++++++++++++++++++++++++------ app/common/command/build/Dist.php | 10 +++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/app/admin/service/NodeService.php b/app/admin/service/NodeService.php index fe5defb..3875d7c 100644 --- a/app/admin/service/NodeService.php +++ b/app/admin/service/NodeService.php @@ -23,4 +23,4 @@ class NodeService ->getNodelist(); return $nodeList; } -} \ No newline at end of file +} diff --git a/app/admin/service/node/Node.php b/app/admin/service/node/Node.php index 681d5d7..aeb687b 100644 --- a/app/admin/service/node/Node.php +++ b/app/admin/service/node/Node.php @@ -58,6 +58,8 @@ class Node */ public function getNodelist() { + + list($nodeList, $controllerList) = [[], $this->getControllerList()]; if (!empty($controllerList)) { @@ -70,7 +72,7 @@ class Node // 获取类和方法的注释信息 $reflectionClass = new \ReflectionClass($controller); - + $methods = $reflectionClass->getMethods(); $actionList = []; @@ -109,13 +111,46 @@ class Node return $nodeList; } + + public function getAllControllerClass() + { + $namespace = $this->baseNamespace; + + $myClasses = array_filter(get_declared_classes(), function ($item) use ($namespace) { + return substr($item, 0, strlen($namespace)) === $namespace; + }); + + $theClasses = []; + foreach ($myClasses as $class) : + $theClasses[] = $class; + endforeach; + return $theClasses; + } + /** * 获取所有控制器 * @return array */ public function getControllerList() { - return $this->readControllerFiles($this->basePath); + $list = []; + if (defined('ULTHON_ADMIN_BUILD_DIST')) { + $list = $this->getAllControllerClass(); + } else { + $list = $this->readControllerFiles($this->basePath); + } + + $target_list = []; + + foreach ($list as $class_name) { + $class_name_main = str_replace($this->baseNamespace . '\\', '', $class_name); + + $controller_format = str_replace('\\', '.', $class_name_main); + + $target_list[$controller_format] = $class_name; + } + + return $target_list; } /** @@ -123,13 +158,14 @@ class Node * @param $path * @return array */ - protected function readControllerFiles($path) + protected function readControllerFiles($path = null) { - - - list($list, $temp_list, $dirExplode) = [[], scandir($path), explode($this->basePath, $path)]; + $temp_list = scandir($path); + $dirExplode = explode($this->basePath, $path); $middleDir = isset($dirExplode[1]) && !empty($dirExplode[1]) ? str_replace('/', '\\', substr($dirExplode[1], 1)) . "\\" : ''; + $list = []; + foreach ($temp_list as $file) { // 排除根目录和没有开启注解的模块 if ($file == ".." || $file == ".") { @@ -147,10 +183,8 @@ class Node } // 根目录下的文件 $className = str_replace('.php', '', $file); - $controllerFormat = str_replace('\\', '.', $middleDir) . Str::snake(lcfirst($className)); - - $list[$controllerFormat] = "{$this->baseNamespace}\\{$middleDir}" . $className; + $list[] = $this->baseNamespace . '\\' . $middleDir . $className; } } diff --git a/app/common/command/build/Dist.php b/app/common/command/build/Dist.php index e70a9db..2323001 100644 --- a/app/common/command/build/Dist.php +++ b/app/common/command/build/Dist.php @@ -325,6 +325,14 @@ class Dist extends Command { $file_stmts = []; + $file_stmts[] = new Expression(new FuncCall( + new Name('define'), + [ + new Arg(new String_('ULTHON_ADMIN_BUILD_DIST')), + new Arg(new String_('1')) + ] + )); + foreach ($files as $file_name) { $file_stmts[] = new Expression(new Include_(new Concat(new Dir, new String_($file_name)), Include_::TYPE_REQUIRE_ONCE)); } @@ -505,7 +513,7 @@ class Dist extends Command * * @param Node\Stmt[]|null $stmts * @param string $name - * @return void + * @return Node\Stmt[] */ public function parseStmts($stmts, $name) {