Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\VarDumper\Command\Descriptor; |
| 13 | |
| 14 | use Symfony\Component\Console\Output\OutputInterface; |
| 15 | use Symfony\Component\VarDumper\Cloner\Data; |
| 16 | use Symfony\Component\VarDumper\Dumper\HtmlDumper; |
| 17 | |
| 18 | /** |
| 19 | * Describe collected data clones for html output. |
| 20 | * |
| 21 | * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> |
| 22 | * |
| 23 | * @final |
| 24 | */ |
| 25 | class HtmlDescriptor implements DumpDescriptorInterface |
| 26 | { |
| 27 | private $dumper; |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 28 | private bool $initialized = false; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 29 | |
| 30 | public function __construct(HtmlDumper $dumper) |
| 31 | { |
| 32 | $this->dumper = $dumper; |
| 33 | } |
| 34 | |
| 35 | public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void |
| 36 | { |
| 37 | if (!$this->initialized) { |
| 38 | $styles = file_get_contents(__DIR__.'/../../Resources/css/htmlDescriptor.css'); |
| 39 | $scripts = file_get_contents(__DIR__.'/../../Resources/js/htmlDescriptor.js'); |
| 40 | $output->writeln("<style>$styles</style><script>$scripts</script>"); |
| 41 | $this->initialized = true; |
| 42 | } |
| 43 | |
| 44 | $title = '-'; |
| 45 | if (isset($context['request'])) { |
| 46 | $request = $context['request']; |
| 47 | $controller = "<span class='dumped-tag'>{$this->dumper->dump($request['controller'], true, ['maxDepth' => 0])}</span>"; |
| 48 | $title = sprintf('<code>%s</code> <a href="%s">%s</a>', $request['method'], $uri = $request['uri'], $uri); |
| 49 | $dedupIdentifier = $request['identifier']; |
| 50 | } elseif (isset($context['cli'])) { |
| 51 | $title = '<code>$ </code>'.$context['cli']['command_line']; |
| 52 | $dedupIdentifier = $context['cli']['identifier']; |
| 53 | } else { |
| 54 | $dedupIdentifier = uniqid('', true); |
| 55 | } |
| 56 | |
| 57 | $sourceDescription = ''; |
| 58 | if (isset($context['source'])) { |
| 59 | $source = $context['source']; |
| 60 | $projectDir = $source['project_dir'] ?? null; |
| 61 | $sourceDescription = sprintf('%s on line %d', $source['name'], $source['line']); |
| 62 | if (isset($source['file_link'])) { |
| 63 | $sourceDescription = sprintf('<a href="%s">%s</a>', $source['file_link'], $sourceDescription); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $isoDate = $this->extractDate($context, 'c'); |
| 68 | $tags = array_filter([ |
| 69 | 'controller' => $controller ?? null, |
| 70 | 'project dir' => $projectDir ?? null, |
| 71 | ]); |
| 72 | |
| 73 | $output->writeln(<<<HTML |
| 74 | <article data-dedup-id="$dedupIdentifier"> |
| 75 | <header> |
| 76 | <div class="row"> |
| 77 | <h2 class="col">$title</h2> |
| 78 | <time class="col text-small" title="$isoDate" datetime="$isoDate"> |
| 79 | {$this->extractDate($context)} |
| 80 | </time> |
| 81 | </div> |
| 82 | {$this->renderTags($tags)} |
| 83 | </header> |
| 84 | <section class="body"> |
| 85 | <p class="text-small"> |
| 86 | $sourceDescription |
| 87 | </p> |
| 88 | {$this->dumper->dump($data, true)} |
| 89 | </section> |
| 90 | </article> |
| 91 | HTML |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | private function extractDate(array $context, string $format = 'r'): string |
| 96 | { |
| 97 | return date($format, (int) $context['timestamp']); |
| 98 | } |
| 99 | |
| 100 | private function renderTags(array $tags): string |
| 101 | { |
| 102 | if (!$tags) { |
| 103 | return ''; |
| 104 | } |
| 105 | |
| 106 | $renderedTags = ''; |
| 107 | foreach ($tags as $key => $value) { |
| 108 | $renderedTags .= sprintf('<li><span class="badge">%s</span>%s</li>', $key, $value); |
| 109 | } |
| 110 | |
| 111 | return <<<HTML |
| 112 | <div class="row"> |
| 113 | <ul class="tags"> |
| 114 | $renderedTags |
| 115 | </ul> |
| 116 | </div> |
| 117 | HTML; |
| 118 | } |
| 119 | } |