mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-23 08:35:35 +08:00
refactor InitCommand
This commit is contained in:
73
src/ZM/Command/Command.php
Normal file
73
src/ZM/Command/Command.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ZM\Command;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use ZM\Exception\ZMException;
|
||||
|
||||
abstract class Command extends \Symfony\Component\Console\Command\Command
|
||||
{
|
||||
protected InputInterface $input;
|
||||
|
||||
protected OutputInterface $output;
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$this->input = $input;
|
||||
$this->output = $output;
|
||||
return $this->handle();
|
||||
}
|
||||
|
||||
abstract protected function handle(): int;
|
||||
|
||||
protected function write(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->output->write($message, $newline);
|
||||
}
|
||||
|
||||
protected function info(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->write("<info>{$message}</info>", $newline);
|
||||
}
|
||||
|
||||
protected function error(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->write("<error>{$message}</error>", $newline);
|
||||
}
|
||||
|
||||
protected function comment(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->write("<comment>{$message}</comment>", $newline);
|
||||
}
|
||||
|
||||
protected function question(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->write("<question>{$message}</question>", $newline);
|
||||
}
|
||||
|
||||
protected function detail(string $message, bool $newline = true): void
|
||||
{
|
||||
$this->write("<fg=gray>{$message}</>", $newline);
|
||||
}
|
||||
|
||||
protected function section(string $message, callable $callback): void
|
||||
{
|
||||
$output = $this->output;
|
||||
if (!$output instanceof ConsoleOutputInterface) {
|
||||
throw new \LogicException('Section 功能只能在 ConsoleOutputInterface 中使用');
|
||||
}
|
||||
|
||||
$this->info($message);
|
||||
$section = $output->section();
|
||||
try {
|
||||
$callback($section);
|
||||
} catch (ZMException $e) {
|
||||
$this->error($e->getMessage());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user