HtmlDescriptor.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\Output\OutputInterface;
  12. use Symfony\Component\VarDumper\Cloner\Data;
  13. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  14. /**
  15. * Describe collected data clones for html output.
  16. *
  17. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  18. *
  19. * @final
  20. */
  21. class HtmlDescriptor implements DumpDescriptorInterface
  22. {
  23. private bool $initialized = false;
  24. public function __construct(
  25. private HtmlDumper $dumper,
  26. ) {
  27. }
  28. public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
  29. {
  30. if (!$this->initialized) {
  31. $styles = file_get_contents(__DIR__.'/../../Resources/css/htmlDescriptor.css');
  32. $scripts = file_get_contents(__DIR__.'/../../Resources/js/htmlDescriptor.js');
  33. $output->writeln("<style>$styles</style><script>$scripts</script>");
  34. $this->initialized = true;
  35. }
  36. $title = '-';
  37. if (isset($context['request'])) {
  38. $request = $context['request'];
  39. $controller = "<span class='dumped-tag'>{$this->dumper->dump($request['controller'], true, ['maxDepth' => 0])}</span>";
  40. $title = \sprintf('<code>%s</code> <a href="%s">%s</a>', $request['method'], $uri = $request['uri'], $uri);
  41. $dedupIdentifier = $request['identifier'];
  42. } elseif (isset($context['cli'])) {
  43. $title = '<code>$ </code>'.$context['cli']['command_line'];
  44. $dedupIdentifier = $context['cli']['identifier'];
  45. } else {
  46. $dedupIdentifier = bin2hex(random_bytes(4));
  47. }
  48. $sourceDescription = '';
  49. if (isset($context['source'])) {
  50. $source = $context['source'];
  51. $projectDir = $source['project_dir'] ?? null;
  52. $sourceDescription = \sprintf('%s on line %d', $source['name'], $source['line']);
  53. if (isset($source['file_link'])) {
  54. $sourceDescription = \sprintf('<a href="%s">%s</a>', $source['file_link'], $sourceDescription);
  55. }
  56. }
  57. $isoDate = $this->extractDate($context, 'c');
  58. $tags = array_filter([
  59. 'controller' => $controller ?? null,
  60. 'project dir' => $projectDir ?? null,
  61. ]);
  62. $output->writeln(<<<HTML
  63. <article data-dedup-id="$dedupIdentifier">
  64. <header>
  65. <div class="row">
  66. <h2 class="col">$title</h2>
  67. <time class="col text-small" title="$isoDate" datetime="$isoDate">
  68. {$this->extractDate($context)}
  69. </time>
  70. </div>
  71. {$this->renderTags($tags)}
  72. </header>
  73. <section class="body">
  74. <p class="text-small">
  75. $sourceDescription
  76. </p>
  77. {$this->dumper->dump($data, true)}
  78. </section>
  79. </article>
  80. HTML
  81. );
  82. }
  83. private function extractDate(array $context, string $format = 'r'): string
  84. {
  85. return date($format, (int) $context['timestamp']);
  86. }
  87. private function renderTags(array $tags): string
  88. {
  89. if (!$tags) {
  90. return '';
  91. }
  92. $renderedTags = '';
  93. foreach ($tags as $key => $value) {
  94. $renderedTags .= \sprintf('<li><span class="badge">%s</span>%s</li>', $key, $value);
  95. }
  96. return <<<HTML
  97. <div class="row">
  98. <ul class="tags">
  99. $renderedTags
  100. </ul>
  101. </div>
  102. HTML;
  103. }
  104. }