ScanCommand.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 TYPO3\CMS\Core\Utility\GeneralUtility;
  10. use TYPO3\CMS\Extbase\Object\ObjectManager;
  11. /**
  12. * This CLI command performs a full scan on the Typo3 db
  13. *
  14. * @package Opentalent\OtAdmin\Command
  15. */
  16. class ScanCommand extends Command
  17. {
  18. /**
  19. * -- This method is expected by Typo3, do not rename ou remove --
  20. *
  21. * Allows to configure the command.
  22. * Allows to add a description, a help text, and / or define arguments.
  23. *
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setName("ot:scan")
  29. ->setDescription("Scan the typo3 DB")
  30. ->setHelp("This CLI command performs a full scan on the Typo3 db.")
  31. ->addOption(
  32. 'report',
  33. 'r',
  34. InputOption::VALUE_OPTIONAL,
  35. 'Build an html report file with the given name.'
  36. );
  37. }
  38. /**
  39. * -- This method is expected by Typo3, do not rename ou remove --
  40. *
  41. * @param InputInterface $input
  42. * @param OutputInterface $output
  43. * @return int
  44. * @throws \Exception
  45. */
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. $io = new SymfonyStyle($input, $output);
  49. // instanciate twig loader
  50. $loader = new \Twig\Loader\FilesystemLoader(dirname(__FILE__) . '/../../templates');
  51. $twig = new \Twig\Environment($loader);
  52. // evaluate the report path
  53. $report_path = $input->getOption('report');
  54. if ($report_path == null) {
  55. $report_path = getcwd() . '/scan_report.html';
  56. } else {
  57. $info = pathinfo($report_path);
  58. if ($info['extension'] != 'html') {
  59. $report_path .= '.html';
  60. }
  61. }
  62. // perform the scan
  63. $scanController = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  64. $scan = $scanController->scanAllAction(true);
  65. // render the twig template
  66. $template = $twig->load('scan_report.twig');
  67. $html_report = $template->render(['scan' => $scan]);
  68. $f = fopen($report_path, 'w+');
  69. try {
  70. fwrite($f, $html_report);
  71. $io->success(sprintf("Report file was created at: " . $report_path));
  72. } finally {
  73. fclose($f);
  74. }
  75. return 0;
  76. }
  77. }