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
24
use Symfony \Component \VarDumper \Dumper \ServerDumper ;
24
- use Twig \Template ;
25
25
26
26
/**
27
27
* @author Nicolas Grekas <p@tchwork.com>
@@ -40,6 +40,7 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
40
40
private $ dumper ;
41
41
private $ dumperIsInjected ;
42
42
private $ serverDumper ;
43
+ private $ sourceContextProvider ;
43
44
44
45
public function __construct (Stopwatch $ stopwatch = null , $ fileLinkFormat = null , $ charset = null , RequestStack $ requestStack = null , DataDumperInterface $ dumper = null , ServerDumper $ serverDumper = null )
45
46
{
@@ -58,6 +59,8 @@ public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null,
58
59
&$ this ->isCollected ,
59
60
&$ this ->clonesCount ,
60
61
);
62
+
63
+ $ this ->sourceContextProvider = new SourceContextProvider ();
61
64
}
62
65
63
66
public function __clone ()
@@ -74,64 +77,16 @@ public function dump(Data $data)
74
77
$ this ->isCollected = false ;
75
78
}
76
79
77
- $ trace = debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS , 7 );
78
-
79
- $ file = $ trace [0 ]['file ' ];
80
- $ line = $ trace [0 ]['line ' ];
81
- $ name = false ;
82
- $ fileExcerpt = false ;
83
-
84
- for ($ i = 1 ; $ i < 7 ; ++$ i ) {
85
- if (isset ($ trace [$ i ]['class ' ], $ trace [$ i ]['function ' ])
86
- && 'dump ' === $ trace [$ i ]['function ' ]
87
- && 'Symfony\Component\VarDumper\VarDumper ' === $ trace [$ i ]['class ' ]
88
- ) {
89
- $ file = $ trace [$ i ]['file ' ];
90
- $ line = $ trace [$ i ]['line ' ];
91
-
92
- while (++$ i < 7 ) {
93
- if (isset ($ trace [$ i ]['function ' ], $ trace [$ i ]['file ' ]) && empty ($ trace [$ i ]['class ' ]) && 0 !== strpos ($ trace [$ i ]['function ' ], 'call_user_func ' )) {
94
- $ file = $ trace [$ i ]['file ' ];
95
- $ line = $ trace [$ i ]['line ' ];
96
-
97
- break ;
98
- } elseif (isset ($ trace [$ i ]['object ' ]) && $ trace [$ i ]['object ' ] instanceof Template) {
99
- $ template = $ trace [$ i ]['object ' ];
100
- $ name = $ template ->getTemplateName ();
101
- $ src = method_exists ($ template , 'getSourceContext ' ) ? $ template ->getSourceContext ()->getCode () : (method_exists ($ template , 'getSource ' ) ? $ template ->getSource () : false );
102
- $ info = $ template ->getDebugInfo ();
103
- if (isset ($ info [$ trace [$ i - 1 ]['line ' ]])) {
104
- $ line = $ info [$ trace [$ i - 1 ]['line ' ]];
105
- $ file = method_exists ($ template , 'getSourceContext ' ) ? $ template ->getSourceContext ()->getPath () : null ;
106
-
107
- if ($ src ) {
108
- $ src = explode ("\n" , $ src );
109
- $ fileExcerpt = array ();
110
-
111
- for ($ i = max ($ line - 3 , 1 ), $ max = min ($ line + 3 , count ($ src )); $ i <= $ max ; ++$ i ) {
112
- $ fileExcerpt [] = '<li ' .($ i === $ line ? ' class="selected" ' : '' ).'><code> ' .$ this ->htmlEncode ($ src [$ i - 1 ]).'</code></li> ' ;
113
- }
114
-
115
- $ fileExcerpt = '<ol start=" ' .max ($ line - 3 , 1 ).'"> ' .implode ("\n" , $ fileExcerpt ).'</ol> ' ;
116
- }
117
- }
118
- break ;
119
- }
120
- }
121
- break ;
122
- }
123
- }
124
-
125
- if (false === $ name ) {
126
- $ name = str_replace ('\\' , '/ ' , $ file );
127
- $ name = substr ($ name , strrpos ($ name , '/ ' ) + 1 );
128
- }
80
+ $ provider = $ this ->sourceContextProvider ;
81
+ $ context = $ provider ();
129
82
130
83
if ($ this ->dumper ) {
131
- $ this ->doDump ($ data , $ name , $ file , $ line );
84
+ $ this ->doDump ($ data , $ context [ ' name ' ] , $ context [ ' file ' ] , $ context [ ' line ' ] );
132
85
}
133
86
134
- $ this ->data [] = compact ('data ' , 'name ' , 'file ' , 'line ' , 'fileExcerpt ' );
87
+ $ context ['data ' ] = $ data ;
88
+
89
+ $ this ->data [] = $ context ;
135
90
++$ this ->dataCount ;
136
91
137
92
if ($ this ->stopwatch ) {
@@ -298,18 +253,4 @@ private function doDump($data, $name, $file, $line)
298
253
}
299
254
$ dumper ->dump ($ data );
300
255
}
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 );
314
- }
315
256
}
0 commit comments