123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- // 定义项目根目录
- $rootDir = __DIR__;
- // 引入Composer自动加载文件
- require_once $rootDir . '/vendor/autoload.php';
- // 定义模块目录
- $moduleDir = $rootDir . '/app';
- // 存储结果
- $modules = [];
- // 遍历模块目录
- $moduleIterator = new DirectoryIterator($moduleDir);
- foreach ($moduleIterator as $module) {
- if ($module->isDir() && !$module->isDot()) {
- $moduleName = $module->getFilename();
- $controllerDir = $module->getPathname() . '/controller';
-
- if (is_dir($controllerDir)) {
- $controllers = [];
- $controllerIterator = new DirectoryIterator($controllerDir);
- foreach ($controllerIterator as $controller) {
- if ($controller->isFile() && $controller->getExtension() === 'php') {
- $controllerName = basename($controller->getFilename(), '.php');
- if($controllerName ==='Base') continue;
- $controllerClass = "app\\{$moduleName}\\controller\\{$controllerName}";
- require_once $controller->getPathname();
- if (class_exists($controllerClass)) {
- $methods = get_class_methods($controllerClass);
- $actions = array_filter($methods, function ($method) {
- return !in_array($method, ['__construct']);
- });
- $controllers[$controllerName] = $actions;
- // 生成控制器文件注释
- generateControllerComment($rootDir.'/extend/lib/'.$moduleName.'/'.strtolower($controller->getFilename()), $actions);
- }
- }
- }
- if (!empty($controllers)) {
- $modules[$moduleName] = $controllers;
- }
- }
- }
- }
- // 打印结果
- echo json_encode($modules, JSON_PRETTY_PRINT);
- // 生成控制器文件注释
- function generateControllerComment($filePath, $actions)
- {
- $config=[];
- if(file_exists($filePath)){
- $config = include $filePath;
- }
- $methodDocBlocks = [];
- foreach ($actions as $action) {
- if(isset($config[$action])) continue;
- $methodDocBlocks[] = <<<DOCBLOCK
- '$action'=>[
- 'name'=>'',
- "msg"=>"",
- "resplace"=>[],
- ],
- DOCBLOCK;
- }
- if (empty($methodDocBlocks)) return;
- // 确保目录存在
- $dir = dirname($filePath);
- if (!is_dir($dir)) {
- mkdir($dir, 0777, true);
- }
- // 如果文件不存在,创建空文件
- if (!file_exists($filePath)) {
- file_put_contents($filePath, '<?php '. PHP_EOL."return [");
- }
- $content = file_get_contents($filePath,);
- $newContent = str_replace("];", "", $content).PHP_EOL;
- foreach ($methodDocBlocks as $docBlock) {
- $newContent .= $docBlock.PHP_EOL;
- }
- $newContent .= "];";
- file_put_contents($filePath, $newContent);
- }
|