2626class DumpDataCollector extends DataCollector implements DataDumperInterface
2727{
2828 private $ stopwatch ;
29+ private $ dataCount = 0 ;
2930 private $ isCollected = true ;
30- private $ clonesRoot ;
3131 private $ clonesCount = 0 ;
32+ private $ clonesIndex = 0 ;
33+ private $ rootRefs ;
3234
3335 public function __construct (Stopwatch $ stopwatch = null )
3436 {
3537 $ this ->stopwatch = $ stopwatch ;
36- $ this ->clonesRoot = $ this ;
38+
39+ // All clones share these properties by reference:
40+ $ this ->rootRefs = array (
41+ &$ this ->data ,
42+ &$ this ->dataCount ,
43+ &$ this ->isCollected ,
44+ &$ this ->clonesCount ,
45+ );
3746 }
3847
3948 public function __clone ()
4049 {
41- $ this ->data = array ();
42- $ this ->clonesRoot ->clonesCount ++;
50+ $ this ->clonesIndex = ++$ this ->clonesCount ;
4351 }
4452
4553 public function dump (Data $ data )
4654 {
4755 if ($ this ->stopwatch ) {
4856 $ this ->stopwatch ->start ('dump ' );
4957 }
50- if ($ this ->clonesRoot ->isCollected ) {
51- $ this ->clonesRoot ->isCollected = false ;
52- register_shutdown_function (array ($ this ->clonesRoot , 'flushDumps ' ));
58+ if ($ this ->isCollected ) {
59+ $ this ->isCollected = false ;
5360 }
5461
5562 $ trace = PHP_VERSION_ID >= 50306 ? DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS : true ;
@@ -66,7 +73,7 @@ public function dump(Data $data)
6673
6774 for ($ i = 1 ; $ i < 7 ; ++$ i ) {
6875 if (isset ($ trace [$ i ]['class ' ], $ trace [$ i ]['function ' ])
69- && 'dump ' === $ trace [$ i ]['function ' ]
76+ && ( 'dump ' === $ trace [$ i ]['function ' ] || ' debug ' === $ trace [ $ i ][ ' function ' ])
7077 && 'Symfony\Component\VarDumper\VarDumper ' === $ trace [$ i ]['class ' ]
7178 ) {
7279 $ file = $ trace [$ i ]['file ' ];
@@ -107,7 +114,8 @@ public function dump(Data $data)
107114 $ name = substr ($ file , strrpos ($ file , '/ ' ) + 1 );
108115 }
109116
110- $ this ->clonesRoot ->data [] = compact ('data ' , 'name ' , 'file ' , 'line ' , 'fileExcerpt ' );
117+ $ this ->data [] = compact ('data ' , 'name ' , 'file ' , 'line ' , 'fileExcerpt ' );
118+ ++$ this ->dataCount ;
111119
112120 if ($ this ->stopwatch ) {
113121 $ this ->stopwatch ->stop ('dump ' );
@@ -120,23 +128,28 @@ public function collect(Request $request, Response $response, \Exception $except
120128
121129 public function serialize ()
122130 {
123- $ ser = serialize ($ this ->clonesRoot ->data );
124- $ this ->clonesRoot ->data = array ();
125- $ this ->clonesRoot ->isCollected = true ;
131+ if ($ this ->clonesCount !== $ this ->clonesIndex ) {
132+ return 'a:0:{} ' ;
133+ }
134+
135+ $ ser = serialize ($ this ->data );
136+ $ this ->data = array ();
137+ $ this ->dataCount = 0 ;
138+ $ this ->isCollected = true ;
126139
127140 return $ ser ;
128141 }
129142
130143 public function unserialize ($ data )
131144 {
132145 parent ::unserialize ($ data );
133-
134- $ this ->clonesRoot = $ this ;
146+ $ this -> dataCount = count ( $ this -> data );
147+ self :: __construct ( $ this ->stopwatch ) ;
135148 }
136149
137150 public function getDumpsCount ()
138151 {
139- return count ( $ this ->clonesRoot -> data ) ;
152+ return $ this ->dataCount ;
140153 }
141154
142155 public function getDumpsExcerpts ()
@@ -193,7 +206,7 @@ public function getDumps($getData = false)
193206 }
194207 $ dumps = array ();
195208
196- foreach ($ this ->clonesRoot -> data as $ dump ) {
209+ foreach ($ this ->data as $ dump ) {
197210 $ json = '' ;
198211 if ($ getData ) {
199212 $ dumper ->dump ($ dump ['data ' ], function ($ line ) use (&$ json ) {$ json .= $ line ;});
@@ -210,11 +223,11 @@ public function getName()
210223 return 'dump ' ;
211224 }
212225
213- public function flushDumps ()
226+ public function __destruct ()
214227 {
215- if (0 === $ this ->clonesRoot -> clonesCount -- && !$ this ->clonesRoot -> isCollected && $ this -> clonesRoot ->data ) {
216- $ this ->clonesRoot -> clonesCount = 0 ;
217- $ this ->clonesRoot -> isCollected = true ;
228+ if (0 === $ this ->clonesCount -- && !$ this ->isCollected && $ this ->data ) {
229+ $ this ->clonesCount = 0 ;
230+ $ this ->isCollected = true ;
218231
219232 $ h = headers_list ();
220233 $ i = count ($ h );
@@ -223,31 +236,32 @@ public function flushDumps()
223236 --$ i ;
224237 }
225238
226- if (stripos ($ h [$ i ], 'html ' )) {
239+ if (' cli ' !== PHP_SAPI && stripos ($ h [$ i ], 'html ' )) {
227240 echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ' ;
228- $ dumper = new HtmlDumper ();
241+ $ dumper = new HtmlDumper (' php://output ' );
229242 } else {
230- $ dumper = new CliDumper ();
243+ $ dumper = new CliDumper (' php://output ' );
231244 $ dumper ->setColors (false );
232245 }
233246
234- foreach ($ this ->clonesRoot -> data as $ i => $ dump ) {
235- $ this ->clonesRoot -> data [$ i ] = null ;
247+ foreach ($ this ->data as $ i => $ dump ) {
248+ $ this ->data [$ i ] = null ;
236249
237250 if ($ dumper instanceof HtmlDumper) {
238251 $ dump ['name ' ] = htmlspecialchars ($ dump ['name ' ], ENT_QUOTES , 'UTF-8 ' );
239252 $ dump ['file ' ] = htmlspecialchars ($ dump ['file ' ], ENT_QUOTES , 'UTF-8 ' );
240253 if ('' !== $ dump ['file ' ]) {
241254 $ dump ['name ' ] = "<abbr title= \"{$ dump ['file ' ]}\"> {$ dump ['name ' ]}</abbr> " ;
242255 }
243- echo "\n<br>< span class= \"sf-dump-meta \">in {$ dump ['name ' ]} on line {$ dump ['line ' ]}:</span> " ;
256+ echo "\n<span class= \"sf-dump-meta \"> {$ dump ['name ' ]} on line {$ dump ['line ' ]}:</span> " ;
244257 } else {
245- echo "\n in {$ dump ['name ' ]} on line {$ dump ['line ' ]}: \n \n" ;
258+ echo "{$ dump ['name ' ]} on line {$ dump ['line ' ]}: \n" ;
246259 }
247260 $ dumper ->dump ($ dump ['data ' ]);
248261 }
249262
250- $ this ->clonesRoot ->data = array ();
263+ $ this ->data = array ();
264+ $ this ->dataCount = 0 ;
251265 }
252266 }
253267}
0 commit comments