public function handle(InputInterface $input, OutputInterface $output) { $availableTags = BaseOrm::getCheckRegistry()->tagsAvailable(); if ($input->getOption('list-tags')) { $output->writeln(implode(PHP_EOL, $availableTags)); return; } $tags = $input->getOption('tag'); if ($tags) { $invalidTags = []; foreach ($tags as $tag) { if (!BaseOrm::getCheckRegistry()->tagExists($tag)) { $invalidTags[] = $tag; } } if ($invalidTags) { throw new CommandError(sprintf('There is no system check with the "%s" tag(s).', implode(', ', $invalidTags))); } } $failLevel = $input->getOption('fail-level'); if ($failLevel) { if (!in_array(strtoupper($failLevel), ['ERROR', 'WARNING', 'INFO', 'DEBUG', 'CRITICAL'])) { throw new CommandError(sprintf("--fail-level: invalid choice: '%s' " . "(choices are 'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG')", $failLevel)); } } try { $this->check($input, $output, $tags, true, $failLevel); } catch (SystemCheckError $e) { // we get a system check error, stop further processing return; } }
public function check(InputInterface $input, OutputInterface $output, $tags = null, $showErrorCount = null, $failLevel = null) { $checks = BaseOrm::getCheckRegistry()->runChecks($tags); $debugs = []; $info = []; $warning = []; $errors = []; $critical = []; $serious = []; $header = $body = $footer = ''; /** @var $check CheckMessage */ foreach ($checks as $check) { if ($check->isSerious($failLevel) && !$check->isSilenced()) { $serious[] = $check; } if ($check->level < CheckMessage::INFO && !$check->isSilenced()) { $debugs[] = $check; } // info if ($check->level >= CheckMessage::INFO && $check->level < CheckMessage::WARNING && !$check->isSilenced()) { $info[] = $check; } // warning if ($check->level >= CheckMessage::WARNING && $check->level < CheckMessage::ERROR && !$check->isSilenced()) { $warning[] = $check; } //error if ($check->level >= CheckMessage::ERROR && $check->level < CheckMessage::CRITICAL && !$check->isSilenced()) { $errors[] = $check; } //critical if ($check->level >= CheckMessage::CRITICAL && !$check->isSilenced()) { $critical[] = $check; } } // get the count of visible issues only, hide the silenced ones $visibleIssues = count($errors) + count($warning) + count($info) + count($debugs) + count($critical); if ($visibleIssues) { $header = 'System check identified issues: ' . PHP_EOL; } $errors = array_merge($critical, $errors); $categorisedIssues = ['critical' => $critical, 'errors' => $errors, 'warning' => $warning, 'info' => $info, 'debug' => $debugs]; /* @var $catIssue CheckMessage */ foreach ($categorisedIssues as $category => $categoryIssues) { if (empty($categoryIssues)) { continue; } $body .= sprintf(PHP_EOL . ' %s' . PHP_EOL, strtoupper($category)); foreach ($categoryIssues as $catIssue) { if ($catIssue->isSerious()) { $msg = ' <errorText>%s</errorText>' . PHP_EOL; } else { $msg = ' <warning>%s</warning>' . PHP_EOL; } $body .= sprintf($msg, $catIssue); } } if ($showErrorCount) { $issueText = $visibleIssues === 1 ? 'issue' : 'issues'; $silenced = count($checks) - $visibleIssues; if ($visibleIssues) { $footer .= PHP_EOL; } $footer .= sprintf(' System check identified %s %s (%s silenced) ', $visibleIssues, $issueText, $silenced); } if (!empty($serious)) { $header = sprintf('<errorText> SystemCheckError: %s</errorText>', $header); $message = $header . $body . $footer; $output->writeln($message); throw new SystemCheckError(); } $message = $header . $body . $footer; $output->writeln($message); }