blob: 13dd475b55d45fde21663945534be9de2d9c9526 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Command;
13
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010014use Symfony\Component\Console\Attribute\AsCommand;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020015use Symfony\Component\Console\Command\Command;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010016use Symfony\Component\Console\Completion\CompletionInput;
17use Symfony\Component\Console\Completion\CompletionSuggestions;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020018use Symfony\Component\Console\Exception\InvalidArgumentException;
19use Symfony\Component\Console\Input\InputInterface;
20use Symfony\Component\Console\Input\InputOption;
21use Symfony\Component\Console\Output\OutputInterface;
22use Symfony\Component\Console\Style\SymfonyStyle;
23use Symfony\Component\VarDumper\Cloner\Data;
24use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
25use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
26use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
27use Symfony\Component\VarDumper\Dumper\CliDumper;
28use Symfony\Component\VarDumper\Dumper\HtmlDumper;
29use Symfony\Component\VarDumper\Server\DumpServer;
30
31/**
32 * Starts a dump server to collect and output dumps on a single place with multiple formats support.
33 *
34 * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
35 *
36 * @final
37 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010038#[AsCommand(name: 'server:dump', description: 'Start a dump server that collects and displays dumps in a single place')]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020039class ServerDumpCommand extends Command
40{
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020041 private $server;
42
43 /** @var DumpDescriptorInterface[] */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010044 private array $descriptors;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020045
46 public function __construct(DumpServer $server, array $descriptors = [])
47 {
48 $this->server = $server;
49 $this->descriptors = $descriptors + [
50 'cli' => new CliDescriptor(new CliDumper()),
51 'html' => new HtmlDescriptor(new HtmlDumper()),
52 ];
53
54 parent::__construct();
55 }
56
57 protected function configure()
58 {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020059 $this
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010060 ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), 'cli')
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020061 ->setHelp(<<<'EOF'
62<info>%command.name%</info> starts a dump server that collects and displays
63dumps in a single place for debugging you application:
64
65 <info>php %command.full_name%</info>
66
67You can consult dumped data in HTML format in your browser by providing the <comment>--format=html</comment> option
68and redirecting the output to a file:
69
70 <info>php %command.full_name% --format="html" > dump.html</info>
71
72EOF
73 )
74 ;
75 }
76
77 protected function execute(InputInterface $input, OutputInterface $output): int
78 {
79 $io = new SymfonyStyle($input, $output);
80 $format = $input->getOption('format');
81
82 if (!$descriptor = $this->descriptors[$format] ?? null) {
83 throw new InvalidArgumentException(sprintf('Unsupported format "%s".', $format));
84 }
85
86 $errorIo = $io->getErrorStyle();
87 $errorIo->title('Symfony Var Dumper Server');
88
89 $this->server->start();
90
91 $errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
92 $errorIo->comment('Quit the server with CONTROL-C.');
93
94 $this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
95 $descriptor->describe($io, $data, $context, $clientId);
96 });
97
98 return 0;
99 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100100
101 public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
102 {
103 if ($input->mustSuggestOptionValuesFor('format')) {
104 $suggestions->suggestValues($this->getAvailableFormats());
105 }
106 }
107
108 private function getAvailableFormats(): array
109 {
110 return array_keys($this->descriptors);
111 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200112}