Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?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 | |
| 12 | namespace Symfony\Component\VarDumper\Command; |
| 13 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 14 | use Symfony\Component\Console\Attribute\AsCommand; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 15 | use Symfony\Component\Console\Command\Command; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 16 | use Symfony\Component\Console\Completion\CompletionInput; |
| 17 | use Symfony\Component\Console\Completion\CompletionSuggestions; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 18 | use Symfony\Component\Console\Exception\InvalidArgumentException; |
| 19 | use Symfony\Component\Console\Input\InputInterface; |
| 20 | use Symfony\Component\Console\Input\InputOption; |
| 21 | use Symfony\Component\Console\Output\OutputInterface; |
| 22 | use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | use Symfony\Component\VarDumper\Cloner\Data; |
| 24 | use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor; |
| 25 | use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface; |
| 26 | use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor; |
| 27 | use Symfony\Component\VarDumper\Dumper\CliDumper; |
| 28 | use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
| 29 | use 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 Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 38 | #[AsCommand(name: 'server:dump', description: 'Start a dump server that collects and displays dumps in a single place')] |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 39 | class ServerDumpCommand extends Command |
| 40 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 41 | private $server; |
| 42 | |
| 43 | /** @var DumpDescriptorInterface[] */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 44 | private array $descriptors; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 45 | |
| 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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | $this |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 60 | ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), 'cli') |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 61 | ->setHelp(<<<'EOF' |
| 62 | <info>%command.name%</info> starts a dump server that collects and displays |
| 63 | dumps in a single place for debugging you application: |
| 64 | |
| 65 | <info>php %command.full_name%</info> |
| 66 | |
| 67 | You can consult dumped data in HTML format in your browser by providing the <comment>--format=html</comment> option |
| 68 | and redirecting the output to a file: |
| 69 | |
| 70 | <info>php %command.full_name% --format="html" > dump.html</info> |
| 71 | |
| 72 | EOF |
| 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 Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 100 | |
| 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 Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 112 | } |