You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.4 KiB

3 years ago
  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\VarDumper\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Exception\InvalidArgumentException;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\VarDumper\Cloner\Data;
  18. use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
  19. use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
  20. use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
  21. use Symfony\Component\VarDumper\Dumper\CliDumper;
  22. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  23. use Symfony\Component\VarDumper\Server\DumpServer;
  24. /**
  25. * Starts a dump server to collect and output dumps on a single place with multiple formats support.
  26. *
  27. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  28. *
  29. * @final
  30. */
  31. class ServerDumpCommand extends Command
  32. {
  33. protected static $defaultName = 'server:dump';
  34. protected static $defaultDescription = 'Start a dump server that collects and displays dumps in a single place';
  35. private $server;
  36. /** @var DumpDescriptorInterface[] */
  37. private $descriptors;
  38. public function __construct(DumpServer $server, array $descriptors = [])
  39. {
  40. $this->server = $server;
  41. $this->descriptors = $descriptors + [
  42. 'cli' => new CliDescriptor(new CliDumper()),
  43. 'html' => new HtmlDescriptor(new HtmlDumper()),
  44. ];
  45. parent::__construct();
  46. }
  47. protected function configure()
  48. {
  49. $availableFormats = implode(', ', array_keys($this->descriptors));
  50. $this
  51. ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
  52. ->setDescription(self::$defaultDescription)
  53. ->setHelp(<<<'EOF'
  54. <info>%command.name%</info> starts a dump server that collects and displays
  55. dumps in a single place for debugging you application:
  56. <info>php %command.full_name%</info>
  57. You can consult dumped data in HTML format in your browser by providing the <comment>--format=html</comment> option
  58. and redirecting the output to a file:
  59. <info>php %command.full_name% --format="html" > dump.html</info>
  60. EOF
  61. )
  62. ;
  63. }
  64. protected function execute(InputInterface $input, OutputInterface $output): int
  65. {
  66. $io = new SymfonyStyle($input, $output);
  67. $format = $input->getOption('format');
  68. if (!$descriptor = $this->descriptors[$format] ?? null) {
  69. throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
  70. }
  71. $errorIo = $io->getErrorStyle();
  72. $errorIo->title('Symfony Var Dumper Server');
  73. $this->server->start();
  74. $errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
  75. $errorIo->comment('Quit the server with CONTROL-C.');
  76. $this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
  77. $descriptor->describe($io, $data, $context, $clientId);
  78. });
  79. return 0;
  80. }
  81. }