- 
                Notifications
    
You must be signed in to change notification settings  - Fork 32
 
fix(AppStore/CreateCommand): correct namespace formatting and improve JSON output #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(AppStore/CreateCommand): correct namespace formatting and improve JSON output #158
Conversation
… JSON output; fix class name in UninstallScript.stub
          
Walkthrough调整 CreateCommand 对插件路径的解析与 mine.json 生成:路径必须为  Changes
 Sequence Diagram(s)sequenceDiagram
    participant CLI as CLI 用户
    participant Cmd as CreateCommand
    participant FS as 文件系统
    participant JSON as mine.json
    CLI->>Cmd: 调用 create(path, pluginType)
    Cmd->>Cmd: 规范化 path(\ 转 /),校验格式(必须含且仅含一个 /)
    Cmd->>Cmd: createNamespace(path) ⇒ Plugin\<Org>\<Ext>
    Cmd->>Cmd: createMineJson(path, pluginType)
    alt pluginType 是 Backend/Mix
        Cmd->>Cmd: createInstallScript(namespace, path)
        Cmd->>Cmd: createUninstallScript(namespace, path)
        Cmd->>Cmd: createConfigProvider(namespace, path)
        Cmd->>FS: createViewScript(namespace, path)(确保 web/ 存在)
        Cmd->>JSON: 在 mine.json 添加 "config": "Namespace\\ConfigProvider"
    end
    Cmd->>JSON: 写入 name=path、PSR-4 映射(无前导反斜杠),使用 pretty/unescaped JSON
    Cmd->>FS: 写入文件
    FS-->>Cmd: 写入成功
    Cmd-->>CLI: 返回完成
    Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
 Suggested labels
 Suggested reviewers
 Poem
 📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
 🚧 Files skipped from review as they are similar to previous changes (1)
 ⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
 Pre-merge checks (2 passed, 1 warning)❌ Failed checks (1 warning)
 ✅ Passed checks (2 passed)
 ✨ Finishing touches
 🧪 Generate unit tests
 Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment   | 
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/AppStore/src/Command/Stub/UninstallScript.stub (1)
5-5: 建议:为模板补充 strict_types 和 __invoke 返回类型提升生成文件的类型约束与一致性;不改变运行时语义。
可参考:
<?php +declare(strict_types=1); namespace %namespace%; -class UninstallScript { +class UninstallScript { - public function __invoke(){ + public function __invoke(): void { echo "Commands to be executed when uninstalling the plug-in"; } }src/AppStore/src/Command/CreateCommand.php (1)
109-111: 建议:显式处理 JSON 编码与写文件失败为避免 mine.json 为空/损坏仍提示成功,建议启用 JSON_THROW_ON_ERROR 并检查写入结果。
可参考:
- $output = Json::encode($output, \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT); - file_put_contents($path . '/mine.json', $output); - $this->output->success(\sprintf('%s 创建成功', $path . '/mine.json')); + try { + $json = Json::encode( + $output, + \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_PRETTY_PRINT | \JSON_THROW_ON_ERROR + ); + } catch (\Throwable $e) { + $this->output->error(\sprintf('mine.json 生成失败(JSON 编码错误):%s', $e->getMessage())); + return; + } + $mineJsonPath = $path . '/mine.json'; + if (false === @file_put_contents($mineJsonPath, $json)) { + $this->output->error(\sprintf('写入失败:%s', $mineJsonPath)); + return; + } + $this->output->success(\sprintf('%s 创建成功', $mineJsonPath));
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/AppStore/src/Command/CreateCommand.php(2 hunks)src/AppStore/src/Command/Stub/UninstallScript.stub(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
- GitHub Check: Test on PHP8.1 Swoole-master
 - GitHub Check: Test on PHP8.2 Swoole-master
 - GitHub Check: Test on PHP8.1 Swoole-v5.0.3
 - GitHub Check: Test on PHP8.2 Swoole-v5.1.2
 - GitHub Check: Test on PHP8.1 Swoole-v5.1.2
 - GitHub Check: Test on PHP8.2 Swoole-v5.0.3
 - GitHub Check: Test on PHP8.3 Swoole-master
 - GitHub Check: Test on PHP8.3 Swoole-v5.1.2
 
🔇 Additional comments (2)
src/AppStore/src/Command/Stub/UninstallScript.stub (1)
5-5: 类名与模版文件名统一,修正合理与 CreateCommand::createUninstallScript 的引用保持一致,避免生成类名不匹配的问题。
src/AppStore/src/Command/CreateCommand.php (1)
95-96: PSR-4 键去掉前导反斜杠是正确的Composer 规范要求命名空间键不含前导 “\”,当前
$namespace . '\\' => 'src'符合预期。
| 
           @People-Sea 生成的代码命名空间不对  | 
    
…dling in JSON and namespace generation
…rrect namespace usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️  Outside diff range comments (1)
src/AppStore/src/Command/CreateCommand.php (1)
157-164: CLI 选项存在两处问题:无用的 name 参数与非法短选项
- 本 PR 已移除对
 --name的使用,保留该选项会造成困惑。- Symfony Console 的短选项需为单字符,目前
 'name'/'type'/'desc'/'author'为非法值,可能导致运行时异常。- ->addOption('name', 'name', InputOption::VALUE_REQUIRED, 'Plug-in Name') - ->addOption('type', 'type', InputOption::VALUE_OPTIONAL, 'Plugin type, default mix optional mix,frond,backend') - ->addOption('description', 'desc', InputOption::VALUE_OPTIONAL, 'Plug-in Introduction') - ->addOption('author', 'author', InputOption::VALUE_OPTIONAL, 'Plugin Author Information'); + // 移除未使用的 --name;修正短选项为单字符 + ->addOption('type', 't', InputOption::VALUE_OPTIONAL, 'Plugin type (mix|frond|backend). Default: mix') + ->addOption('description', 'd', InputOption::VALUE_OPTIONAL, 'Plug-in Introduction') + ->addOption('author', 'a', InputOption::VALUE_OPTIONAL, 'Plugin Author Information');
🧹 Nitpick comments (4)
src/AppStore/src/Command/CreateCommand.php (4)
64-69: createNamespace 增强健壮性,避免分段异常
explode('/', $pluginPath)假定一定是两段。为防止未来复用时传入不合规路径,建议显式校验段数并抛出异常。- [$orgName, $extName] = explode('/', $pluginPath); + $segments = explode('/', $pluginPath, 3); + if (count($segments) !== 2 || $segments[0] === '' || $segments[1] === '') { + throw new \InvalidArgumentException(sprintf('Invalid plugin path: %s', $pluginPath)); + } + [$orgName, $extName] = $segments;
87-93: 前端-only 插件未创建 web 目录的差异行为当前仅 Backend/Mix 分支会调用
createViewScript()(实际是确保 web 目录存在),Frond 分支不会。若前端插件同样需要 web 目录,建议同步处理以避免模板缺失。- if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) { + if ($pluginType === PluginTypeEnum::Backend || $pluginType === PluginTypeEnum::Mix) { // ... $this->createViewScript($namespace, $path); // ... } + if ($pluginType === PluginTypeEnum::Frond) { + // 仅确保 web 目录 + $this->createViewScript('N/A', $path); + }另外,
createViewScript()目前只创建目录,建议重命名为ensureWebDirectory()以贴合语义。
51-53: 重复创建 web 目录,建议去重
$createDirectors已包含$pluginPath . '/web',而createViewScript()里再次mkdir。建议去掉其一以减少分支差异与无效 I/O。去掉初始化处的 web 目录创建:
- $createDirectors = [ - $pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders', $pluginPath . '/web', - ]; + $createDirectors = [ + $pluginPath, $pluginPath . '/src', $pluginPath . '/Database', $pluginPath . '/Database/Migrations', $pluginPath . '/Database/Seeders', + ];Also applies to: 166-169
112-121: 提示语语言风格统一(中/英混用)同一命令中既有中文“创建成功”也有英文“Created Successfully”。建议统一语言,便于日志检索与用户理解。
Also applies to: 120-121
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/AppStore/src/Command/CreateCommand.php(5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/AppStore/src/Command/CreateCommand.php (2)
src/AppStore/src/Command/AbstractCommand.php (1)
AbstractCommand(17-35)src/AppStore/src/Plugin.php (1)
Plugin(29-434)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
- GitHub Check: Test on PHP8.1 Swoole-v5.1.2
 - GitHub Check: Test on PHP8.2 Swoole-master
 - GitHub Check: Test on PHP8.1 Swoole-master
 - GitHub Check: Test on PHP8.2 Swoole-v5.0.3
 - GitHub Check: Test on PHP8.3 Swoole-master
 - GitHub Check: Test on PHP8.2 Swoole-v5.1.2
 - GitHub Check: Test on PHP8.3 Swoole-v5.1.2
 - GitHub Check: Test on PHP8.1 Swoole-v5.0.3
 
🔇 Additional comments (4)
src/AppStore/src/Command/CreateCommand.php (4)
60-62: 调用时传入绝对路径 OK此处传入
$pluginPath(绝对路径)给createMineJson()合理;其内部通过去前缀获得相对路径使用场景一致。无进一步意见。
96-96: PSR-4 映射移除前导反斜杠是正确修复Composer PSR-4 前缀不应以反斜杠开头,此变更可避免自动加载异常。LGTM。
110-113: JSON 输出选项改进 LGTM开启
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT改善可读性与跨平台一致性。
51-58: 已验证:PHP 环境支持 0o 八进制字面量 — 无需变更
根目录 composer.json(及 src/Crontab/composer.json)将 php 平台约束设为 >=8.1,且当前 CLI 为 PHP 8.2.29;0o755/0o775 在 PHP 8.1+ 受支持。
…r format and prevent invalid characters
Related mineadmin/MineAdmin#697
Summary by CodeRabbit
新功能
改进
修复