Skip to content

Commit

Permalink
[VarDumper] with-er interface for Cloner\Data
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 23, 2015
1 parent ce8d371 commit 2462c5b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
$dumps = array();

foreach ($this->data as $dump) {
$dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth));
if (method_exists($dump['data'], 'withMaxDepth')) {
$dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
} else {
// getLimitedClone is @deprecated, to be removed in 3.0
$dumper->dump($dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth));
}
rewind($data);
$dump['data'] = stream_get_contents($data);
ftruncate($data, 0);
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,51 @@ public function getRawData()
return $this->data;
}

/**
* Returns a depth limited clone of $this.
*
* @param int $maxDepth The max dumped depth level.
*
* @return self A clone of $this.
*/
public function withMaxDepth($maxDepth)
{
$data = clone $this;
$data->maxDepth = (int) $maxDepth;

return $data;
}

/**
* Limits the numbers of elements per depth level.
*
* @param int $maxItemsPerDepth The max number of items dumped per depth level.
*
* @return self A clone of $this.
*/
public function withMaxItemsPerDepth($maxItemsPerDepth)
{
$data = clone $this;
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;

return $data;
}

/**
* Enables/disables objects' identifiers tracking.
*
* @param bool $useRefHandles False to hide global ref. handles.
*
* @return self A clone of $this.
*/
public function withRefHandles($useRefHandles)
{
$data = clone $this;
$data->useRefHandles = $useRefHandles ? -1 : 0;

return $data;
}

/**
* Returns a depth limited clone of $this.
*
Expand All @@ -45,9 +90,13 @@ public function getRawData()
* @param bool $useRefHandles False to hide ref. handles.
*
* @return self A depth limited clone of $this.
*
* @deprecated since Symfony 2.7, to be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles instead.
*/
public function getLimitedClone($maxDepth, $maxItemsPerDepth, $useRefHandles = true)
{
trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.7 and will be removed in 3.0. Use withMaxDepth, withMaxItemsPerDepth or withRefHandles methods instead.', E_USER_DEPRECATED);

$data = clone $this;
$data->maxDepth = (int) $maxDepth;
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function testBuggyRefs()
$dumper->setColors(false);
$cloner = new VarCloner();

$data = $cloner->cloneVar($var)->getLimitedClone(3, -1);
$data = $cloner->cloneVar($var)->withMaxDepth(3);
$out = '';
$dumper->dump($data, function ($line, $depth) use (&$out) {
if ($depth >= 0) {
Expand Down

0 comments on commit 2462c5b

Please sign in to comment.