Skip to content

Commit 98eba19

Browse files
authored
Merge pull request #772 from cakephp/feat/improve-dump
Feat/improve dump
2 parents 6ca239b + ad5ceba commit 98eba19

File tree

8 files changed

+66
-11
lines changed

8 files changed

+66
-11
lines changed

psalm-baseline.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@
149149
</TypeDoesNotContainType>
150150
</file>
151151
<file src="src/View/Helper/ToolbarHelper.php">
152+
<DeprecatedMethod occurrences="1">
153+
<code>makeNeatArray</code>
154+
</DeprecatedMethod>
152155
<InvalidArgument occurrences="1">
153156
<code>$value</code>
154157
</InvalidArgument>

src/View/Helper/ToolbarHelper.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace DebugKit\View\Helper;
1717

1818
use ArrayAccess;
19+
use Cake\Error\Debug\HtmlFormatter;
20+
use Cake\Error\Debugger;
1921
use Cake\View\Helper;
2022
use Closure;
2123
use Iterator;
@@ -55,6 +57,33 @@ public function setSort($sort)
5557
$this->sort = $sort;
5658
}
5759

60+
/**
61+
* Dump the value in $value into an interactive HTML output.
62+
*
63+
* @param mixed $value The value to output.
64+
* @return string Formatted HTML
65+
*/
66+
public function dump($value)
67+
{
68+
$debugger = Debugger::getInstance();
69+
$exportFormatter = $debugger->getConfig('exportFormatter');
70+
$restore = false;
71+
if ($exportFormatter !== HtmlFormatter::class) {
72+
$restore = true;
73+
$debugger->setConfig('exportFormatter', HtmlFormatter::class);
74+
}
75+
$contents = Debugger::exportVar($value, 25);
76+
if ($restore) {
77+
$debugger->setConfig('exportFormatter', $exportFormatter);
78+
}
79+
80+
return implode([
81+
'<div class="cake-debug-output cake-debug" style="direction:ltr">',
82+
$contents,
83+
'</div>',
84+
]);
85+
}
86+
5887
/**
5988
* Recursively goes through an array and makes neat HTML out of it.
6089
*
@@ -65,6 +94,7 @@ public function setSort($sort)
6594
* @param \SplObjectStorage $currentAncestors Object references found down
6695
* the path.
6796
* @return string
97+
* @deprecated 4.4.0 Use ToolbarHelper::dump() instead.
6898
*/
6999
public function makeNeatArray(
70100
$values,

templates/element/include_panel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
}
2929
?>
3030
<h4><?= __d('debug_kit', 'Include Paths') ?></h4>
31-
<?= $this->Toolbar->makeNeatArray($paths) ?>
31+
<?= $this->Toolbar->dump($paths) ?>
3232

3333
<h4><?= __d('debug_kit', 'Included Files') ?></h4>
34-
<?= $this->Toolbar->makeNeatArray(compact('app', 'cake', 'plugins', 'vendor', 'other')) ?>
34+
<?= $this->Toolbar->dump(compact('app', 'cake', 'plugins', 'vendor', 'other')) ?>

templates/element/request_panel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
<?php endif; ?>
3333

3434
<h4><?= __d('debug_kit', 'Routing Params') ?></h4>
35-
<?= $this->Toolbar->makeNeatArray($params) ?>
35+
<?= $this->Toolbar->dump($params) ?>
3636

3737
<h4><?= __d('debug_kit', 'Post data') ?></h4>
3838
<?php
3939
if (empty($data)):
4040
echo '<p class="info">' . __d('debug_kit', 'No post data.') . '</p>';
4141
else:
42-
echo $this->Toolbar->makeNeatArray($data);
42+
echo $this->Toolbar->dump($data);
4343
endif;
4444
?>
4545

@@ -48,18 +48,18 @@
4848
if (empty($query)):
4949
echo '<p class="info">' . __d('debug_kit', 'No querystring data.') . '</p>';
5050
else:
51-
echo $this->Toolbar->makeNeatArray($query);
51+
echo $this->Toolbar->dump($query);
5252
endif;
5353
?>
5454

5555
<h4>Cookie</h4>
5656
<?php if (isset($cookie)): ?>
57-
<?= $this->Toolbar->makeNeatArray($cookie) ?>
57+
<?= $this->Toolbar->dump($cookie) ?>
5858
<?php else: ?>
5959
<p class="info"><?= __d('debug_kit', 'No Cookie data.') ?></p>
6060
<?php endif; ?>
6161

6262
<?php if (!empty($matchedRoute)): ?>
6363
<h4><?= __d('debug_kit', 'Matched Route') ?></h4>
64-
<p><?= $this->Toolbar->makeNeatArray(['template' => $matchedRoute]) ?></p>
64+
<p><?= $this->Toolbar->dump(['template' => $matchedRoute]) ?></p>
6565
<?php endif; ?>

templates/element/session_panel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
* @var array $content
1818
*/
1919
?>
20-
<?= $this->Toolbar->makeNeatArray($content);
20+
<?= $this->Toolbar->dump($content);

templates/element/variables_panel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
if (!empty($content)):
2828
printf('<label class="toggle-checkbox"><input type="checkbox" class="neat-array-sort"%s>%s</label>', $sort ? ' checked="checked"' : '', __d('debug_kit', 'Sort variables by name'));
2929
$this->Toolbar->setSort($sort);
30-
echo $this->Toolbar->makeNeatArray($content);
30+
echo $this->Toolbar->dump($content);
3131
endif;
3232

3333
if (!empty($errors)):
3434
echo '<h4>' . __d('debug_kit', 'Validation errors') . '</h4>';
35-
echo $this->Toolbar->makeNeatArray($errors);
35+
echo $this->Toolbar->dump($errors);
3636
endif;

templates/layout/panel.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,10 @@
22
/**
33
* @var \DebugKit\View\AjaxView $this
44
*/
5-
65
echo $this->fetch('content');
6+
?>
7+
<script type="text/javascript">
8+
if (window.__cakeDebugBlockInit) {
9+
window.__cakeDebugBlockInit();
10+
}
11+
</script>

tests/TestCase/View/Helper/ToolbarHelperTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
namespace DebugKit\Test\TestCase\View\Helper;
1717

18+
use Cake\Error\Debug\TextFormatter;
19+
use Cake\Error\Debugger;
1820
use Cake\Http\ServerRequest as Request;
1921
use Cake\Routing\Router;
2022
use Cake\TestSuite\TestCase;
@@ -65,6 +67,21 @@ public function tearDown(): void
6567
unset($this->Toolbar);
6668
}
6769

70+
public function testDumpCoerceHtml()
71+
{
72+
$restore = Debugger::configInstance('exportFormatter');
73+
Debugger::configInstance('exportFormatter', TextFormatter::class);
74+
$result = $this->Toolbar->dump(false);
75+
$this->assertRegExp('/<\w/', $result, 'Contains HTML tags.');
76+
$this->assertSame(
77+
TextFormatter::class,
78+
Debugger::configInstance('exportFormatter'),
79+
'Should restore setting'
80+
);
81+
// Restore back to original value.
82+
Debugger::configInstance('exportFormatter', $restore);
83+
}
84+
6885
/**
6986
* Test makeNeatArray with basic types.
7087
*

0 commit comments

Comments
 (0)