refactor command as more easily

This commit is contained in:
crazywhalecc
2023-04-22 17:45:43 +08:00
parent 1540af266b
commit 4c0d35c723
10 changed files with 150 additions and 106 deletions

View File

@@ -13,6 +13,18 @@ use ZM\Logger\ConsoleLogger;
abstract class BaseCommand extends Command
{
/**
* 输入
*/
protected InputInterface $input;
/**
* 输出
*
* 一般来说同样会是 ConsoleOutputInterface
*/
protected OutputInterface $output;
public function __construct(string $name = null)
{
parent::__construct($name);
@@ -56,4 +68,44 @@ abstract class BaseCommand extends Command
";
}
}
abstract public function handle(): int;
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;
if ($this->shouldExecute()) {
try {
return $this->handle();
} catch (\Throwable $e) {
$msg = explode("\n", $e->getMessage());
foreach ($msg as $v) {
logger()->error($v);
}
return self::FAILURE;
}
}
return self::SUCCESS;
}
protected function getOption(string $name): mixed
{
return $this->input->getOption($name);
}
protected function getArgument(string $name): mixed
{
return $this->input->getArgument($name);
}
/**
* 是否应该执行
*
* @return bool 返回 true 以继续执行,返回 false 以中断执行
*/
protected function shouldExecute(): bool
{
return true;
}
}