CliDescriptor.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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', $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. $rows[] = ['source', sprintf('%s on line %d', $source['name'], $source['line'])];
  56. $file = $source['file_relative'] ?? $source['file'];
  57. $rows[] = ['file', $file];
  58. $fileLink = $source['file_link'] ?? null;
  59. }
  60. $io->table([], $rows);
  61. if (isset($fileLink)) {
  62. $io->writeln(['<info>Open source in your IDE/browser:</info>', $fileLink]);
  63. $io->newLine();
  64. }
  65. $this->dumper->dump($data);
  66. $io->newLine();
  67. }
  68. }