8000 feature #19672 [VarDumper] Allow dumping subparts of cloned Data stru… · symfony/symfony@640c620 · GitHub
[go: up one dir, main page]

Skip to content

Commit 640c620

Browse files
committed
feature #19672 [VarDumper] Allow dumping subparts of cloned Data structures (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [VarDumper] Allow dumping subparts of cloned Data structures | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | License | MIT | Doc PR | symfony/symfony-docs#6891 ping @wouterj: with this, we'll be able to dump only the trace for deprecations in #19614 instead of being forced to dump the full exception right now. See test case. We'd do `{{ profiler_dump(log.context.seek('trace')) }}` in `logger.html.twig`. Commits ------- 8f2f440 [VarDumper] Allow dumping subparts of cloned Data structures
2 parents 9c52f49 + 8f2f440 commit 640c620

File tree

4 files changed

+91
-4
lines changed

4 files changed

+91
-4
lines changed

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 <p@tchwork.com>
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)
0