From 470b2736b7dc5592a1ce6fb7d722e72516b37aa4 Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Tue, 21 Feb 2023 00:35:14 +0800 Subject: [PATCH] set default dir to new --- src/ZM/Command/Plugin/PluginCommand.php | 4 ++-- src/ZM/Command/Plugin/PluginMakeCommand.php | 9 ++++++--- src/ZM/Utils/CodeGenerator/PluginGenerator.php | 3 ++- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ZM/Command/Plugin/PluginCommand.php b/src/ZM/Command/Plugin/PluginCommand.php index c139b4cc..94b18812 100644 --- a/src/ZM/Command/Plugin/PluginCommand.php +++ b/src/ZM/Command/Plugin/PluginCommand.php @@ -97,11 +97,11 @@ abstract class PluginCommand extends Command * @param string $question 问题 * @param callable $validator 验证器 */ - protected function questionWithOption(string $name, string $question, callable $validator): void + protected function questionWithOption(string $name, string $question, callable $validator, string $default = null): void { /** @var QuestionHelper $helper */ $helper = $this->getHelper('question'); - $question = new Question('' . $question . ''); + $question = new Question('' . $question . '', $default); $question->setValidator($validator); $this->input->setOption($name, $helper->ask($this->input, $this->output, $question)); } diff --git a/src/ZM/Command/Plugin/PluginMakeCommand.php b/src/ZM/Command/Plugin/PluginMakeCommand.php index 3379aa58..6d24d584 100644 --- a/src/ZM/Command/Plugin/PluginMakeCommand.php +++ b/src/ZM/Command/Plugin/PluginMakeCommand.php @@ -7,6 +7,7 @@ namespace ZM\Command\Plugin; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; +use ZM\Exception\FileSystemException; use ZM\Store\FileSystem; use ZM\Utils\CodeGenerator\PluginGenerator; @@ -30,6 +31,7 @@ class PluginMakeCommand extends PluginCommand /** * {@inheritDoc} + * @throws FileSystemException */ protected function handle(): int { @@ -57,15 +59,16 @@ class PluginMakeCommand extends PluginCommand if ($this->input->getOption('type') === 'psr4') { // 询问命名空间 if ($this->input->getOption('namespace') === null) { - $this->questionWithOption('namespace', '请输入插件命名空间:', [$this, 'validateNamespace']); + $default_namespace = explode('/', $this->input->getArgument('name'))[0]; + $this->questionWithOption('namespace', '请输入插件命名空间,输入则使用自定义,回车默认使用 [' . $default_namespace . ']:', [$this, 'validateNamespace'], $default_namespace); } } $generator = new PluginGenerator($this->input->getArgument('name'), $this->plugin_dir); - $generator->generate($this->input->getOptions()); + $dir = $generator->generate($this->input->getOptions()); $this->info('已生成插件:' . $this->input->getArgument('name')); - $this->info('目录位置:' . zm_dir($this->plugin_dir . '/' . $this->input->getArgument('name'))); + $this->info('目录位置:' . $dir); return self::SUCCESS; } } diff --git a/src/ZM/Utils/CodeGenerator/PluginGenerator.php b/src/ZM/Utils/CodeGenerator/PluginGenerator.php index 76985cac..0e16dd65 100644 --- a/src/ZM/Utils/CodeGenerator/PluginGenerator.php +++ b/src/ZM/Utils/CodeGenerator/PluginGenerator.php @@ -23,7 +23,7 @@ class PluginGenerator * @param array $options 传入的命令行选项 * @throws FileSystemException */ - public function generate(array $options): void + public function generate(array $options): string { // 先检查插件目录是否存在,不存在则创建 FileSystem::createDir($this->plugin_dir); @@ -87,6 +87,7 @@ class PluginGenerator } passthru(PHP_BINARY . ' ' . escapeshellcmd($env) . ' dump-autoload'); chdir(WORKING_DIR); + return $plugin_base_dir; } /**