|
| 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\Dumper; |
| 13 | + |
| 14 | +/** |
| 15 | + * HtmlDumper dumps variables as HTML. |
| 16 | + * |
| 17 | + * @author Nicolas Grekas <p@tchwork.com> |
| 18 | + */ |
| 19 | +class HtmlDumper extends CliDumper |
| 20 | +{ |
| 21 | + public static $defaultOutputStream = 'php://output'; |
| 22 | + |
| 23 | + protected $dumpHeader; |
| 24 | + protected $dumpPrefix = '<pre class=sf-var-debug style=white-space:pre>'; |
| 25 | + protected $dumpSuffix = '</pre>'; |
| 26 | + protected $colors = true; |
| 27 | + protected $headerIsDumped = false; |
| 28 | + protected $lastDepth = -1; |
| 29 | + protected $styles = array( |
| 30 | + 'num' => 'font-weight:bold;color:#0087FF', |
| 31 | + 'const' => 'font-weight:bold;color:#0087FF', |
| 32 | + 'str' => 'font-weight:bold;color:#00D7FF', |
| 33 | + 'cchr' => 'font-style: italic', |
| 34 | + 'note' => 'color:#D7AF00', |
| 35 | + 'ref' => 'color:#444444', |
| 36 | + 'public' => 'color:#008700', |
| 37 | + 'protected' => 'color:#D75F00', |
| 38 | + 'private' => 'color:#D70000', |
| 39 | + 'meta' => 'color:#005FFF', |
| 40 | + ); |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function setLineDumper($callback) |
| 46 | + { |
| 47 | + $this->headerIsDumped = false; |
| 48 | + |
| 49 | + return parent::setLineDumper($callback); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + public function setStyles(array $styles) |
| 56 | + { |
| 57 | + $this->headerIsDumped = false; |
| 58 | + $this->styles = $styles + $this->styles; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Sets an HTML header the will be dumped once in the output stream. |
| 63 | + * |
| 64 | + * @param string $header An HTML string. |
| 65 | + */ |
| 66 | + public function setDumpHeader($header) |
| 67 | + { |
| 68 | + $this->dumpHeader = $header; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Sets an HTML prefix and suffix that will encapse every single dump. |
| 73 | + * |
| 74 | + * @param string $prefix The prepended HTML string. |
| 75 | + * @param string $suffix The appended HTML string. |
| 76 | + */ |
| 77 | + public function setDumpBoudaries($prefix, $suffix) |
| 78 | + { |
| 79 | + $this->dumpPrefix = $prefix; |
| 80 | + $this->dumpSuffix = $suffix; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Dumps the HTML header. |
| 85 | + */ |
| 86 | + protected function dumpHeader() |
| 87 | + { |
| 88 | + $this->headerIsDumped = true; |
| 89 | + $line = $this->line; |
| 90 | + |
| 91 | + $p = 'sf-var-debug'; |
| 92 | + $this->line = '<!DOCTYPE html><style>'; |
| 93 | + parent::dumpLine(0); |
| 94 | + $this->line .= "a.$p-ref {{$this->styles['ref']}}"; |
| 95 | + parent::dumpLine(0); |
| 96 | + |
| 97 | + foreach ($this->styles as $class => $style) { |
| 98 | + $this->line .= "span.$p-$class {{$style}}"; |
| 99 | + parent::dumpLine(0); |
| 100 | + } |
| 101 | + |
| 102 | + $this->line .= '</style>'; |
| 103 | + parent::dumpLine(0); |
| 104 | + $this->line .= $this->dumpHeader; |
| 105 | + parent::dumpLine(0); |
| 106 | + |
| 107 | + $this->line = $line; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * {@inheritdoc} |
| 112 | + */ |
| 113 | + protected function style($style, $val) |
| 114 | + { |
| 115 | + if ('' === $val) { |
| 116 | + return ''; |
| 117 | + } |
| 118 | + |
| 119 | + if ('ref' === $style) { |
| 120 | + $ref = substr($val, 1); |
| 121 | + if ('#' === $val[0]) { |
| 122 | + return "<a class=sf-var-debug-ref name=\"sf-var-debug-ref$ref\">$val</a>"; |
| 123 | + } else { |
| 124 | + return "<a class=sf-var-debug-ref href=\"#sf-var-debug-ref$ref\">$val</a>"; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $val = htmlspecialchars($val, ENT_QUOTES, 'UTF-8'); |
| 129 | + |
| 130 | + if ('str' === $style || 'meta' === $style || 'public' === $style) { |
| 131 | + foreach (static::$controlChars as $c) { |
| 132 | + if (false !== strpos($val, $c)) { |
| 133 | + $r = "\x7F" === $c ? '?' : chr(64 + ord($c)); |
| 134 | + $val = str_replace($c, "<span class=sf-var-debug-cchr>$r</span>", $val); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return "<span class=sf-var-debug-$style>$val</span>"; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * {@inheritdoc} |
| 144 | + */ |
| 145 | + protected function dumpLine($depth) |
| 146 | + { |
| 147 | + if (!$this->headerIsDumped) { |
| 148 | + $this->dumpHeader(); |
| 149 | + } |
| 150 | + |
| 151 | + switch ($this->lastDepth - $depth) { |
| 152 | + case +1: $this->line = '</span>'.$this->line; break; |
| 153 | + case -1: $this->line = "<span class=sf-var-debug-$depth>$this->line"; break; |
| 154 | + } |
| 155 | + |
| 156 | + if (-1 === $this->lastDepth) { |
| 157 | + $this->line = $this->dumpPrefix.$this->line; |
| 158 | + } |
| 159 | + |
| 160 | + if (false === $depth) { |
| 161 | + $this->lastDepth = -1; |
| 162 | + $this->line .= $this->dumpSuffix; |
| 163 | + parent::dumpLine(0); |
| 164 | + } else { |
| 165 | + $this->lastDepth = $depth; |
| 166 | + } |
| 167 | + |
| 168 | + parent::dumpLine($depth); |
| 169 | + } |
| 170 | +} |
0 commit comments