ScanCommand.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\InputArgument;
  7. use Symfony\Component\Console\Input\InputInterface;
  8. use Symfony\Component\Console\Input\InputOption;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  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:scan")
  31. ->setDescription("Scan the typo3 DB")
  32. ->setHelp("This CLI command performs a full scan on the Typo3 db.")
  33. ->addOption(
  34. 'report',
  35. 'r',
  36. InputOption::VALUE_OPTIONAL,
  37. 'Build an html report file with the given name.'
  38. );
  39. }
  40. /**
  41. * -- This method is expected by Typo3, do not rename ou remove --
  42. *
  43. * Executes the command for creating the new organization
  44. *
  45. * @param InputInterface $input
  46. * @param OutputInterface $output
  47. * @throws \Exception
  48. */
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $io = new SymfonyStyle($input, $output);
  52. // instanciate twig loader
  53. $loader = new \Twig\Loader\FilesystemLoader(dirname(__FILE__) . '/../../templates');
  54. $twig = new \Twig\Environment($loader);
  55. // evaluate the report path
  56. $report_path = $input->getOption('report');
  57. if ($report_path == null) {
  58. $report_path = getcwd() . '/scan_report.html';
  59. } else {
  60. $info = pathinfo($report_path);
  61. if ($info['extension'] != 'html') {
  62. $report_path .= '.html';
  63. }
  64. }
  65. // perform the scan
  66. $scanController = GeneralUtility::makeInstance(ObjectManager::class)->get(ScanController::class);
  67. $scan = $scanController->scanAllAction(true);
  68. // render the twig template
  69. $template = $twig->load('scan_report.twig');
  70. $html_report = $template->render(['scan' => $scan]);
  71. $f = fopen($report_path, 'w+');
  72. try {
  73. fwrite($f, $html_report);
  74. $io->success(sprintf("Report file was created at: " . $report_path));
  75. } finally {
  76. fclose($f);
  77. }
  78. }
  79. }