Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e1f204

Browse files
[VarDumper] Allow dumping subparts of cloned Data structures
1 parent 58cdb04 commit 4e1f204

File tree

5 files changed

+92
-9
lines changed

5 files changed

+92
-9
lines changed

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,7 @@ public function testDump()
4949
);
5050
$this->assertEquals($xDump, $dump);
5151

52-
$this->assertStringMatchesFormat(
53-
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":4:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:%i;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:%i;s:54:"Symfony\Component\VarDumper\Cloner\DatauseRefHandles";i:%i;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:%a',
54-
str_replace("\0", '', $collector->serialize())
55-
);
56-
52+
$this->assertStringMatchesFormat('a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
5753
$this->assertSame(0, $collector->getDumpsCount());
5854
$this->assertSame('a:0:{}', $collector->serialize());
5955
}

src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Symfony\Component\VarDumper\Cloner;
1313

14+
use Symfony\Component\VarDumper\Caster\Caster;
15+
1416
/**
1517
* @author Nicolas Grekas <[email protected]>
1618
*/
1719
class Data
1820
{
1921
private $data;
22+
private $position = 0;
23+
private $key = 0;
2024
private $maxDepth = 20;
2125
private $maxItemsPerDepth = -1;
2226
private $useRefHandles = -1;
@@ -82,13 +86,57 @@ public function withRefHandles($useRefHandles)
8286
return $data;
8387
}
8488

89+
/**
90+
* Seeks to a specific key in nested data structures.
91+
*
92+
* @param string|int $key The key to seek to
93+
*
94+
* @return self|null A clone of $this of null if the key is not set
95+
*/
96+
public function seek($key)
97+
{
98+
$item = $this->data[$this->position][$this->key];
99+
100+
if (!$item instanceof Stub || !$item->position) {
101+
return;
102+
}
103+
$keys = array($key);
104+
105+
switch ($item->type) {
106+
case Stub::TYPE_OBJECT:
107+
$keys[] = Caster::PREFIX_DYNAMIC.$key;
108+
$keys[] = Caster::PREFIX_PROTECTED.$key;
109+
$keys[] = Caster::PREFIX_VIRTUAL.$key;
110+
$keys[] = "\0$item->class\0$key";
111+
case Stub::TYPE_ARRAY:
112+
case Stub::TYPE_RESOURCE:
113+
break;
114+
default:
115+
return;
116+
}
117+
118+
$data = null;
119+
$children = $this->data[$item->position];
120+
121+
foreach ($keys as $key) {
122+
if (isset($children[$key]) || array_key_exists($key, $children)) {
123+
$data = clone $this;
124+
$data->key = $key;
125+
$data->position = $item->position;
126+
break;
127+
}
128+
}
129+
130+
return $data;
131+
}
132+
85133
/**
86134
* Dumps data with a DumperInterface dumper.
87135
*/
88136
public function dump(DumperInterface $dumper)
89137
{
90138
$refs = array(0);
91-
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[0][0]);
139+
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
92140
}
93141

94142
/**

src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function assertDumpMatchesFormat($dump, $data, $message = '')
2929
$this->assertStringMatchesFormat(rtrim($dump), $this->getDump($data), $message);
3030
}
3131

32-
protected function getDump($data)
32+
protected function getDump($data, $key = null)
3333
{
3434
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
3535
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
@@ -39,7 +39,11 @@ protected function getDump($data)
3939
$cloner->setMaxItems(-1);
4040
$dumper = new CliDumper($h, null, $flags);
4141
$dumper->setColors(false);
42-
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
42+
$data = $cloner->cloneVar($data)->withRefHandles(false);
43+
if (null !== $key && null === $data = $data->seek($key)) {
44+
return;
45+
}
46+
$dumper->dump($data);
4347
$data = stream_get_contents($h, -1, 0);
4448
fclose($h);
4549

src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ public function testDefaultSettings()
5959
$this->assertDumpMatchesFormat($expectedDump, $e);
6060
}
6161

62+
public function testSeek()
63+
{
64+
$e = $this->getTestException(2);
65+
66+
$expectedDump = <<<'EODUMP'
67+
{
68+
%d. %sExceptionCasterTest.php:23: {
69+
22: {
70+
23: return new \Exception('foo');
71+
24: }
72+
}
73+
%d. %sExceptionCasterTest.php:%d: {
74+
%d: {
75+
%d: $e = $this->getTestException(2);
76+
%d:
77+
args: {
78+
2
79+
}
80+
}
81+
%A
82+
EODUMP;
83+
84+
$this->assertStringMatchesFormat($expectedDump, $this->getDump($e, 'trace'));
85+
}
86+
6287
public function testNoArgs()
6388
{
6489
$e = $this->getTestException(1);

src/Symfony/Component/VarDumper/Tests/VarClonerTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function testMaxIntBoundary()
5252
5353
)
5454
55+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
56+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
5557
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
5658
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
5759
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
@@ -126,6 +128,8 @@ public function testClone()
126128
127129
)
128130
131+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
132+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
129133
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
130134
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
131135
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1
@@ -143,7 +147,7 @@ public function testJsonCast()
143147
$clone = $cloner->cloneVar($data);
144148

145149
$expected = <<<'EOTXT'
146-
object(Symfony\Component\VarDumper\Cloner\Data)#%i (4) {
150+
object(Symfony\Component\VarDumper\Cloner\Data)#%i (6) {
147151
["data":"Symfony\Component\VarDumper\Cloner\Data":private]=>
148152
array(2) {
149153
[0]=>
@@ -187,6 +191,10 @@ public function testJsonCast()
187191
}
188192
}
189193
}
194+
["position":"Symfony\Component\VarDumper\Cloner\Data":private]=>
195+
int(0)
196+
["key":"Symfony\Component\VarDumper\Cloner\Data":private]=>
197+
int(0)
190198
["maxDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
191199
int(20)
192200
["maxItemsPerDepth":"Symfony\Component\VarDumper\Cloner\Data":private]=>
@@ -242,6 +250,8 @@ public function testCaster()
242250
243251
)
244252
253+
[position:Symfony\Component\VarDumper\Cloner\Data:private] => 0
254+
[key:Symfony\Component\VarDumper\Cloner\Data:private] => 0
245255
[maxDepth:Symfony\Component\VarDumper\Cloner\Data:private] => 20
246256
[maxItemsPerDepth:Symfony\Component\VarDumper\Cloner\Data:private] => -1
247257
[useRefHandles:Symfony\Component\VarDumper\Cloner\Data:private] => -1

0 commit comments

Comments
 (0)