mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-07-09 01:45:36 +08:00
add manual doctors
This commit is contained in:
@@ -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,
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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,28 +96,30 @@ 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) {
|
||||||
/** @var AsCheckItem $instance */
|
if (is_a($a->getName(), AsCheckItem::class, true)) {
|
||||||
$instance = $attr[0]->newInstance();
|
/** @var AsCheckItem $instance */
|
||||||
$instance->callback = [new $class(), $method->getName()];
|
$instance = $a->newInstance();
|
||||||
$this->check_list[] = $instance;
|
if (!$include_manual && $instance->manual) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$attr = $method->getAttributes(AsFixItem::class);
|
$instance->callback = [new $class(), $method->getName()];
|
||||||
if (isset($attr[0])) {
|
$this->check_list[] = $instance;
|
||||||
/** @var AsFixItem $instance */
|
} elseif (is_a($a->getName(), AsFixItem::class, true)) {
|
||||||
$instance = $attr[0]->newInstance();
|
/** @var AsFixItem $instance */
|
||||||
// Redundant fix item
|
$instance = $a->newInstance();
|
||||||
if (isset($this->fix_map[$instance->name])) {
|
// Redundant fix item
|
||||||
throw new RuntimeException('Redundant doctor fix item: ' . $instance->name);
|
if (isset($this->fix_map[$instance->name])) {
|
||||||
|
throw new RuntimeException('Redundant doctor fix item: ' . $instance->name);
|
||||||
|
}
|
||||||
|
$this->fix_map[$instance->name] = [new $class(), $method->getName()];
|
||||||
}
|
}
|
||||||
$this->fix_map[$instance->name] = [new $class(), $method->getName()];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 = [])
|
||||||
|
|||||||
@@ -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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user