| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace UCore\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\File;
- /**
- * 生成 app 目录的文件树并保存到 app/tree.md
- */
- class GenerateAppTreeCommand extends Command
- {
- /**
- * 命令的名称和签名。
- *
- * @var string
- */
- protected $signature = 'ucore:generate-apptree';
- /**
- * 命令的描述。
- *
- * @var string
- */
- protected $description = '生成 app 目录的文件树并保存到 app/tree.md';
- /**
- * 执行控制台命令。
- *
- * @return int
- */
- public function handle()
- {
- $appPath = base_path('app');
- $outputFile = base_path('app/tree.md');
- $tree = $this->generateDirectoryTree($appPath);
- File::put($outputFile, $tree);
- $this->info('File tree generated successfully at app/tree.md');
- return Command::SUCCESS;
- }
- /**
- * 生成目录树结构为字符串
- *
- * @param string $directory 目录路径
- * @param string $prefix 前缀,用于格式化输出
- * @return string 返回目录树的字符串表示
- */
- private function generateDirectoryTree(string $directory, string $prefix = ''): string
- {
- $tree = '';
- $files = scandir($directory);
- foreach ($files as $file) {
- if ($file === '.' || $file === '..') {
- continue;
- }
- $filePath = $directory . DIRECTORY_SEPARATOR . $file;
- $tree .= $prefix . $file . "\n";
- if (is_dir($filePath)) {
- $tree .= $this->generateDirectoryTree($filePath, $prefix . ' ');
- }
- }
- return $tree;
- }
- }
|