15
15
use Symfony \Component \HttpFoundation \Response ;
16
16
use Symfony \Component \Stopwatch \Stopwatch ;
17
17
use Symfony \Component \VarDumper \Cloner \Data ;
18
+ use Symfony \Component \VarDumper \Cloner \VarCloner ;
18
19
use Symfony \Component \VarDumper \Dumper \CliDumper ;
19
20
use Symfony \Component \VarDumper \Dumper \HtmlDumper ;
20
21
use Symfony \Component \VarDumper \Dumper \DataDumperInterface ;
@@ -31,11 +32,13 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
31
32
private $ clonesCount = 0 ;
32
33
private $ clonesIndex = 0 ;
33
34
private $ rootRefs ;
35
+ private $ charset ;
34
36
35
- public function __construct (Stopwatch $ stopwatch = null , $ fileLinkFormat = null )
37
+ public function __construct (Stopwatch $ stopwatch = null , $ fileLinkFormat = null , $ charset = null )
36
38
{
37
39
$ this ->stopwatch = $ stopwatch ;
38
40
$ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
41
+ $ this ->charset = $ charset ?: ini_get ('php.output_encoding ' ) ?: ini_get ('default_charset ' ) ?: 'UTF-8 ' ;
39
42
40
43
// All clones share these properties by reference:
41
44
$ this ->rootRefs = array (
@@ -98,7 +101,7 @@ public function dump(Data $data)
98
101
$ fileExcerpt = array ();
99
102
100
103
for ($ i = max ($ line - 3 , 1 ), $ max = min ($ line + 3 , count ($ src )); $ i <= $ max ; ++$ i ) {
101
- $ fileExcerpt [] = '<li ' .($ i === $ line ? ' class="selected" ' : '' ).'><code> ' .htmlspecialchars ($ src [$ i - 1 ]).'</code></li> ' ;
104
+ $ fileExcerpt [] = '<li ' .($ i === $ line ? ' class="selected" ' : '' ).'><code> ' .$ this -> htmlEncode ($ src [$ i - 1 ]).'</code></li> ' ;
102
105
}
103
106
104
107
$ fileExcerpt = '<ol start=" ' .max ($ line - 3 , 1 ).'"> ' .implode ("\n" , $ fileExcerpt ).'</ol> ' ;
@@ -158,7 +161,7 @@ public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
158
161
$ data = fopen ('php://memory ' , 'r+b ' );
159
162
160
163
if ('html ' === $ format ) {
161
- $ dumper = new HtmlDumper ($ data );
164
+ $ dumper = new HtmlDumper ($ data, $ this -> charset );
162
165
} else {
163
166
throw new \InvalidArgumentException (sprintf ('Invalid dump format: %s ' , $ format ));
164
167
}
@@ -195,19 +198,18 @@ public function __destruct()
195
198
}
196
199
197
200
if ('cli ' !== PHP_SAPI && stripos ($ h [$ i ], 'html ' )) {
198
- echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ' ;
199
- $ dumper = new HtmlDumper ('php://output ' );
201
+ $ dumper = new HtmlDumper ('php://output ' , $ this ->charset );
200
202
} else {
201
- $ dumper = new CliDumper ('php://output ' );
203
+ $ dumper = new CliDumper ('php://output ' , $ this -> charset );
202
204
$ dumper ->setColors (false );
203
205
}
204
206
205
207
foreach ($ this ->data as $ i => $ dump ) {
206
208
$ this ->data [$ i ] = null ;
207
209
208
210
if ($ dumper instanceof HtmlDumper) {
209
- $ dump ['name ' ] = htmlspecialchars ($ dump ['name ' ], ENT_QUOTES , ' UTF-8 ' );
210
- $ dump ['file ' ] = htmlspecialchars ($ dump ['file ' ], ENT_QUOTES , ' UTF-8 ' );
211
+ $ dump ['name ' ] = $ this -> htmlEncode ($ dump ['name ' ]);
212
+ $ dump ['file ' ] = $ this -> htmlEncode ($ dump ['file ' ]);
211
213
if ('' !== $ dump ['file ' ]) {
212
214
if ($ this ->fileLinkFormat ) {
213
215
$ link = strtr ($ this ->fileLinkFormat , array ('%f ' => $ dump ['file ' ], '%l ' => $ dump ['line ' ]));
@@ -227,4 +229,18 @@ public function __destruct()
227
229
$ this ->dataCount = 0 ;
228
230
}
229
231
}
232
+
233
+ private function htmlEncode ($ s )
234
+ {
235
+ $ html = '' ;
236
+
237
+ $ dumper = new HtmlDumper (function ($ line ) use (&$ html ) {$ html .= $ line ;}, $ this ->charset );
238
+ $ dumper ->setDumpHeader ('' );
239
+ $ dumper ->setDumpBoundaries ('' , '' );
240
+
241
+ $ cloner = new VarCloner ();
242
+ $ dumper ->dump ($ cloner ->cloneVar ($ s ));
243
+
244
+ return substr (strip_tags ($ html ), 1 , -1 );
245
+ }
230
246
}
0 commit comments