CheckCommand.php 3.1 KB

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