addOption('auto-fix', null, null, 'Automatically fix failed items (if possible)'); } public function handle(): int { try { $checker = new CheckListHandler(); $fix_policy = $this->input->getOption('auto-fix') ? FIX_POLICY_AUTOFIX : FIX_POLICY_PROMPT; foreach ($checker->runChecks() as $check) { if ($check->limit_os !== null && $check->limit_os !== PHP_OS_FAMILY) { continue; } $this->output->write('Checking ' . $check->item_name . ' ... '); $result = call_user_func($check->callback); if ($result === null) { $this->output->writeln('skipped'); } elseif ($result instanceof CheckResult) { if ($result->isOK()) { $this->output->writeln($result->getMessage() ?? 'ok'); continue; } // Failed $this->output->writeln('' . $result->getMessage() . ''); switch ($fix_policy) { case FIX_POLICY_DIE: throw new RuntimeException('Some check items can not be fixed !'); case FIX_POLICY_PROMPT: if ($result->getFixItem() !== '') { $helper = new QuestionHelper(); $question = new ConfirmationQuestion('Do you want to fix it? [Y/n] ', true); if ($helper->ask($this->input, $this->output, $question)) { $checker->emitFix($this->output, $result); } else { throw new RuntimeException('You cancelled fix'); } } else { throw new RuntimeException('Some check items can not be fixed !'); } break; case FIX_POLICY_AUTOFIX: if ($result->getFixItem() !== '') { $this->output->writeln('Automatically fixing ' . $result->getFixItem() . ' ...'); $checker->emitFix($this->output, $result); } else { throw new RuntimeException('Some check items can not be fixed !'); } break; } } } $this->output->writeln('Doctor check complete !'); } catch (\Throwable $e) { $this->output->writeln('' . $e->getMessage() . ''); pcntl_signal(SIGINT, SIG_IGN); return static::FAILURE; } return static::SUCCESS; } }