change commands definition to attribute

This commit is contained in:
sunxyw
2022-12-17 21:53:08 +08:00
parent c6f877700e
commit 3806983bc2
11 changed files with 36 additions and 73 deletions

View File

@@ -4,19 +4,15 @@ declare(strict_types=1);
namespace ZM\Command\BotCraft; namespace ZM\Command\BotCraft;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface; use ZM\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
/** #[AsCommand(name: 'bc:make', description: '生成插件')]
* TODO: 用于从命令行创建插件
*/
class BotCraftCommand extends Command class BotCraftCommand extends Command
{ {
protected static $defaultName = 'bc:make'; protected function handle(): int
public function execute(InputInterface $input, OutputInterface $output)
{ {
return 0; // TODO: Implement handle() method.
return self::SUCCESS;
} }
} }

View File

@@ -4,22 +4,20 @@ declare(strict_types=1);
namespace ZM\Command; namespace ZM\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'build', description: '将项目构建一个phar包')]
class BuildCommand extends Command class BuildCommand extends Command
{ {
// the name of the command (the part after "bin/console")
protected static $defaultName = 'build';
/** /**
* 配置 * 配置
*/ */
protected function configure() protected function configure()
{ {
$this->setDescription('Build an ".phar" file | 将项目构建一个phar包');
$this->setHelp('此功能将会把整个项目打包为phar'); $this->setHelp('此功能将会把整个项目打包为phar');
$this->addOption('target', 'D', InputOption::VALUE_REQUIRED, 'Output Directory | 指定输出目录'); $this->addOption('target', 'D', InputOption::VALUE_REQUIRED, 'Output Directory | 指定输出目录');
// ... // ...

View File

@@ -4,21 +4,16 @@ declare(strict_types=1);
namespace ZM\Command; namespace ZM\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'check:config', description: '检查配置文件是否和框架当前版本有更新')]
class CheckConfigCommand extends Command class CheckConfigCommand extends Command
{ {
protected static $defaultName = 'check:config';
private $need_update = false; private $need_update = false;
protected function configure()
{
$this->setDescription('检查配置文件是否和框架当前版本有更新');
}
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
if (LOAD_MODE !== 1) { if (LOAD_MODE !== 1) {

View File

@@ -4,20 +4,14 @@ declare(strict_types=1);
namespace ZM\Command\Generate; namespace ZM\Command\Generate;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'generate:systemd', description: '生成systemd配置文件')]
class SystemdGenerateCommand extends Command class SystemdGenerateCommand extends Command
{ {
// the name of the command (the part after "bin/console")
protected static $defaultName = 'generate:systemd';
protected function configure()
{
$this->setDescription('生成框架的 systemd 配置文件');
}
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
config()->addConfigPath(SOURCE_ROOT_DIR . '/config'); config()->addConfigPath(SOURCE_ROOT_DIR . '/config');

View File

@@ -4,23 +4,21 @@ declare(strict_types=1);
namespace ZM\Command; namespace ZM\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleSectionOutput; use Symfony\Component\Console\Output\ConsoleSectionOutput;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use ZM\Exception\InitException; use ZM\Exception\InitException;
#[AsCommand(name: 'init', description: '初始化框架运行的基础文件')]
class InitCommand extends Command class InitCommand extends Command
{ {
// the name of the command (the part after "bin/console")
protected static $defaultName = 'init';
private string $base_path; private string $base_path;
private bool $force = false; private bool $force = false;
protected function configure(): void protected function configure(): void
{ {
$this->setDescription('初始化框架运行的基础文件');
$this->setDefinition([ $this->setDefinition([
new InputOption('force', 'f', InputOption::VALUE_NONE, '覆盖现有文件'), new InputOption('force', 'f', InputOption::VALUE_NONE, '覆盖现有文件'),
new InputOption('docker', null, InputOption::VALUE_NONE, '启用 Docker 支持'), new InputOption('docker', null, InputOption::VALUE_NONE, '启用 Docker 支持'),

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ZM\Command; namespace ZM\Command;
use OneBot\Driver\Workerman\Worker; use OneBot\Driver\Workerman\Worker;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
@@ -14,11 +15,9 @@ use Workerman\Connection\AsyncUdpConnection;
use Workerman\Timer; use Workerman\Timer;
use ZM\Logger\ConsoleLogger; use ZM\Logger\ConsoleLogger;
#[AsCommand(name: 'proxy', description: '开启一个 HTTP 代理服务器')]
class ProxyServerCommand extends Command class ProxyServerCommand extends Command
{ {
// the name of the command (the part after "bin/console")
protected static $defaultName = 'proxy';
private array $config = []; private array $config = [];
public function onSocksMessage($connection, $buffer) public function onSocksMessage($connection, $buffer)
@@ -394,14 +393,13 @@ class ProxyServerCommand extends Command
*/ */
protected function configure() protected function configure()
{ {
$this->setDescription('Start HTTP proxy server | 开启一个 HTTP 代理服务器'); $this->setDescription('开启一个 HTTP 代理服务器');
$this->addOption('type', 't', InputOption::VALUE_REQUIRED, '类型可选http、socks'); $this->addOption('type', 't', InputOption::VALUE_REQUIRED, '类型可选http、socks');
$this->addOption('host', 'H', InputOption::VALUE_REQUIRED, '监听地址默认0.0.0.0'); $this->addOption('host', 'H', InputOption::VALUE_REQUIRED, '监听地址默认0.0.0.0');
$this->addOption('port', 'P', InputOption::VALUE_REQUIRED, '监听端口默认8080'); $this->addOption('port', 'P', InputOption::VALUE_REQUIRED, '监听端口默认8080');
$this->addOption('udp-port', 'U', InputOption::VALUE_REQUIRED, '监听端口默认8080'); $this->addOption('udp-port', 'U', InputOption::VALUE_REQUIRED, '监听端口默认8080');
$this->addOption('username', 'u', InputOption::VALUE_REQUIRED, '认证用的用户名'); $this->addOption('username', 'u', InputOption::VALUE_REQUIRED, '认证用的用户名');
$this->addOption('password', 'p', InputOption::VALUE_REQUIRED, '认证用的密码'); $this->addOption('password', 'p', InputOption::VALUE_REQUIRED, '认证用的密码');
// ...
} }
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int

View File

@@ -7,20 +7,15 @@ namespace ZM\Command;
use Psy\Configuration; use Psy\Configuration;
use Psy\Shell; use Psy\Shell;
use Psy\VersionUpdater\Checker; use Psy\VersionUpdater\Checker;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use ZM\Framework; use ZM\Framework;
#[AsCommand(name: 'repl', description: '交互式控制台')]
class ReplCommand extends Command class ReplCommand extends Command
{ {
protected static $defaultName = 'repl'; public function handle(): int
protected static $defaultDescription = '交互式控制台';
public function execute(InputInterface $input, OutputInterface $output): int
{ {
$config = Configuration::fromInput($input); $config = Configuration::fromInput($this->input);
$config->setUpdateCheck(Checker::NEVER); $config->setUpdateCheck(Checker::NEVER);
$config->setStartupMessage('你可以使用 "help" 来查看帮助'); $config->setStartupMessage('你可以使用 "help" 来查看帮助');
@@ -28,13 +23,13 @@ class ReplCommand extends Command
$shell->addCommands([]); // TODO: add some great commands $shell->addCommands([]); // TODO: add some great commands
try { try {
$output->writeln(sprintf('<fg=blue>Zhamao Repl on Zhamao Framework %s</>', Framework::VERSION)); $this->info(sprintf('<fg=blue>Zhamao Repl on Zhamao Framework %s</>', Framework::VERSION));
$shell->run(); $shell->run();
} catch (\Throwable $e) { } catch (\Throwable $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); $this->error($e->getMessage());
return Command::FAILURE; return self::FAILURE;
} }
return Command::SUCCESS; return self::SUCCESS;
} }
} }

View File

@@ -5,18 +5,13 @@ declare(strict_types=1);
namespace ZM\Command\Server; namespace ZM\Command\Server;
use Swoole\Process; use Swoole\Process;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'server:reload', description: '重载服务器')]
class ServerReloadCommand extends ServerCommand class ServerReloadCommand extends ServerCommand
{ {
protected static $defaultName = 'server:reload';
protected function configure()
{
$this->setDescription('重载框架');
}
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
parent::execute($input, $output); parent::execute($input, $output);

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ZM\Command\Server; namespace ZM\Command\Server;
use OneBot\Driver\Process\ProcessManager; use OneBot\Driver\Process\ProcessManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@@ -13,20 +14,20 @@ use ZM\Exception\ZMKnownException;
use ZM\Framework; use ZM\Framework;
use ZM\Process\ProcessStateManager; use ZM\Process\ProcessStateManager;
#[AsCommand(name: 'server:start', description: '启动服务器', aliases: ['server'])]
class ServerStartCommand extends ServerCommand class ServerStartCommand extends ServerCommand
{ {
protected static $defaultName = 'server';
public static function exportOptionArray(): array public static function exportOptionArray(): array
{ {
$cmd = new self(); $cmd = new self();
$cmd->configure(); $cmd->configure();
return array_map(function ($x) { return $x->getDefault(); }, $cmd->getDefinition()->getOptions()); return array_map(function ($x) {
return $x->getDefault();
}, $cmd->getDefinition()->getOptions());
} }
protected function configure() protected function configure()
{ {
$this->setAliases(['server:start']);
$this->setDefinition([ $this->setDefinition([
new InputOption('config-dir', null, InputOption::VALUE_REQUIRED, '指定其他配置文件目录'), new InputOption('config-dir', null, InputOption::VALUE_REQUIRED, '指定其他配置文件目录'),
new InputOption('driver', null, InputOption::VALUE_REQUIRED, '指定驱动类型'), new InputOption('driver', null, InputOption::VALUE_REQUIRED, '指定驱动类型'),
@@ -40,7 +41,6 @@ class ServerStartCommand extends ServerCommand
new InputOption('private-mode', null, null, '启动时隐藏MOTD和敏感信息'), new InputOption('private-mode', null, null, '启动时隐藏MOTD和敏感信息'),
new InputOption('print-process-pid', null, null, '打印所有进程的PID'), new InputOption('print-process-pid', null, null, '打印所有进程的PID'),
]); ]);
$this->setDescription('Run zhamao-framework | 启动框架');
$this->setHelp('直接运行可以启动'); $this->setHelp('直接运行可以启动');
} }

View File

@@ -4,18 +4,13 @@ declare(strict_types=1);
namespace ZM\Command\Server; namespace ZM\Command\Server;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name: 'server:status', description: '查看服务器状态')]
class ServerStatusCommand extends ServerCommand class ServerStatusCommand extends ServerCommand
{ {
protected static $defaultName = 'server:status';
protected function configure()
{
$this->setDescription('查看框架的运行状态');
}
protected function execute(InputInterface $input, OutputInterface $output): int protected function execute(InputInterface $input, OutputInterface $output): int
{ {
parent::execute($input, $output); parent::execute($input, $output);

View File

@@ -5,19 +5,18 @@ declare(strict_types=1);
namespace ZM\Command\Server; namespace ZM\Command\Server;
use Swoole\Process; use Swoole\Process;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use ZM\Process\ProcessStateManager; use ZM\Process\ProcessStateManager;
use ZM\Store\FileSystem; use ZM\Store\FileSystem;
#[AsCommand(name: 'server:stop', description: '停止服务器')]
class ServerStopCommand extends ServerCommand class ServerStopCommand extends ServerCommand
{ {
protected static $defaultName = 'server:stop';
protected function configure() protected function configure()
{ {
$this->setDescription('停止运行的框架');
$this->setDefinition([ $this->setDefinition([
new InputOption('force', 'f', InputOption::VALUE_NONE, '强制停止'), new InputOption('force', 'f', InputOption::VALUE_NONE, '强制停止'),
]); ]);