makeLan.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // 定义项目根目录
  3. $rootDir = __DIR__;
  4. // 引入Composer自动加载文件
  5. require_once $rootDir . '/vendor/autoload.php';
  6. // 定义模块目录
  7. $moduleDir = $rootDir . '/app';
  8. // 存储结果
  9. $modules = [];
  10. // 遍历模块目录
  11. $moduleIterator = new DirectoryIterator($moduleDir);
  12. foreach ($moduleIterator as $module) {
  13. if ($module->isDir() && !$module->isDot()) {
  14. $moduleName = $module->getFilename();
  15. $controllerDir = $module->getPathname() . '/controller';
  16. if (is_dir($controllerDir)) {
  17. $controllers = [];
  18. $controllerIterator = new DirectoryIterator($controllerDir);
  19. foreach ($controllerIterator as $controller) {
  20. if ($controller->isFile() && $controller->getExtension() === 'php') {
  21. $controllerName = basename($controller->getFilename(), '.php');
  22. if($controllerName ==='Base') continue;
  23. $controllerClass = "app\\{$moduleName}\\controller\\{$controllerName}";
  24. require_once $controller->getPathname();
  25. if (class_exists($controllerClass)) {
  26. $methods = get_class_methods($controllerClass);
  27. $actions = array_filter($methods, function ($method) {
  28. return !in_array($method, ['__construct']);
  29. });
  30. $controllers[$controllerName] = $actions;
  31. // 生成控制器文件注释
  32. generateControllerComment($rootDir.'/extend/lib/'.$moduleName.'/'.strtolower($controller->getFilename()), $actions);
  33. }
  34. }
  35. }
  36. if (!empty($controllers)) {
  37. $modules[$moduleName] = $controllers;
  38. }
  39. }
  40. }
  41. }
  42. // 打印结果
  43. echo json_encode($modules, JSON_PRETTY_PRINT);
  44. // 生成控制器文件注释
  45. function generateControllerComment($filePath, $actions)
  46. {
  47. $config=[];
  48. if(file_exists($filePath)){
  49. $config = include $filePath;
  50. }
  51. $methodDocBlocks = [];
  52. foreach ($actions as $action) {
  53. if(isset($config[$action])) continue;
  54. $methodDocBlocks[] = <<<DOCBLOCK
  55. '$action'=>[
  56. 'name'=>'',
  57. "msg"=>"",
  58. "resplace"=>[],
  59. ],
  60. DOCBLOCK;
  61. }
  62. if (empty($methodDocBlocks)) return;
  63. // 确保目录存在
  64. $dir = dirname($filePath);
  65. if (!is_dir($dir)) {
  66. mkdir($dir, 0777, true);
  67. }
  68. // 如果文件不存在,创建空文件
  69. if (!file_exists($filePath)) {
  70. file_put_contents($filePath, '<?php '. PHP_EOL."return [");
  71. }
  72. $content = file_get_contents($filePath,);
  73. $newContent = str_replace("];", "", $content).PHP_EOL;
  74. foreach ($methodDocBlocks as $docBlock) {
  75. $newContent .= $docBlock.PHP_EOL;
  76. }
  77. $newContent .= "];";
  78. file_put_contents($filePath, $newContent);
  79. }