HtmlDescriptor.php 3.6 KB

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