CliDescriptor.php 3.0 KB

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