18
18
use Symfony \Component \VarDumper \Cloner \Data ;
19
19
use Symfony \Component \VarDumper \Cloner \VarCloner ;
20
20
use Symfony \Component \VarDumper \Dumper \CliDumper ;
21
+ use Symfony \Component \VarDumper \Dumper \ContextProvider \SourceContextProvider ;
21
22
use Symfony \Component \VarDumper \Dumper \HtmlDumper ;
22
23
use Symfony \Component \VarDumper \Dumper \DataDumperInterface ;
23
- use Twig \ Template ;
24
+ use Symfony \ Component \ VarDumper \ Dumper \ ServerDumper ;
24
25
25
26
/**
26
27
* @author Nicolas Grekas <p@tchwork.com>
@@ -38,15 +39,18 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
38
39
private $ requestStack ;
39
40
private $ dumper ;
40
41
private $ dumperIsInjected ;
42
+ private $ serverDumper ;
43
+ private $ sourceContextProvider ;
41
44
42
- public function __construct (Stopwatch $ stopwatch = null , $ fileLinkFormat = null , string $ charset = null , RequestStack $ requestStack = null , DataDumperInterface $ dumper = null )
45
+ public function __construct (Stopwatch $ stopwatch = null , $ fileLinkFormat = null , string $ charset = null , RequestStack $ requestStack = null , DataDumperInterface $ dumper = null , ServerDumper $ serverDumper = null )
43
46
{
44
47
$ this ->stopwatch = $ stopwatch ;
45
48
$ this ->fileLinkFormat = $ fileLinkFormat ?: ini_get ('xdebug.file_link_format ' ) ?: get_cfg_var ('xdebug.file_link_format ' );
46
49
$ this ->charset = $ charset ?: ini_get ('php.output_encoding ' ) ?: ini_get ('default_charset ' ) ?: 'UTF-8 ' ;
47
50
$ this ->requestStack = $ requestStack ;
48
51
$ this ->dumper = $ dumper ;
49
52
$ this ->dumperIsInjected = null !== $ dumper ;
53
+ $ this ->serverDumper = $ serverDumper ;
50
54
51
55
// All clones share these properties by reference:
52
56
$ this ->rootRefs = array (
@@ -55,6 +59,8 @@ public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null,
55
59
&$ this ->isCollected ,
56
60
&$ this ->clonesCount ,
57
61
);
62
+
63
+ $ this ->sourceContextProvider = new SourceContextProvider ($ this ->charset );
58
64
}
59
65
60
66
public function __clone ()
@@ -71,61 +77,12 @@ public function dump(Data $data)
71
77
$ this ->isCollected = false ;
72
78
}
73
79
74
- $ trace = debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS , 7 );
75
-
76
- $ file = $ trace [0 ]['file ' ];
77
- $ line = $ trace [0 ]['line ' ];
78
- $ name = false ;
79
- $ fileExcerpt = false ;
80
-
81
- for ($ i = 1 ; $ i < 7 ; ++$ i ) {
82
- if (isset ($ trace [$ i ]['class ' ], $ trace [$ i ]['function ' ])
83
- && 'dump ' === $ trace [$ i ]['function ' ]
84
- && 'Symfony\Component\VarDumper\VarDumper ' === $ trace [$ i ]['class ' ]
85
- ) {
86
- $ file = $ trace [$ i ]['file ' ];
87
- $ line = $ trace [$ i ]['line ' ];
88
-
89
- while (++$ i < 7 ) {
90
- if (isset ($ trace [$ i ]['function ' ], $ trace [$ i ]['file ' ]) && empty ($ trace [$ i ]['class ' ]) && 0 !== strpos ($ trace [$ i ]['function ' ], 'call_user_func ' )) {
91
- $ file = $ trace [$ i ]['file ' ];
92
- $ line = $ trace [$ i ]['line ' ];
93
-
94
- break ;
95
- } elseif (isset ($ trace [$ i ]['object ' ]) && $ trace [$ i ]['object ' ] instanceof Template) {
96
- $ template = $ trace [$ i ]['object ' ];
97
- $ name = $ template ->getTemplateName ();
98
- $ src = method_exists ($ template , 'getSourceContext ' ) ? $ template ->getSourceContext ()->getCode () : (method_exists ($ template , 'getSource ' ) ? $ template ->getSource () : false );
99
- $ info = $ template ->getDebugInfo ();
100
- if (isset ($ info [$ trace [$ i - 1 ]['line ' ]])) {
101
- $ line = $ info [$ trace [$ i - 1 ]['line ' ]];
102
- $ file = method_exists ($ template , 'getSourceContext ' ) ? $ template ->getSourceContext ()->getPath () : null ;
103
-
104
- if ($ src ) {
105
- $ src = explode ("\n" , $ src );
106
- $ fileExcerpt = array ();
107
-
108
- for ($ i = max ($ line - 3 , 1 ), $ max = min ($ line + 3 , count ($ src )); $ i <= $ max ; ++$ i ) {
109
- $ fileExcerpt [] = '<li ' .($ i === $ line ? ' class="selected" ' : '' ).'><code> ' .$ this ->htmlEncode ($ src [$ i - 1 ]).'</code></li> ' ;
110
- }
111
-
112
- $ fileExcerpt = '<ol start=" ' .max ($ line - 3 , 1 ).'"> ' .implode ("\n" , $ fileExcerpt ).'</ol> ' ;
113
- }
114
- }
115
- break ;
116
- }
117
- }
118
- break ;
119
- }
120
- }
80
+ list ('name ' => $ name , 'file ' => $ file , 'line ' => $ line , 'file_excerpt ' => $ fileExcerpt ) = $ this ->sourceContextProvider ->getContext ();
121
81
122
- if (false === $ name ) {
123
- $ name = str_replace ('\\' , '/ ' , $ file );
124
- $ name = substr ($ name , strrpos ($ name , '/ ' ) + 1 );
125
- }
126
-
127
- if ($ this ->dumper ) {
128
- $ this ->doDump ($ data , $ name , $ file , $ line );
82
+ if ($ this ->serverDumper && $ this ->serverDumper ->isServerListening ()) {
83
+ $ this ->doDump ($ this ->serverDumper , $ data , $ name , $ file , $ line );
84
+ } elseif ($ this ->dumper ) {
85
+ $ this ->doDump ($ this ->dumper , $ data , $ name , $ file , $ line );
129
86
}
130
87
131
88
$ this ->data [] = compact ('data ' , 'name ' , 'file ' , 'line ' , 'fileExcerpt ' );
@@ -138,6 +95,11 @@ public function dump(Data $data)
138
95
139
96
public function collect (Request $ request , Response $ response , \Exception $ exception = null )
140
97
{
98
+ // If the server dumper is set and server is up, dump were already sent to it.
99
+ if ($ this ->serverDumper && $ this ->serverDumper ->isServerListening ()) {
100
+ return ;
101
+ }
102
+
141
103
// Sub-requests and programmatic calls stay in the collected profile.
142
104
if ($ this ->dumper || ($ this ->requestStack && $ this ->requestStack ->getMasterRequest () !== $ request ) || $ request ->isXmlHttpRequest () || $ request ->headers ->has ('Origin ' )) {
143
105
return ;
@@ -152,14 +114,14 @@ public function collect(Request $request, Response $response, \Exception $except
152
114
|| false === strripos ($ response ->getContent (), '</body> ' )
153
115
) {
154
116
if ($ response ->headers ->has ('Content-Type ' ) && false !== strpos ($ response ->headers ->get ('Content-Type ' ), 'html ' )) {
155
- $ this -> dumper = new HtmlDumper ('php://output ' , $ this ->charset );
156
- $ this -> dumper ->setDisplayOptions (array ('fileLinkFormat ' => $ this ->fileLinkFormat ));
117
+ $ dumper = new HtmlDumper ('php://output ' , $ this ->charset );
118
+ $ dumper ->setDisplayOptions (array ('fileLinkFormat ' => $ this ->fileLinkFormat ));
157
119
} else {
158
- $ this -> dumper = new CliDumper ('php://output ' , $ this ->charset );
120
+ $ dumper = new CliDumper ('php://output ' , $ this ->charset );
159
121
}
160
122
161
123
foreach ($ this ->data as $ dump ) {
162
- $ this ->doDump ($ dump ['data ' ], $ dump ['name ' ], $ dump ['file ' ], $ dump ['line ' ]);
124
+ $ this ->doDump ($ dumper , $ dump ['data ' ], $ dump ['name ' ], $ dump ['file ' ], $ dump ['line ' ]);
163
125
}
164
126
}
165
127
}
@@ -243,33 +205,36 @@ public function __destruct()
243
205
$ this ->clonesCount = 0 ;
244
206
$ this ->isCollected = true ;
245
207
246
- $ h = headers_list ();
247
- $ i = count ($ h );
248
- array_unshift ($ h , 'Content-Type: ' .ini_get ('default_mimetype ' ));
249
- while (0 !== stripos ($ h [$ i ], 'Content-Type: ' )) {
250
- --$ i ;
251
- }
208
+ // If the server dumper is set and server is up, dump were already sent to it.
209
+ if (!$ this ->serverDumper || !$ this ->serverDumper ->isServerListening ()) {
210
+ $ h = headers_list ();
211
+ $ i = count ($ h );
212
+ array_unshift ($ h , 'Content-Type: ' .ini_get ('default_mimetype ' ));
213
+ while (0 !== stripos ($ h [$ i ], 'Content-Type: ' )) {
214
+ --$ i ;
215
+ }
252
216
253
- if ('cli ' !== PHP_SAPI && stripos ($ h [$ i ], 'html ' )) {
254
- $ this -> dumper = new HtmlDumper ('php://output ' , $ this ->charset );
255
- $ this -> dumper ->setDisplayOptions (array ('fileLinkFormat ' => $ this ->fileLinkFormat ));
256
- } else {
257
- $ this -> dumper = new CliDumper ('php://output ' , $ this ->charset );
258
- }
217
+ if ('cli ' !== PHP_SAPI && stripos ($ h [$ i ], 'html ' )) {
218
+ $ dumper = new HtmlDumper ('php://output ' , $ this ->charset );
219
+ $ dumper ->setDisplayOptions (array ('fileLinkFormat ' => $ this ->fileLinkFormat ));
220
+ } else {
221
+ $ dumper = new CliDumper ('php://output ' , $ this ->charset );
222
+ }
259
223
260
- foreach ($ this ->data as $ i => $ dump ) {
261
- $ this ->data [$ i ] = null ;
262
- $ this ->doDump ($ dump ['data ' ], $ dump ['name ' ], $ dump ['file ' ], $ dump ['line ' ]);
224
+ foreach ($ this ->data as $ i => $ dump ) {
225
+ $ this ->data [$ i ] = null ;
226
+ $ this ->doDump ($ dumper , $ dump ['data ' ], $ dump ['name ' ], $ dump ['file ' ], $ dump ['line ' ]);
227
+ }
263
228
}
264
229
265
230
$ this ->data = array ();
266
231
$ this ->dataCount = 0 ;
267
232
}
268
233
}
269
234
270
- private function doDump ($ data , $ name , $ file , $ line )
235
+ private function doDump (DataDumperInterface $ dumper , $ data , $ name , $ file , $ line )
271
236
{
272
- if ($ this -> dumper instanceof CliDumper) {
237
+ if ($ dumper instanceof CliDumper) {
273
238
$ contextDumper = function ($ name , $ file , $ line , $ fmt ) {
274
239
D7AE
if ($ this instanceof HtmlDumper) {
275
240
if ($ file ) {
@@ -290,26 +255,16 @@ private function doDump($data, $name, $file, $line)
290
255
}
291
256
$ this ->dumpLine (0 );
292
257
};
293
- $ contextDumper = $ contextDumper ->bindTo ($ this -> dumper , $ this -> dumper );
258
+ $ contextDumper = $ contextDumper ->bindTo ($ dumper , $ dumper );
294
259
$ contextDumper ($ name , $ file , $ line , $ this ->fileLinkFormat );
260
+ } elseif ($ dumper instanceof ServerDumper) {
261
+ if ($ this ->dumper ) {
262
+ $ dumper ->setWrappedDumper ($ this ->dumper );
263
+ }
295
264
} else {
296
265
$ cloner = new VarCloner ();
297
- $ this -> dumper ->dump ($ cloner ->cloneVar ($ name .' on line ' .$ line .': ' ));
266
+ $ dumper ->dump ($ cloner ->cloneVar ($ name .' on line ' .$ line .': ' ));
298
267
}
299
- $ this ->dumper ->dump ($ data );
300
- }
301
-
302
- private function htmlEncode ($ s )
303
- {
304
- $ html = '' ;
305
-
306
- $ dumper = new HtmlDumper (function ($ line ) use (&$ html ) { $ html .= $ line ; }, $ this ->charset );
307
- $ dumper ->setDumpHeader ('' );
308
- $ dumper ->setDumpBoundaries ('' , '' );
309
-
310
- $ cloner = new VarCloner ();
311
- $ dumper ->dump ($ cloner ->cloneVar ($ s ));
312
-
313
- return substr (strip_tags ($ html ), 1 , -1 );
268
+ $ dumper ->dump ($ data );
314
269
}
315
270
}
0 commit comments