CheckCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Grav\Plugin\Console;
  3. use Grav\Console\ConsoleCommand;
  4. use Grav\Plugin\Problems\Base\Problem;
  5. use Grav\Plugin\Problems\Base\ProblemChecker;
  6. use Symfony\Component\Console\Helper\Table;
  7. use Symfony\Component\Console\Helper\TableCell;
  8. use Symfony\Component\Console\Helper\TableSeparator;
  9. use Symfony\Component\Console\Style\SymfonyStyle;
  10. /**
  11. * Class ProblemsCommand
  12. *
  13. * @package Grav\Plugin\Console
  14. */
  15. class CheckCommand extends ConsoleCommand
  16. {
  17. /**
  18. * @return void
  19. */
  20. protected function configure(): void
  21. {
  22. $this
  23. ->setName('check')
  24. ->setDescription('Check Problems')
  25. ->setHelp('The <info>problems command</info> allows you display any potential problems with your Grav setup')
  26. ;
  27. }
  28. /**
  29. * @return int
  30. */
  31. protected function serve(): int
  32. {
  33. $io = new SymfonyStyle($this->input, $this->output);
  34. $plugin_dir = realpath(dirname(__DIR__));
  35. $problems_dir = $plugin_dir . '/classes/Problems';
  36. require $plugin_dir . '/vendor/autoload.php';
  37. $checker = new ProblemChecker();
  38. $checker->check($problems_dir);
  39. $problems = $checker->getProblems();
  40. $io->title('Grav Problems');
  41. $table = new Table($this->output);
  42. $table->setStyle('default');
  43. $headers = ['ID', 'Status', 'Level', 'Message'];
  44. $rows = [];
  45. /** @var Problem $problem */
  46. foreach ($problems as $problem) {
  47. $rows[] = new TableSeparator();
  48. $rows[] = [
  49. $problem->getStatus() ? $problem->getId() : '<red>' . $problem->getId() . '</red>' ,
  50. $problem->getStatus() ? '<green>success</green>' : '<red>error</red>',
  51. $problem->getLevel() === 'critical' ? '<red>' . $problem->getLevel() . '</red>' : '<yellow>' .$problem->getLevel() . '</yellow>',
  52. strip_tags($problem->getMsg()),
  53. ];
  54. $details = $problem->getDetails();
  55. if (is_array($details)) {
  56. $errors_row = [];
  57. $success_row = [];
  58. if (isset($details['errors'])) {
  59. foreach ($details['errors'] as $key => $value) {
  60. $errors_row[] = "<red>✗</red> <yellow>{$key} → {$value}</yellow>";
  61. }
  62. }
  63. if (isset($details['success'])) {
  64. foreach ($details['success'] as $key => $value) {
  65. $success_row[] = "<green>✔</green> {$key} → {$value}";
  66. }
  67. }
  68. foreach($errors_row as $e_row) {
  69. $rows[] = ['', new TableCell($e_row, array('colspan' => 3)), ];
  70. }
  71. foreach($success_row as $e_row) {
  72. $rows[] = ['', new TableCell($e_row, array('colspan' => 3)), ];
  73. }
  74. }
  75. }
  76. if (!empty($rows)) {
  77. $table->setHeaders($headers);
  78. $table->setRows($rows);
  79. $table->render();
  80. } else {
  81. $io->text('did not find anything to check...');
  82. }
  83. return 0;
  84. }
  85. }