Skip to content

Commit

Permalink
Dumper: added option max ITEMS
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 14, 2020
1 parent 3128566 commit c8f135c
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 11 deletions.
20 changes: 17 additions & 3 deletions src/Tracy/Dumper/Describer.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class Describer
/** @var int|null */
public $maxLength = 150;

/** @var int|null */
public $maxItems = 50;

/** @var bool */
public $location = false;

Expand Down Expand Up @@ -114,18 +117,24 @@ private function describeArray(array $arr, int $depth = 0)
{
if (count($arr) && $depth >= $this->maxDepth) {
return (object) ['array' => [], 'stop' => true, 'length' => count($arr)];

} elseif ($this->maxItems && count($arr) > $this->maxItems) {
$res = (object) ['array' => &$items, 'length' => count($arr), 'long' => true];
$arr = array_slice($arr, 0, $this->maxItems, true);
}
$res = [];

$items = [];
foreach ($arr as $k => $v) {
$refId = $this->getReferenceId($arr, $k);
$res[] = [
$items[] = [
$this->encodeKey($k),
is_string($k) && isset($this->keysToHide[strtolower($k)])
? (object) ['key' => self::hideValue($v)]
: $this->describeVar($v, $depth + 1, $refId),
] + ($refId ? [2 => $refId] : []);
}
return $res;

return $res ?? $items;
}


Expand Down Expand Up @@ -154,6 +163,11 @@ private function describeObject(object $obj, int $depth = 0): object
$shot->items = [];

$props = $this->exposeObject($obj);
if ($this->maxItems && count($props) > $this->maxItems) {
$shot->long = true;
$props = array_slice($props, 0, $this->maxItems, true);
}

foreach ($props as $k => $v) {
$visibility = 0;
$refId = $this->getReferenceId($props, $k);
Expand Down
2 changes: 2 additions & 0 deletions src/Tracy/Dumper/Dumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Dumper
public const
DEPTH = 'depth', // how many nested levels of array/object properties display (defaults to 4)
TRUNCATE = 'truncate', // how truncate long strings? (defaults to 150)
ITEMS = 'items', // how many items in array/object display? (defaults to 50)
COLLAPSE = 'collapse', // collapse top array/object or how big are collapsed? (defaults to 14)
COLLAPSE_COUNT = 'collapsecount', // how big array/object are collapsed? (defaults to 7)
LOCATION = 'location', // show location string? (defaults to 0)
Expand Down Expand Up @@ -131,6 +132,7 @@ private function __construct(array $options = [])
$describer = $this->describer = new Describer;
$describer->maxDepth = $options[self::DEPTH] ?? $describer->maxDepth;
$describer->maxLength = $options[self::TRUNCATE] ?? $describer->maxLength;
$describer->maxItems = $options[self::ITEMS] ?? $describer->maxItems;
$describer->location = (bool) ($this->location & self::LOCATION_CLASS);
if ($options[self::LIVE] ?? false) {
$describer->snapshot = &self::$liveSnapshot;
Expand Down
19 changes: 14 additions & 5 deletions src/Tracy/Dumper/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ private function renderArray($model, int $depth): string
return $out . $model->length . ') ' . ($model->stop === 'r' ? '[ <i>RECURSION</i> ]' : '[ ... ]') . "\n";
}

[$items, $count] = is_array($model)
? [$model, count($model)]
: [$model->array, $model->length ?? count($model->array)];
[$items, $count, $long] = is_array($model)
? [$model, count($model), false]
: [$model->array, $model->length ?? count($model->array), !empty($model->long)];

if (empty($items)) {
return $out . ")\n";
Expand All @@ -189,14 +189,18 @@ private function renderArray($model, int $depth): string
}

$out = $span . '>' . $out . $count . ")</span>\n" . '<div' . ($collapsed ? ' class="tracy-collapsed"' : '') . '>';
$indent = '<span class="tracy-dump-indent"> ' . str_repeat('| ', $depth) . '</span>';

foreach ($items as $info) {
$out .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $depth) . '</span>'
$out .= $indent
. '<span class="tracy-dump-key">' . Helpers::escapeHtml($info[0]) . '</span> => '
. (isset($info[2]) ? '<span class="tracy-dump-hash">&' . $info[2] . '</span> ' : '')
. $this->renderVar($info[1], $depth + 1);
}

if ($long) {
$out .= "$indent...\n";
}
return $out . '</div>';
}

Expand Down Expand Up @@ -243,16 +247,21 @@ private function renderObject(object $model, int $depth): string
}

$out = $span . '>' . $out . "</span>\n" . '<div' . ($collapsed ? ' class="tracy-collapsed"' : '') . '>';
$indent = '<span class="tracy-dump-indent"> ' . str_repeat('| ', $depth) . '</span>';
$this->parents[] = $model->object;

foreach ($object->items as $info) {
$out .= '<span class="tracy-dump-indent"> ' . str_repeat('| ', $depth) . '</span>'
$out .= $indent
. '<span class="tracy-dump-key">' . Helpers::escapeHtml($info[0]) . '</span>'
. ': '
. ($info[2] ? '<span class="tracy-dump-visibility">' . ($info[2] === 1 ? 'protected' : 'private') . '</span> ' : '')
. (isset($info[3]) ? '<span class="tracy-dump-hash">&' . $info[3] . '</span> ' : '')
. $this->renderVar($info[1], $depth + 1);
}

if (!empty($object->long)) {
$out .= "$indent...\n";
}
array_pop($this->parents);
return $out . '</div>';
}
Expand Down
11 changes: 9 additions & 2 deletions src/Tracy/Dumper/assets/dumper.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
' [ ... ]',
data.array,
collapsed === true || len >= collapseCount,

data.long,
true,
repository,
parentIds
Expand Down Expand Up @@ -172,6 +172,7 @@
recursive ? ' { RECURSION }' : ' { ... }',
recursive ? null : object.items,
collapsed === true || (object.items && object.items.length >= collapseCount),
object.long,
false,
repository,
parentIds
Expand All @@ -180,7 +181,7 @@
}


function buildStruct(span, ellipsis, items, collapsed, array, repository, parentIds) {
function buildStruct(span, ellipsis, items, collapsed, shortened, array, repository, parentIds) {
let res, toggle, div, handler;

if (!items || !items.length) {
Expand All @@ -198,9 +199,15 @@
toggle.addEventListener('tracy-toggle', handler = function() {
toggle.removeEventListener('tracy-toggle', handler);
createItems(div, items, array, repository, parentIds);
if (shortened) {
createEl(div, null, ['...']);
}
});
} else {
createItems(div, items, array, repository, parentIds);
if (shortened) {
createEl(div, null, ['...']);
}
}
return res;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Tracy/Dumper.toHtml().depth.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,24 @@ Assert::match('<pre class="tracy-dump"><span class="tracy-toggle"><span class="t
<span class="tracy-dump-indent"> </span><span class="tracy-dump-key">1</span> => <span class="tracy-toggle"><span class="tracy-dump-object">stdClass</span> <span class="tracy-dump-hash">#%a%</span></span>
<div><span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">0</span>: <span class="tracy-dump-object">stdClass</span> <span class="tracy-dump-hash">#%a%</span> { ... }
</div></div></pre>', Dumper::toHtml($arr, [Dumper::DEPTH => 2, Dumper::TRUNCATE => 50]));


$arr = [1, 2, 3, 4, 5, 6];

Assert::match('<pre class="tracy-dump"><span class="tracy-toggle"><span class="tracy-dump-array">array</span> (2)</span>
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-key">0</span> => <span class="tracy-toggle"><span class="tracy-dump-array">array</span> (6)</span>
<div><span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">0</span> => <span class="tracy-dump-number">1</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">1</span> => <span class="tracy-dump-number">2</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">2</span> => <span class="tracy-dump-number">3</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">3</span> => <span class="tracy-dump-number">4</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">4</span> => <span class="tracy-dump-number">5</span>
<span class="tracy-dump-indent"> | </span>...
</div><span class="tracy-dump-indent"> </span><span class="tracy-dump-key">1</span> => <span class="tracy-toggle"><span class="tracy-dump-object">stdClass</span> <span class="tracy-dump-hash">#%d%</span></span>
<div><span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">0</span>: <span class="tracy-dump-number">1</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">1</span>: <span class="tracy-dump-number">2</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">2</span>: <span class="tracy-dump-number">3</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">3</span>: <span class="tracy-dump-number">4</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">4</span>: <span class="tracy-dump-number">5</span>
<span class="tracy-dump-indent"> | </span>...
</div></div></pre>
', Dumper::toHtml([$arr, (object) $arr], [Dumper::ITEMS => 5]));
15 changes: 15 additions & 0 deletions tests/Tracy/Dumper.toHtml().lazy.auto.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ Assert::match(
</div></pre>',
Dumper::toHtml($obj)
);


// lazy dump & max items
$arr = [1, 2, 3, 4, 5, 6, 7, 8];
Assert::match('<pre class="tracy-dump" data-tracy-snapshot=\'[]\'><span class="tracy-toggle"><span class="tracy-dump-array">array</span> (2)</span>
<div><span class="tracy-dump-indent"> </span><span class="tracy-dump-key">0</span> => <span class="tracy-toggle tracy-collapsed" data-tracy-dump=\'{"array":[[0,1],[1,2],[2,3],[3,4],[4,5]],"length":8,"long":true}\'><span class="tracy-dump-array">array</span> (8)</span>
<span class="tracy-dump-indent"> </span><span class="tracy-dump-key">1</span> => <span class="tracy-toggle"><span class="tracy-dump-object">stdClass</span> <span class="tracy-dump-hash">#%d%</span></span>
<div><span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">0</span>: <span class="tracy-dump-number">1</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">1</span>: <span class="tracy-dump-number">2</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">2</span>: <span class="tracy-dump-number">3</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">3</span>: <span class="tracy-dump-number">4</span>
<span class="tracy-dump-indent"> | </span><span class="tracy-dump-key">4</span>: <span class="tracy-dump-number">5</span>
<span class="tracy-dump-indent"> | </span>...
</div></div></pre>
', Dumper::toHtml([$arr, (object) $arr], [Dumper::ITEMS => 5]));
16 changes: 16 additions & 0 deletions tests/Tracy/Dumper.toHtml().lazy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,19 @@ Assert::match(
'<pre class="tracy-dump" data-tracy-snapshot=\'{"%d%":{"name":"stdClass","items":[["a",{"object":%d%},0]]},"%d%":{"name":"stdClass","items":[["b",{"object":%d%},0]]},"%d%":{"name":"stdClass","items":[["c",{"object":%d%},0]]},"%d%":{"name":"stdClass","items":[["d",{"object":%d%},0]]},"%d%":{"name":"stdClass"}}\' data-tracy-dump=\'{"object":%d%}\'></pre>',
Dumper::toHtml($obj, $options)
);


// lazy dump & max string length
$arr = [str_repeat('x', 80)];
Assert::match(
'<pre class="tracy-dump" data-tracy-snapshot=\'[]\' data-tracy-dump=\'[[0,{"string":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ... ","length":80}]]\'></pre>',
Dumper::toHtml($arr, $options + [Dumper::TRUNCATE => 50])
);


// lazy dump & max items
$arr = [1, 2, 3, 4, 5, 6, 7, 8];
Assert::match(
'<pre class="tracy-dump" data-tracy-snapshot=\'{"14":{"name":"stdClass","items":[[0,1,0],[1,2,0],[2,3,0],[3,4,0],[4,5,0]],"long":true}}\' data-tracy-dump=\'[[0,{"array":[[0,1],[1,2],[2,3],[3,4],[4,5]],"length":8,"long":true}],[1,{"object":14}]]\'></pre>',
Dumper::toHtml([$arr, (object) $arr], $options + [Dumper::ITEMS => 5])
);
22 changes: 21 additions & 1 deletion tests/Tracy/Dumper.toText().depth.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* Test: Tracy\Dumper::toText() depth & truncate
* Test: Tracy\Dumper::toText() depth & truncate & items
*/

declare(strict_types=1);
Expand Down Expand Up @@ -54,3 +54,23 @@ Assert::match('array (4)
1 => stdClass #%a%
| 0: stdClass #%a% { ... }
', Dumper::toText($arr, [Dumper::DEPTH => 2, Dumper::TRUNCATE => 50]));


$arr = [1, 2, 3, 4, 5, 6];

Assert::match('array (2)
0 => array (6)
| 0 => 1
| 1 => 2
| 2 => 3
| 3 => 4
| 4 => 5
| ...
1 => stdClass #8
| 0: 1
| 1: 2
| 2: 3
| 3: 4
| 4: 5
| ...
', Dumper::toText([$arr, (object) $arr], [Dumper::ITEMS => 5]));

0 comments on commit c8f135c

Please sign in to comment.