SetupChecks.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command;
  8. use OCP\RichObjectStrings\IRichTextFormatter;
  9. use OCP\SetupCheck\ISetupCheckManager;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Output\OutputInterface;
  12. class SetupChecks extends Base {
  13. public function __construct(
  14. private ISetupCheckManager $setupCheckManager,
  15. private IRichTextFormatter $richTextFormatter,
  16. ) {
  17. parent::__construct();
  18. }
  19. protected function configure(): void {
  20. parent::configure();
  21. $this
  22. ->setName('setupchecks')
  23. ->setDescription('Run setup checks and output the results')
  24. ;
  25. }
  26. protected function execute(InputInterface $input, OutputInterface $output): int {
  27. $results = $this->setupCheckManager->runAll();
  28. switch ($input->getOption('output')) {
  29. case self::OUTPUT_FORMAT_JSON:
  30. case self::OUTPUT_FORMAT_JSON_PRETTY:
  31. $this->writeArrayInOutputFormat($input, $output, $results);
  32. break;
  33. default:
  34. foreach ($results as $category => $checks) {
  35. $output->writeln("\t{$category}:");
  36. foreach ($checks as $check) {
  37. $styleTag = match ($check->getSeverity()) {
  38. 'success' => 'info',
  39. 'error' => 'error',
  40. 'warning' => 'comment',
  41. default => null,
  42. };
  43. $emoji = match ($check->getSeverity()) {
  44. 'success' => '✓',
  45. 'error' => '✗',
  46. 'warning' => '⚠',
  47. default => 'ℹ',
  48. };
  49. $verbosity = ($check->getSeverity() === 'error' ? OutputInterface::VERBOSITY_QUIET : OutputInterface::VERBOSITY_NORMAL);
  50. $description = $check->getDescription();
  51. $descriptionParameters = $check->getDescriptionParameters();
  52. if ($description !== null && $descriptionParameters !== null) {
  53. $description = $this->richTextFormatter->richToParsed($description, $descriptionParameters);
  54. }
  55. $output->writeln(
  56. "\t\t" .
  57. ($styleTag !== null ? "<{$styleTag}>" : '') .
  58. "{$emoji} " .
  59. ($check->getName() ?? $check::class) .
  60. ($description !== null ? ': ' . $description : '') .
  61. ($styleTag !== null ? "</{$styleTag}>" : ''),
  62. $verbosity
  63. );
  64. }
  65. }
  66. }
  67. foreach ($results as $category => $checks) {
  68. foreach ($checks as $check) {
  69. if ($check->getSeverity() !== 'success') {
  70. return self::FAILURE;
  71. }
  72. }
  73. }
  74. return self::SUCCESS;
  75. }
  76. }