Skip to content

Commit 1a873d7

Browse files
authored
Allow useHtmlVarDumper() on ExceptionsCollector (php-debugbar#464)
1 parent 7602079 commit 1a873d7

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

src/DebugBar/DataCollector/ExceptionsCollector.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class ExceptionsCollector extends DataCollector implements Renderable
2121
protected $exceptions = array();
2222
protected $chainExceptions = false;
2323

24+
// The HTML var dumper requires debug bar users to support the new inline assets, which not all
25+
// may support yet - so return false by default for now.
26+
protected $useHtmlVarDumper = false;
27+
2428
/**
2529
* Adds an exception to be profiled in the debug bar
2630
*
@@ -65,6 +69,30 @@ public function getExceptions()
6569
return $this->exceptions;
6670
}
6771

72+
/**
73+
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
74+
* rich variable rendering.
75+
*
76+
* @param bool $value
77+
* @return $this
78+
*/
79+
public function useHtmlVarDumper($value = true)
80+
{
81+
$this->useHtmlVarDumper = $value;
82+
return $this;
83+
}
84+
85+
/**
86+
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
87+
* rendering.
88+
*
89+
* @return mixed
90+
*/
91+
public function isHtmlVarDumperUsed()
92+
{
93+
return $this->useHtmlVarDumper;
94+
}
95+
6896
public function collect()
6997
{
7098
return array(
@@ -102,13 +130,19 @@ public function formatThrowableData($e)
102130
$lines = array("Cannot open the file ($filePath) in which the exception occurred ");
103131
}
104132

133+
$traceHtml = null;
134+
if ($this->isHtmlVarDumperUsed()) {
135+
$traceHtml = $this->getVarDumper()->renderVar($e->getTrace());
136+
}
137+
105138
return array(
106139
'type' => get_class($e),
107140
'message' => $e->getMessage(),
108141
'code' => $e->getCode(),
109142
'file' => $filePath,
110143
'line' => $e->getLine(),
111144
'stack_trace' => $e->getTraceAsString(),
145+
'stack_trace_html' => $traceHtml,
112146
'surrounding_lines' => $lines,
113147
'xdebug_link' => $this->getXdebugLink($filePath, $e->getLine())
114148
);

src/DebugBar/Resources/widgets.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,11 @@ if (typeof(PhpDebugBar) == 'undefined') {
216216
});
217217

218218
// ------------------------------------------------------------------
219-
219+
220220
/**
221221
* An extension of KVListWidget where the data represents a list
222222
* of variables
223-
*
223+
*
224224
* Options:
225225
* - data
226226
*/
@@ -468,7 +468,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
468468

469469
m.appendTo(li);
470470
this.$el.append(li);
471-
471+
472472
if (measure.params && !$.isEmptyObject(measure.params)) {
473473
var table = $('<table><tr><th colspan="2">Params</th></tr></table>').addClass(csscls('params')).appendTo(li);
474474
for (var key in measure.params) {
@@ -518,7 +518,7 @@ if (typeof(PhpDebugBar) == 'undefined') {
518518
});
519519

520520
// ------------------------------------------------------------------
521-
521+
522522
/**
523523
* Widget for the displaying exceptions
524524
*
@@ -550,20 +550,26 @@ if (typeof(PhpDebugBar) == 'undefined') {
550550
}
551551
if (e.surrounding_lines) {
552552
var pre = createCodeBlock(e.surrounding_lines.join(""), 'php').addClass(csscls('file')).appendTo(li);
553-
li.click(function() {
554-
if (pre.is(':visible')) {
555-
pre.hide();
556-
} else {
557-
pre.show();
558-
}
559-
});
553+
if (!e.stack_trace_html) {
554+
// This click event makes the var-dumper hard to use.
555+
li.click(function () {
556+
if (pre.is(':visible')) {
557+
pre.hide();
558+
} else {
559+
pre.show();
560+
}
561+
});
562+
}
560563
}
561-
if (e.stack_trace) {
562-
e.stack_trace.split("\n").forEach(function(trace) {
563-
var $traceLine = $('<div />');
564-
$('<span />').addClass(csscls('filename')).text(trace).appendTo($traceLine);
565-
$traceLine.appendTo(li);
566-
});
564+
if (e.stack_trace_html) {
565+
var $trace = $('<span />').addClass(csscls('filename')).html(e.stack_trace_html);
566+
$trace.appendTo(li);
567+
} else if (e.stack_trace) {
568+
e.stack_trace.split("\n").forEach(function (trace) {
569+
var $traceLine = $('<div />');
570+
$('<span />').addClass(csscls('filename')).text(trace).appendTo($traceLine);
571+
$traceLine.appendTo(li);
572+
});
567573
}
568574
}});
569575
this.$list.$el.appendTo(this.$el);
@@ -578,6 +584,6 @@ if (typeof(PhpDebugBar) == 'undefined') {
578584
}
579585

580586
});
581-
587+
582588

583589
})(PhpDebugBar.$);

0 commit comments

Comments
 (0)