CliDescriptor.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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\Descriptor;
  11. use Symfony\Component\Console\Input\ArrayInput;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Style\SymfonyStyle;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Component\VarDumper\Dumper\CliDumper;
  16. /**
  17. * Describe collected data clones for cli output.
  18. *
  19. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  20. *
  21. * @final
  22. */
  23. class CliDescriptor implements DumpDescriptorInterface
  24. {
  25. private mixed $lastIdentifier = null;
  26. public function __construct(
  27. private CliDumper $dumper,
  28. ) {
  29. }
  30. public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
  31. {
  32. $io = $output instanceof SymfonyStyle ? $output : new SymfonyStyle(new ArrayInput([]), $output);
  33. $this->dumper->setColors($output->isDecorated());
  34. $rows = [['date', date('r', (int) $context['timestamp'])]];
  35. $lastIdentifier = $this->lastIdentifier;
  36. $this->lastIdentifier = $clientId;
  37. $section = "Received from client #$clientId";
  38. if (isset($context['request'])) {
  39. $request = $context['request'];
  40. $this->lastIdentifier = $request['identifier'];
  41. $section = \sprintf('%s %s', $request['method'], $request['uri']);
  42. if ($controller = $request['controller']) {
  43. $rows[] = ['controller', rtrim($this->dumper->dump($controller, true), "\n")];
  44. }
  45. } elseif (isset($context['cli'])) {
  46. $this->lastIdentifier = $context['cli']['identifier'];
  47. $section = '$ '.$context['cli']['command_line'];
  48. }
  49. if ($this->lastIdentifier !== $lastIdentifier) {
  50. $io->section($section);
  51. }
  52. if (isset($context['source'])) {
  53. $source = $context['source'];
  54. $sourceInfo = \sprintf('%s on line %d', $source['name'], $source['line']);
  55. if ($fileLink = $source['file_link'] ?? null) {
  56. $sourceInfo = \sprintf('<href=%s>%s</>', $fileLink, $sourceInfo);
  57. }
  58. $rows[] = ['source', $sourceInfo];
  59. $file = $source['file_relative'] ?? $source['file'];
  60. $rows[] = ['file', $file];
  61. }
  62. $io->table([], $rows);
  63. $this->dumper->dump($data);
  64. $io->newLine();
  65. }
  66. }