ErrorDumpCommand.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ErrorHandler\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
  19. use Symfony\Component\Filesystem\Filesystem;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\HttpKernel\Exception\HttpException;
  22. use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
  23. /**
  24. * Dump error pages to plain HTML files that can be directly served by a web server.
  25. *
  26. * @author Loïck Piera <pyrech@gmail.com>
  27. */
  28. #[AsCommand(
  29. name: 'error:dump',
  30. description: 'Dump error pages to plain HTML files that can be directly served by a web server',
  31. )]
  32. final class ErrorDumpCommand extends Command
  33. {
  34. public function __construct(
  35. private readonly Filesystem $filesystem,
  36. private readonly ErrorRendererInterface $errorRenderer,
  37. private readonly ?EntrypointLookupInterface $entrypointLookup = null,
  38. ) {
  39. parent::__construct();
  40. }
  41. protected function configure(): void
  42. {
  43. $this
  44. ->addArgument('path', InputArgument::REQUIRED, 'Path where to dump the error pages in')
  45. ->addArgument('status-codes', InputArgument::IS_ARRAY, 'Status codes to dump error pages for, all of them by default')
  46. ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force directory removal before dumping new error pages')
  47. ;
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output): int
  50. {
  51. $path = $input->getArgument('path');
  52. $io = new SymfonyStyle($input, $output);
  53. $io->title('Dumping error pages');
  54. $this->dump($io, $path, $input->getArgument('status-codes'), (bool) $input->getOption('force'));
  55. $io->success(\sprintf('Error pages have been dumped in "%s".', $path));
  56. return Command::SUCCESS;
  57. }
  58. private function dump(SymfonyStyle $io, string $path, array $statusCodes, bool $force = false): void
  59. {
  60. if (!$statusCodes) {
  61. $statusCodes = array_filter(array_keys(Response::$statusTexts), fn ($statusCode) => $statusCode >= 400);
  62. }
  63. if ($force || ($this->filesystem->exists($path) && $io->confirm(\sprintf('The "%s" directory already exists. Do you want to remove it before dumping the error pages?', $path), false))) {
  64. $this->filesystem->remove($path);
  65. }
  66. foreach ($statusCodes as $statusCode) {
  67. // Avoid assets to be included only on the first dumped page
  68. $this->entrypointLookup?->reset();
  69. $this->filesystem->dumpFile($path.\DIRECTORY_SEPARATOR.$statusCode.'.html', $this->errorRenderer->render(new HttpException((int) $statusCode))->getAsString());
  70. }
  71. }
  72. }