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