ScanCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Opentalent\OtAdmin\Command;
  3. use Opentalent\OtAdmin\Controller\ScanController;
  4. use Symfony\Component\Console\Command\Command;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. use Twig\Environment;
  10. use Twig\Loader\FilesystemLoader;
  11. use TYPO3\CMS\Core\Utility\GeneralUtility;
  12. use TYPO3\CMS\Extbase\Object\ObjectManager;
  13. /**
  14. * This CLI command performs a full scan on the Typo3 db
  15. *
  16. * @package Opentalent\OtAdmin\Command
  17. */
  18. class ScanCommand extends Command
  19. {
  20. /**
  21. * -- This method is expected by Typo3, do not rename ou remove --
  22. *
  23. * Allows to configure the command.
  24. * Allows to add a description, a help text, and / or define arguments.
  25. *
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setName("ot:site:scan")
  31. ->setDescription("Scan the typo3 DB")
  32. ->setHelp("This CLI command performs a full scan on the Typo3 db.")
  33. ->addOption(
  34. 'deep',
  35. 'd',
  36. InputOption::VALUE_NONE,
  37. 'Performs a deeper scan, with more control operations'
  38. )->addOption(
  39. 'report',
  40. 'r',
  41. InputOption::VALUE_OPTIONAL,
  42. 'Build an html report file with the given name.'
  43. );
  44. }
  45. /**
  46. * -- This method is expected by Typo3, do not rename ou remove --
  47. *
  48. * @param InputInterface $input
  49. * @param OutputInterface $output
  50. * @return int
  51. * @throws \Exception
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. $deep = $input->getOption('deep');
  56. $io = new SymfonyStyle($input, $output);
  57. // instanciate twig loader
  58. $loader = new FilesystemLoader(__DIR__ . '/../../templates');
  59. $twig = new Environment($loader);
  60. // evaluate the report path
  61. $report_path = $input->getOption('report');
  62. if ($report_path == null) {
  63. $report_path = getcwd() . '/scan_report.html';
  64. } else {
  65. $info = pathinfo($report_path);
  66. if ($info['extension'] !== 'html') {
  67. $report_path .= '.html';
  68. }
  69. }
  70. // perform the scan
  71. $scanController = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  72. $progressBar = $io->createProgressBar();
  73. $progressBar->display();
  74. $scan = $scanController->scanAllAction(
  75. $deep,
  76. function ($i, $total) use ($progressBar) {
  77. $progressBar->setProgress($i);
  78. if ($total !== $progressBar->getMaxSteps()) {
  79. $progressBar->setMaxSteps($total);
  80. }
  81. }
  82. );
  83. // render the twig template
  84. $template = $twig->load('scan_report.twig');
  85. $html_report = $template->render(['scan' => $scan]);
  86. $f = fopen($report_path, 'wb+');
  87. try {
  88. fwrite($f, $html_report);
  89. $io->success(sprintf("Report file was created at: " . $report_path));
  90. } finally {
  91. fclose($f);
  92. }
  93. return 0;
  94. }
  95. }