ServerDumpCommand.php 3.8 KB

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