Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Symfony\Component\VarDumper\Cloner;

use Symfony\Component\VarDumper\Caster\Caster;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class Data
{
private $data;
private $position = 0;
private $key = 0;
private $maxDepth = 20;
private $maxItemsPerDepth = -1;
private $useRefHandles = -1;
Expand Down Expand Up @@ -82,13 +86,57 @@ public function withRefHandles($useRefHandles)
return $data;
}

/**
* Seeks to a specific key in nested data structures.
*
* @param string|int $key The key to seek to
*
* @return self|null A clone of $this of null if the key is not set
*/
public function seek($key)
{
$item = $this->data[$this->position][$this->key];

if (!$item instanceof Stub || !$item->position) {
return;
}
$keys = array($key);

switch ($item->type) {
case Stub::TYPE_OBJECT:
$keys[] = Caster::PREFIX_DYNAMIC.$key;
$keys[] = Caster::PREFIX_PROTECTED.$key;
$keys[] = Caster::PREFIX_VIRTUAL.$key;
$keys[] = "\0$item->class\0$key";
case Stub::TYPE_ARRAY:
case Stub::TYPE_RESOURCE:
break;
default:
return;
}

$data = null;
$children = $this->data[$item->position];

foreach ($keys as $key) {
if (isset($children[$key]) || array_key_exists($key, $children)) {
$data = clone $this;
$data->key = $key;
$data->position = $item->position;
break;
}
}

return $data;
}

/**
* Dumps data with a DumperInterface dumper.
*/
public function dump(DumperInterface $dumper)
{
$refs = array(0);
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function assertDumpMatchesFormat($dump, $data, $message = '')
$this->assertStringMatchesFormat(rtrim($dump), $this->getDump($data), $message);
}

protected function getDump($data)
protected function getDump($data, $key = null)
{
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
Expand All @@ -39,7 +39,11 @@ protected function getDump($data)
$cloner->setMaxItems(-1);
$dumper = new CliDumper($h, null, $flags);
$dumper->setColors(false);
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
$data = $cloner->cloneVar($data)->withRefHandles(false);
if (null !== $key && null === $data = $data->seek($key)) {
return;
}
$dumper->dump($data);
$data = stream_get_contents($h, -1, 0);
fclose($h);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ public function testDefaultSettings()
$this->assertDumpMatchesFormat($expectedDump, $e);
}

public function testSeek()
{
$e = $this->getTestException(2);

$expectedDump = <<<'EODUMP'
{
%d. %sExceptionCasterTest.php:23: {
22: {
23: return new \Exception('foo');
24: }
}
%d. %sExceptionCasterTest.php:%d: {
%d: {
%d: $e = $this->getTestException(2);
%d:
args: {
2
}
}
%A
EODUMP;

$this->assertStringMatchesFormat($expectedDump, $this->getDump($e, 'trace'));
}

public function testNoArgs()
{
$e = $this->getTestException(1);
Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/VarDumper/Tests/VarClonerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public function testMaxIntBoundary()

)

[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
Expand Down Expand Up @@ -126,6 +128,8 @@ public function testClone()

)

[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
Expand All @@ -143,7 +147,7 @@ public function testJsonCast()
$clone = $cloner->cloneVar($data);

$expected = <<<'EOTXT'
object(Symfony\Component\VarDumper\Cloner\Data)#%i (4) {
object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
array(2) {
[0]=>
Expand Down Expand Up @@ -187,6 +191,10 @@ public function testJsonCast()
}
}
}
["position":"Symfony\Component\VarDumper\Cloner\Data":private]=>
int(0)
["key":"Symfony\Component\VarDumper\Cloner\Data":private]=>
int(0)
["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
int(20)
["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
Expand Down Expand Up @@ -242,6 +250,8 @@ public function testCaster()

)

[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
Expand Down