[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/var-dumper/Command/ -> ServerDumpCommand.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   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\Completion\CompletionInput;
  16  use Symfony\Component\Console\Completion\CompletionSuggestions;
  17  use Symfony\Component\Console\Exception\InvalidArgumentException;
  18  use Symfony\Component\Console\Input\InputInterface;
  19  use Symfony\Component\Console\Input\InputOption;
  20  use Symfony\Component\Console\Output\OutputInterface;
  21  use Symfony\Component\Console\Style\SymfonyStyle;
  22  use Symfony\Component\VarDumper\Cloner\Data;
  23  use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
  24  use Symfony\Component\VarDumper\Command\Descriptor\DumpDescriptorInterface;
  25  use Symfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
  26  use Symfony\Component\VarDumper\Dumper\CliDumper;
  27  use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  28  use Symfony\Component\VarDumper\Server\DumpServer;
  29  
  30  /**
  31   * Starts a dump server to collect and output dumps on a single place with multiple formats support.
  32   *
  33   * @author Maxime Steinhausser <[email protected]>
  34   *
  35   * @final
  36   */
  37  class ServerDumpCommand extends Command
  38  {
  39      protected static $defaultName = 'server:dump';
  40      protected static $defaultDescription = 'Start a dump server that collects and displays dumps in a single place';
  41  
  42      private $server;
  43  
  44      /** @var DumpDescriptorInterface[] */
  45      private $descriptors;
  46  
  47      public function __construct(DumpServer $server, array $descriptors = [])
  48      {
  49          $this->server = $server;
  50          $this->descriptors = $descriptors + [
  51              'cli' => new CliDescriptor(new CliDumper()),
  52              'html' => new HtmlDescriptor(new HtmlDumper()),
  53          ];
  54  
  55          parent::__construct();
  56      }
  57  
  58      protected function configure()
  59      {
  60          $this
  61              ->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', implode(', ', $this->getAvailableFormats())), '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  
 103      public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
 104      {
 105          if ($input->mustSuggestOptionValuesFor('format')) {
 106              $suggestions->suggestValues($this->getAvailableFormats());
 107          }
 108      }
 109  
 110      private function getAvailableFormats(): array
 111      {
 112          return array_keys($this->descriptors);
 113      }
 114  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer