ServerDumpCommand.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. private $server;
  35. /** @var DumpDescriptorInterface[] */
  36. private $descriptors;
  37. public function __construct(DumpServer $server, array $descriptors = [])
  38. {
  39. $this->server = $server;
  40. $this->descriptors = $descriptors + [
  41. 'cli' => new CliDescriptor(new CliDumper()),
  42. 'html' => new HtmlDescriptor(new HtmlDumper()),
  43. ];
  44. parent::__construct();
  45. }
  46. protected function configure()
  47. {
  48. $availableFormats = implode(', ', array_keys($this->descriptors));
  49. $this
  50. ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
  51. ->setDescription('Starts a dump server that collects and displays dumps in a single place')
  52. ->setHelp(<<<'EOF'
  53. <info>%command.name%</info> starts a dump server that collects and displays
  54. dumps in a single place for debugging you application:
  55. <info>php %command.full_name%</info>
  56. You can consult dumped data in HTML format in your browser by providing the <comment>--format=html</comment> option
  57. and redirecting the output to a file:
  58. <info>php %command.full_name% --format="html" > dump.html</info>
  59. EOF
  60. )
  61. ;
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int
  64. {
  65. $io = new SymfonyStyle($input, $output);
  66. $format = $input->getOption('format');
  67. if (!$descriptor = $this->descriptors[$format] ?? null) {
  68. throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
  69. }
  70. $errorIo = $io->getErrorStyle();
  71. $errorIo->title('Symfony Var Dumper Server');
  72. $this->server->start();
  73. $errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
  74. $errorIo->comment('Quit the server with CONTROL-C.');
  75. $this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
  76. $descriptor->describe($io, $data, $context, $clientId);
  77. });
  78. }
  79. }