add manual doctors

This commit is contained in:
crazywhalecc
2023-06-28 18:38:14 +08:00
parent fdf36ad726
commit b852471596
5 changed files with 44 additions and 30 deletions

View File

@@ -12,7 +12,8 @@ class AsCheckItem
public function __construct( public function __construct(
public string $item_name, public string $item_name,
public ?string $limit_os = null, public ?string $limit_os = null,
public int $level = 100 public int $level = 100,
public bool $manual = false,
) { ) {
} }
} }

View File

@@ -4,7 +4,7 @@ declare(strict_types=1);
namespace SPC\doctor; namespace SPC\doctor;
#[\Attribute(\Attribute::TARGET_METHOD)] #[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class AsFixItem class AsFixItem
{ {
public function __construct(public string $name) public function __construct(public string $name)

View File

@@ -24,9 +24,20 @@ class CheckListHandler
* @throws FileSystemException * @throws FileSystemException
* @throws RuntimeException * @throws RuntimeException
*/ */
public function __construct(private InputInterface $input, private OutputInterface $output) public function __construct(private InputInterface $input, private OutputInterface $output, bool $include_manual = false)
{ {
$this->loadCheckList(); $this->loadCheckList($include_manual);
}
public function runSingleCheck(string $item_name, int $fix_policy = FIX_POLICY_DIE): void
{
foreach ($this->check_list as $item) {
if ($item->item_name === $item_name) {
$this->check_list = [$item];
break;
}
}
$this->runCheck($fix_policy);
} }
/** /**
@@ -44,7 +55,7 @@ class CheckListHandler
$this->output->writeln('skipped'); $this->output->writeln('skipped');
} elseif ($result instanceof CheckResult) { } elseif ($result instanceof CheckResult) {
if ($result->isOK()) { if ($result->isOK()) {
$this->output->writeln('ok'); $this->output->writeln($result->getMessage() ?? 'ok');
continue; continue;
} }
// Failed // Failed
@@ -85,23 +96,24 @@ class CheckListHandler
* @throws RuntimeException * @throws RuntimeException
* @throws FileSystemException * @throws FileSystemException
*/ */
private function loadCheckList(): void private function loadCheckList(bool $include_manual = false): void
{ {
foreach (FileSystem::getClassesPsr4(__DIR__ . '/item', 'SPC\\doctor\\item') as $class) { foreach (FileSystem::getClassesPsr4(__DIR__ . '/item', 'SPC\\doctor\\item') as $class) {
$ref = new \ReflectionClass($class); $ref = new \ReflectionClass($class);
foreach ($ref->getMethods() as $method) { foreach ($ref->getMethods() as $method) {
$attr = $method->getAttributes(AsCheckItem::class); $attr = $method->getAttributes();
if (isset($attr[0])) { foreach ($attr as $a) {
if (is_a($a->getName(), AsCheckItem::class, true)) {
/** @var AsCheckItem $instance */ /** @var AsCheckItem $instance */
$instance = $attr[0]->newInstance(); $instance = $a->newInstance();
$instance->callback = [new $class(), $method->getName()]; if (!$include_manual && $instance->manual) {
$this->check_list[] = $instance;
continue; continue;
} }
$attr = $method->getAttributes(AsFixItem::class); $instance->callback = [new $class(), $method->getName()];
if (isset($attr[0])) { $this->check_list[] = $instance;
} elseif (is_a($a->getName(), AsFixItem::class, true)) {
/** @var AsFixItem $instance */ /** @var AsFixItem $instance */
$instance = $attr[0]->newInstance(); $instance = $a->newInstance();
// Redundant fix item // Redundant fix item
if (isset($this->fix_map[$instance->name])) { if (isset($this->fix_map[$instance->name])) {
throw new RuntimeException('Redundant doctor fix item: ' . $instance->name); throw new RuntimeException('Redundant doctor fix item: ' . $instance->name);
@@ -110,6 +122,7 @@ class CheckListHandler
} }
} }
} }
}
// sort check list by level // sort check list by level
usort($this->check_list, fn ($a, $b) => $a->level > $b->level ? -1 : ($a->level == $b->level ? 0 : 1)); usort($this->check_list, fn ($a, $b) => $a->level > $b->level ? -1 : ($a->level == $b->level ? 0 : 1));
} }

View File

@@ -6,21 +6,21 @@ namespace SPC\doctor;
class CheckResult class CheckResult
{ {
public function __construct(private string $message = '', private string $fix_item = '', private array $fix_params = []) public function __construct(private bool $ok, private ?string $message = null, private string $fix_item = '', private array $fix_params = [])
{ {
} }
public static function fail(string $message, string $fix_item = '', array $fix_params = []): CheckResult public static function fail(string $message, string $fix_item = '', array $fix_params = []): CheckResult
{ {
return new static($message, $fix_item, $fix_params); return new static(false, $message, $fix_item, $fix_params);
} }
public static function ok(): CheckResult public static function ok(?string $message = null): CheckResult
{ {
return new static(); return new static(true, $message);
} }
public function getMessage(): string public function getMessage(): ?string
{ {
return $this->message; return $this->message;
} }
@@ -37,7 +37,7 @@ class CheckResult
public function isOK(): bool public function isOK(): bool
{ {
return empty($this->message); return $this->ok;
} }
public function setFixItem(string $fix_item = '', array $fix_params = []) public function setFixItem(string $fix_item = '', array $fix_params = [])

View File

@@ -12,12 +12,12 @@ class OSCheckList
{ {
use UnixSystemUtilTrait; use UnixSystemUtilTrait;
#[AsCheckItem('if current os are supported', level: 999)] #[AsCheckItem('if current OS are supported', level: 999)]
public function checkOS(): ?CheckResult public function checkOS(): ?CheckResult
{ {
if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux'])) { if (!in_array(PHP_OS_FAMILY, ['Darwin', 'Linux'])) {
return CheckResult::fail('Current OS is not supported'); return CheckResult::fail('Current OS is not supported');
} }
return CheckResult::ok(); return CheckResult::ok(PHP_OS_FAMILY . ' ' . php_uname('m') . ', supported');
} }
} }