ScanCommand.php 3.3 KB

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