Skip to content

Commit 35fa8ab

Browse files
james-johnston-thumbtackbarryvdh
authored andcommitted
Use new HtmlVariableListWidget with collectors (php-debugbar#346)
Introduce a new HtmlVariableListWidget that is similar to VariableListWidget but for variables with HTML contents. Update the collectors that use the existing VariableListWidget to use DebugBarVarDumper to dump the variables using the VarDumper HtmlDumper. Because many debug bar users may not yet support inline static assets, default to use the old VariableListWidget for now. Updated collectors: * ConfigCollector * RequestDataCollector
1 parent 54214da commit 35fa8ab

File tree

6 files changed

+158
-12
lines changed

6 files changed

+158
-12
lines changed

docs/base_collectors.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,26 @@ If you want to see your PDO requests in the TimeDataCollector, you must add the
9090

9191
## RequestData
9292

93-
Collects the data of PHP's global variables
93+
Collects the data of PHP's global variables. You can call the `useHtmlVarDumper()` function to use
94+
VarDumper's interactive HTML dumper for rendering the variables. If you do that, you must properly
95+
render [inline assets](rendering.html#assets) when rendering the debug bar in addition to the normal
96+
js/css static assets.
9497

95-
$debugbar->addCollector(new DebugBar\DataCollector\RequestDataCollector());
98+
$requestDataCollector = new DebugBar\DataCollector\RequestDataCollector();
99+
$requestDataCollector->useHtmlVarDumper();
100+
$debugbar->addCollector($requestDataCollector);
96101

97102
## Config
98103

99-
Used to display any key/value pairs array
104+
Used to display any key/value pairs array. You can call the `useHtmlVarDumper()` function to use
105+
VarDumper's interactive HTML dumper for rendering the variables. If you do that, you must properly
106+
render [inline assets](rendering.html#assets) when rendering the debug bar in addition to the normal
107+
js/css static assets.
100108

101109
$data = array('foo' => 'bar');
102-
$debugbar->addCollector(new DebugBar\DataCollector\ConfigCollector($data));
110+
$configCollector = new DebugBar\DataCollector\ConfigCollector($data);
111+
$configCollector->useHtmlVarDumper();
112+
$debugbar->addCollector($configCollector);
103113

104114
You can provide a different name for this collector in the second argument of the constructor.
105115

src/DebugBar/DataCollector/ConfigCollector.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,40 @@
1313
/**
1414
* Collects array data
1515
*/
16-
class ConfigCollector extends DataCollector implements Renderable
16+
class ConfigCollector extends DataCollector implements Renderable, AssetProvider
1717
{
1818
protected $name;
1919

2020
protected $data;
2121

22+
// The HTML var dumper requires debug bar users to support the new inline assets, which not all
23+
// may support yet - so return false by default for now.
24+
protected $useHtmlVarDumper = false;
25+
26+
/**
27+
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
28+
* rich variable rendering.
29+
*
30+
* @param bool $value
31+
* @return $this
32+
*/
33+
public function useHtmlVarDumper($value = true)
34+
{
35+
$this->useHtmlVarDumper = $value;
36+
return $this;
37+
}
38+
39+
/**
40+
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
41+
* rendering.
42+
*
43+
* @return mixed
44+
*/
45+
public function isHtmlVarDumperUsed()
46+
{
47+
return $this->useHtmlVarDumper;
48+
}
49+
2250
/**
2351
* @param array $data
2452
* @param string $name
@@ -46,7 +74,9 @@ public function collect()
4674
{
4775
$data = array();
4876
foreach ($this->data as $k => $v) {
49-
if (!is_string($v)) {
77+
if ($this->isHtmlVarDumperUsed()) {
78+
$v = $this->getVarDumper()->renderVar($v);
79+
} else if (!is_string($v)) {
5080
$v = $this->getDataFormatter()->formatVar($v);
5181
}
5282
$data[$k] = $v;
@@ -62,16 +92,26 @@ public function getName()
6292
return $this->name;
6393
}
6494

95+
/**
96+
* @return array
97+
*/
98+
public function getAssets() {
99+
return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : array();
100+
}
101+
65102
/**
66103
* @return array
67104
*/
68105
public function getWidgets()
69106
{
70107
$name = $this->getName();
108+
$widget = $this->isHtmlVarDumperUsed()
109+
? "PhpDebugBar.Widgets.HtmlVariableListWidget"
110+
: "PhpDebugBar.Widgets.VariableListWidget";
71111
return array(
72112
"$name" => array(
73113
"icon" => "gear",
74-
"widget" => "PhpDebugBar.Widgets.VariableListWidget",
114+
"widget" => $widget,
75115
"map" => "$name",
76116
"default" => "{}"
77117
)

src/DebugBar/DataCollector/RequestDataCollector.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,36 @@
1313
/**
1414
* Collects info about the current request
1515
*/
16-
class RequestDataCollector extends DataCollector implements Renderable
16+
class RequestDataCollector extends DataCollector implements Renderable, AssetProvider
1717
{
18+
// The HTML var dumper requires debug bar users to support the new inline assets, which not all
19+
// may support yet - so return false by default for now.
20+
protected $useHtmlVarDumper = false;
21+
22+
/**
23+
* Sets a flag indicating whether the Symfony HtmlDumper will be used to dump variables for
24+
* rich variable rendering.
25+
*
26+
* @param bool $value
27+
* @return $this
28+
*/
29+
public function useHtmlVarDumper($value = true)
30+
{
31+
$this->useHtmlVarDumper = $value;
32+
return $this;
33+
}
34+
35+
/**
36+
* Indicates whether the Symfony HtmlDumper will be used to dump variables for rich variable
37+
* rendering.
38+
*
39+
* @return mixed
40+
*/
41+
public function isHtmlVarDumperUsed()
42+
{
43+
return $this->useHtmlVarDumper;
44+
}
45+
1846
/**
1947
* @return array
2048
*/
@@ -25,7 +53,12 @@ public function collect()
2553

2654
foreach ($vars as $var) {
2755
if (isset($GLOBALS[$var])) {
28-
$data["$" . $var] = $this->getDataFormatter()->formatVar($GLOBALS[$var]);
56+
$key = "$" . $var;
57+
if ($this->isHtmlVarDumperUsed()) {
58+
$data[$key] = $this->getVarDumper()->renderVar($GLOBALS[$var]);
59+
} else {
60+
$data[$key] = $this->getDataFormatter()->formatVar($GLOBALS[$var]);
61+
}
2962
}
3063
}
3164

@@ -40,15 +73,25 @@ public function getName()
4073
return 'request';
4174
}
4275

76+
/**
77+
* @return array
78+
*/
79+
public function getAssets() {
80+
return $this->isHtmlVarDumperUsed() ? $this->getVarDumper()->getAssets() : array();
81+
}
82+
4383
/**
4484
* @return array
4585
*/
4686
public function getWidgets()
4787
{
88+
$widget = $this->isHtmlVarDumperUsed()
89+
? "PhpDebugBar.Widgets.HtmlVariableListWidget"
90+
: "PhpDebugBar.Widgets.VariableListWidget";
4891
return array(
4992
"request" => array(
5093
"icon" => "tags",
51-
"widget" => "PhpDebugBar.Widgets.VariableListWidget",
94+
"widget" => $widget,
5295
"map" => "request",
5396
"default" => "{}"
5497
)

src/DebugBar/Resources/widgets.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ dl.phpdebugbar-widgets-kvlist {
149149

150150
/* -------------------------------------- */
151151

152-
dl.phpdebugbar-widgets-varlist {
152+
dl.phpdebugbar-widgets-varlist,
153+
dl.phpdebugbar-widgets-htmlvarlist {
153154
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
154155
}
156+
dl.phpdebugbar-widgets-htmlvarlist dd {
157+
cursor: initial;
158+
}
155159

156160
/* -------------------------------------- */
157161

src/DebugBar/Resources/widgets.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,28 @@ if (typeof(PhpDebugBar) == 'undefined') {
249249
});
250250

251251
// ------------------------------------------------------------------
252-
252+
253+
/**
254+
* An extension of KVListWidget where the data represents a list
255+
* of variables whose contents are HTML; this is useful for showing
256+
* variable output from VarDumper's HtmlDumper.
257+
*
258+
* Options:
259+
* - data
260+
*/
261+
var HtmlVariableListWidget = PhpDebugBar.Widgets.HtmlVariableListWidget = KVListWidget.extend({
262+
263+
className: csscls('kvlist htmlvarlist'),
264+
265+
itemRenderer: function(dt, dd, key, value) {
266+
$('<span />').attr('title', key).text(key).appendTo(dt);
267+
dd.html(value);
268+
}
269+
270+
});
271+
272+
// ------------------------------------------------------------------
273+
253274
/**
254275
* Iframe widget
255276
*

tests/DebugBar/Tests/DataCollector/ConfigCollectorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,32 @@ public function testName()
2525
$this->assertEquals('foo', $c->getName());
2626
$this->assertArrayHasKey('foo', $c->getWidgets());
2727
}
28+
29+
public function testAssets()
30+
{
31+
$c = new ConfigCollector();
32+
$this->assertEmpty($c->getAssets());
33+
34+
$c->useHtmlVarDumper();
35+
$this->assertNotEmpty($c->getAssets());
36+
}
37+
38+
public function testHtmlRendering()
39+
{
40+
$c = new ConfigCollector(array('k' => array('one', 'two')));
41+
42+
$this->assertFalse($c->isHtmlVarDumperUsed());
43+
$data = $c->collect();
44+
$this->assertEquals(array('k'), array_keys($data));
45+
$this->assertContains('one', $data['k']);
46+
$this->assertContains('two', $data['k']);
47+
$this->assertNotContains('span', $data['k']);
48+
49+
$c->useHtmlVarDumper();
50+
$data = $c->collect();
51+
$this->assertEquals(array('k'), array_keys($data));
52+
$this->assertContains('one', $data['k']);
53+
$this->assertContains('two', $data['k']);
54+
$this->assertContains('span', $data['k']);
55+
}
2856
}

0 commit comments

Comments
 (0)