You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

2 years ago
  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 $dumper;
  26. private $lastIdentifier;
  27. public function __construct(CliDumper $dumper)
  28. {
  29. $this->dumper = $dumper;
  30. }
  31. public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
  32. {
  33. $io = $output instanceof SymfonyStyle ? $output : new SymfonyStyle(new ArrayInput([]), $output);
  34. $this->dumper->setColors($output->isDecorated());
  35. $rows = [['date', date('r', (int) $context['timestamp'])]];
  36. $lastIdentifier = $this->lastIdentifier;
  37. $this->lastIdentifier = $clientId;
  38. $section = "Received from client #$clientId";
  39. if (isset($context['request'])) {
  40. $request = $context['request'];
  41. $this->lastIdentifier = $request['identifier'];
  42. $section = sprintf('%s %s', $request['method'], $request['uri']);
  43. if ($controller = $request['controller']) {
  44. $rows[] = ['controller', rtrim($this->dumper->dump($controller, true), "\n")];
  45. }
  46. } elseif (isset($context['cli'])) {
  47. $this->lastIdentifier = $context['cli']['identifier'];
  48. $section = '$ '.$context['cli']['command_line'];
  49. }
  50. if ($this->lastIdentifier !== $lastIdentifier) {
  51. $io->section($section);
  52. }
  53. if (isset($context['source'])) {
  54. $source = $context['source'];
  55. $sourceInfo = sprintf('%s on line %d', $source['name'], $source['line']);
  56. if ($fileLink = $source['file_link'] ?? null) {
  57. $sourceInfo = sprintf('<href=%s>%s</>', $fileLink, $sourceInfo);
  58. }
  59. $rows[] = ['source', $sourceInfo];
  60. $file = $source['file_relative'] ?? $source['file'];
  61. $rows[] = ['file', $file];
  62. }
  63. $io->table([], $rows);
  64. $this->dumper->dump($data);
  65. $io->newLine();
  66. }
  67. }