mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-25 01:25:34 +08:00
improve Command phpdoc
This commit is contained in:
@@ -6,15 +6,28 @@ namespace ZM\Command;
|
|||||||
|
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||||
|
use Symfony\Component\Console\Output\ConsoleSectionOutput;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use ZM\Exception\ZMException;
|
use ZM\Exception\ZMException;
|
||||||
|
|
||||||
abstract class Command extends \Symfony\Component\Console\Command\Command
|
abstract class Command extends \Symfony\Component\Console\Command\Command
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* 输入
|
||||||
|
*/
|
||||||
protected InputInterface $input;
|
protected InputInterface $input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出
|
||||||
|
*
|
||||||
|
* 一般来说同样会是 ConsoleOutputInterface
|
||||||
|
*/
|
||||||
protected OutputInterface $output;
|
protected OutputInterface $output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
* @internal 不建议覆写此方法,建议使用 {@see handle()} 方法
|
||||||
|
*/
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
{
|
{
|
||||||
$this->input = $input;
|
$this->input = $input;
|
||||||
@@ -22,38 +35,88 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
|
|||||||
return $this->handle();
|
return $this->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 命令的主体
|
||||||
|
*
|
||||||
|
* @return int 命令执行结果 {@see self::SUCCESS} 或 {@see self::FAILURE} 或 {@see self::INVALID}
|
||||||
|
*/
|
||||||
abstract protected function handle(): int;
|
abstract protected function handle(): int;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出一段文本,默认样式
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
* @see OutputInterface::write()
|
||||||
|
*/
|
||||||
protected function write(string $message, bool $newline = true): void
|
protected function write(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->output->write($message, $newline);
|
$this->output->write($message, $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出文本,一般用于提示信息
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
*/
|
||||||
protected function info(string $message, bool $newline = true): void
|
protected function info(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->write("<info>{$message}</info>", $newline);
|
$this->write("<info>{$message}</info>", $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出文本,一般用于错误信息
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
*/
|
||||||
protected function error(string $message, bool $newline = true): void
|
protected function error(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->write("<error>{$message}</error>", $newline);
|
$this->write("<error>{$message}</error>", $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出文本,一般用于警告或附注信息
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
*/
|
||||||
protected function comment(string $message, bool $newline = true): void
|
protected function comment(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->write("<comment>{$message}</comment>", $newline);
|
$this->write("<comment>{$message}</comment>", $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出文本,一般用于提问信息
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
*/
|
||||||
protected function question(string $message, bool $newline = true): void
|
protected function question(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->write("<question>{$message}</question>", $newline);
|
$this->write("<question>{$message}</question>", $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出文本,一般用于详细信息
|
||||||
|
*
|
||||||
|
* @param string $message 要输出的文本
|
||||||
|
* @param bool $newline 是否在文本后换行
|
||||||
|
*/
|
||||||
protected function detail(string $message, bool $newline = true): void
|
protected function detail(string $message, bool $newline = true): void
|
||||||
{
|
{
|
||||||
$this->write("<fg=gray>{$message}</>", $newline);
|
$this->write("<fg=gray>{$message}</>", $newline);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 输出一个区块,区块内内容可以覆写
|
||||||
|
*
|
||||||
|
* 此功能需要 $output 为 {@see ConsoleOutputInterface} 类型
|
||||||
|
*
|
||||||
|
* @param string $message 作为标题的文本
|
||||||
|
* @param callable $callback 回调函数,接收一个参数,类型为 {@see ConsoleSectionOutput}
|
||||||
|
*/
|
||||||
protected function section(string $message, callable $callback): void
|
protected function section(string $message, callable $callback): void
|
||||||
{
|
{
|
||||||
$output = $this->output;
|
$output = $this->output;
|
||||||
@@ -67,7 +130,7 @@ abstract class Command extends \Symfony\Component\Console\Command\Command
|
|||||||
$callback($section);
|
$callback($section);
|
||||||
} catch (ZMException $e) {
|
} catch (ZMException $e) {
|
||||||
$this->error($e->getMessage());
|
$this->error($e->getMessage());
|
||||||
exit(1);
|
exit(self::FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ class InitCommand extends Command
|
|||||||
|
|
||||||
// 将命令行入口标记为可执行
|
// 将命令行入口标记为可执行
|
||||||
chmod($this->base_path . '/zhamao', 0755);
|
chmod($this->base_path . '/zhamao', 0755);
|
||||||
return 0;
|
return self::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getExtractFiles(): array
|
private function getExtractFiles(): array
|
||||||
|
|||||||
Reference in New Issue
Block a user