CheckCommand.php 3.1 KB

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