Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9be6dea

Browse files
[VarDumper] Add an options builder to configure VarDumper's behaviour at runtime
1 parent4b33917 commit9be6dea

File tree

11 files changed

+470
-24
lines changed

11 files changed

+470
-24
lines changed

‎src/Symfony/Bundle/DebugBundle/Resources/config/services.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
useSymfony\Component\VarDumper\Command\Descriptor\HtmlDescriptor;
2323
useSymfony\Component\VarDumper\Command\ServerDumpCommand;
2424
useSymfony\Component\VarDumper\Dumper\CliDumper;
25+
useSymfony\Component\VarDumper\Dumper\ContextProvider\BacktraceContextProvider;
2526
useSymfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
2627
useSymfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
2728
useSymfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
@@ -87,6 +88,10 @@
8788
param('kernel.project_dir'),
8889
service('debug.file_link_formatter')->nullOnInvalid(),
8990
]),
91+
'backtrace' =>inline_service(BacktraceContextProvider::class)->args([
92+
null,
93+
service('var_dump.cloner'),
94+
]),
9095
],
9196
])
9297

@@ -111,6 +116,10 @@
111116
]),
112117
'request' =>inline_service(RequestContextProvider::class)->args([service('request_stack')]),
113118
'cli' =>inline_service(CliContextProvider::class),
119+
'backtrace' =>inline_service(BacktraceContextProvider::class)->args([
120+
null,
121+
service('var_dump.cloner'),
122+
]),
114123
],
115124
])
116125

‎src/Symfony/Bundle/DebugBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"symfony/dependency-injection":"^5.4|^6.0",
2222
"symfony/http-kernel":"^5.4|^6.0",
2323
"symfony/twig-bridge":"^5.4|^6.0",
24-
"symfony/var-dumper":"^5.4|^6.0"
24+
"symfony/var-dumper":"^6.3"
2525
},
2626
"require-dev": {
2727
"symfony/config":"^5.4|^6.0",

‎src/Symfony/Component/VarDumper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add caster for`WeakMap`
88
* Add support of named arguments to`dd()` and`dump()` to display the argument name
9+
* Add support of options when using`dd()` and`dump()`
910

1011
6.2
1112
---

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
namespaceSymfony\Component\VarDumper\Cloner;
1313

1414
useSymfony\Component\VarDumper\Caster\Caster;
15+
useSymfony\Component\VarDumper\Dumper\ContextProvider\BacktraceContextProvider;
1516
useSymfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
17+
useSymfony\Component\VarDumper\Dumper\VarDumperOptions;
1618

1719
/**
1820
* @author Nicolas Grekas <p@tchwork.com>
@@ -268,6 +270,7 @@ public function dump(DumperInterface $dumper)
268270
$refs = [0];
269271
$cursor =newCursor();
270272
$label =$this->context['label'] ??'';
273+
$options =$this->context['options'] ??newVarDumperOptions();
271274

272275
if ($cursor->attr =$this->context[SourceContextProvider::class] ?? []) {
273276
$cursor->attr['if_links'] =true;
@@ -279,6 +282,10 @@ public function dump(DumperInterface $dumper)
279282
}
280283

281284
$this->dumpItem($dumper,$cursor,$refs,$this->data[$this->position][$this->key]);
285+
286+
if (false !==$options->get(VarDumperOptions::TRACE) &&$cursor->attr =$this->context[BacktraceContextProvider::class] ?? []) {
287+
$this->dumpDebugBacktrace($dumper,$cursor);
288+
}
282289
}
283290

284291
/**
@@ -429,4 +436,14 @@ private function getStub(mixed $item)
429436

430437
return$stub;
431438
}
439+
440+
privatefunctiondumpDebugBacktrace(DumperInterface$dumper,Cursor$cursor):void
441+
{
442+
$backtrace =$cursor->attr['backtrace'];
443+
444+
$dumper->dumpScalar($cursor,'default','');
445+
$dumper->dumpScalar($cursor,'default','**DEBUG BACKTRACE**');
446+
447+
$backtrace->dump($dumper);
448+
}
432449
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarDumper\Dumper\ContextProvider;
13+
14+
useSymfony\Component\VarDumper\Caster\TraceStub;
15+
useSymfony\Component\VarDumper\Cloner\ClonerInterface;
16+
useSymfony\Component\VarDumper\Dumper\VarDumperOptions;
17+
18+
/**
19+
* Provides the debug stacktrace of the VarDumper call.
20+
*
21+
* @author Alexandre Daubois <alex.daubois@gmail.com>
22+
*/
23+
finalclass BacktraceContextProviderimplements ContextProviderInterface
24+
{
25+
privateconstBACKTRACE_CONTEXT_PROVIDER_DEPTH =4;
26+
27+
publicfunction__construct(
28+
privatereadonlybool|int$limit,
29+
privatereadonlyClonerInterface$cloner
30+
) {
31+
}
32+
33+
publicfunctiongetContext(): ?array
34+
{
35+
if (false ===$this->limit) {
36+
return [];
37+
}
38+
39+
$context = [];
40+
$traces =debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
41+
42+
for ($i =self::BACKTRACE_CONTEXT_PROVIDER_DEPTH;$i <\count($traces); ++$i) {
43+
if (VarDumperOptions::class === ($traces[$i +1]['class'] ??null)) {
44+
continue;
45+
}
46+
47+
$context[] =$traces[$i];
48+
49+
if ($this->limit ===\count($context)) {
50+
break;
51+
}
52+
}
53+
54+
$stub =newTraceStub($context);
55+
56+
return ['backtrace' =>$this->cloner->cloneVar($stub->value)];
57+
}
58+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarDumper\Dumper;
13+
14+
useSymfony\Component\VarDumper\Caster\ScalarStub;
15+
16+
/**
17+
* @author Alexandre Daubois <alex.daubois@gmail.com>
18+
*/
19+
class VarDumperOptions
20+
{
21+
publicconstFORMAT ='_format';
22+
publicconstTRACE ='_trace';
23+
publicconstMAX_ITEMS ='_max_items';
24+
publicconstMIN_DEPTH ='_min_depth';
25+
publicconstMAX_STRING ='_max_string';
26+
publicconstMAX_DEPTH ='_max_depth';
27+
publicconstMAX_ITEMS_PER_DEPTH ='_max_items_per_depth';
28+
publicconstTHEME ='_theme';
29+
publicconstFLAGS ='_flags';
30+
publicconstCHARSET ='_charset';
31+
32+
publicconstAVAILABLE_OPTIONS = [
33+
self::FORMAT,
34+
self::TRACE,
35+
self::MAX_ITEMS,
36+
self::MIN_DEPTH,
37+
self::MAX_STRING,
38+
self::MAX_DEPTH,
39+
self::MAX_ITEMS_PER_DEPTH,
40+
self::THEME,
41+
self::FLAGS,
42+
self::CHARSET,
43+
];
44+
45+
privatearray$options;
46+
47+
publicfunction__construct(array$options = [])
48+
{
49+
$this->options =array_filter(
50+
$options,
51+
staticfn (mixed$key):bool =>\in_array($key,self::AVAILABLE_OPTIONS),
52+
\ARRAY_FILTER_USE_KEY
53+
);
54+
}
55+
56+
/**
57+
* @template T
58+
*
59+
* @param T ...$vars
60+
*
61+
* @return T
62+
*/
63+
publicfunctiondump(mixed ...$vars):mixed
64+
{
65+
if (!$vars) {
66+
$vars = [newScalarStub('🐛')];
67+
}
68+
69+
returndump(...$vars +$this->options);
70+
}
71+
72+
publicfunctiondd(mixed ...$vars): never
73+
{
74+
dd(...$vars +$this->options);
75+
}
76+
77+
publicfunctionformat(?string$format):static
78+
{
79+
$this->options[self::FORMAT] =$format;
80+
81+
return$this;
82+
}
83+
84+
publicfunctiontrace(bool|int$trace =true):static
85+
{
86+
$this->options[self::TRACE] =$trace;
87+
88+
return$this;
89+
}
90+
91+
publicfunctionmaxItems(int$maxItems):static
92+
{
93+
$this->options[self::MAX_ITEMS] =$maxItems;
94+
95+
return$this;
96+
}
97+
98+
publicfunctionminDepth(int$minDepth):static
99+
{
100+
$this->options[self::MIN_DEPTH] =$minDepth;
101+
102+
return$this;
103+
}
104+
105+
publicfunctionmaxString(int$maxString):static
106+
{
107+
$this->options[self::MAX_STRING] =$maxString;
108+
109+
return$this;
110+
}
111+
112+
publicfunctionmaxDepth(int$maxDepth):static
113+
{
114+
$this->options[self::MAX_DEPTH] =$maxDepth;
115+
116+
return$this;
117+
}
118+
119+
publicfunctionmaxItemsPerDepth(int$maxItemsPerDepth):static
120+
{
121+
$this->options[self::MAX_ITEMS_PER_DEPTH] =$maxItemsPerDepth;
122+
123+
return$this;
124+
}
125+
126+
publicfunctiontheme(?string$theme):static
127+
{
128+
$this->options[self::THEME] =$theme ??'dark';
129+
130+
return$this;
131+
}
132+
133+
/**
134+
* @param AbstractDumper::DUMP_* $flags
135+
*/
136+
publicfunctionflags(int$flags):static
137+
{
138+
$this->options[self::FLAGS] =$flags;
139+
140+
return$this;
141+
}
142+
143+
/**
144+
* Display arrays with short form (omitting elements count and `array` prefix).
145+
*/
146+
publicfunctionshowLightArray():static
147+
{
148+
$this->options[self::FLAGS] = ($this->options[self::FLAGS] ??0) | AbstractDumper::DUMP_LIGHT_ARRAY;
149+
150+
return$this;
151+
}
152+
153+
/**
154+
* Display string lengths, just before its value.
155+
*/
156+
publicfunctionshowStringLength():static
157+
{
158+
$this->options[self::FLAGS] = ($this->options[self::FLAGS] ??0) | AbstractDumper::DUMP_STRING_LENGTH;
159+
160+
return$this;
161+
}
162+
163+
/**
164+
* Display a comma at the end of the line of an array element.
165+
*/
166+
publicfunctionshowCommaSeparator():static
167+
{
168+
$this->options[self::FLAGS] = ($this->options[self::FLAGS] ??0) | AbstractDumper::DUMP_COMMA_SEPARATOR;
169+
170+
return$this;
171+
}
172+
173+
/**
174+
* Display a trailing comma after the last element of an array.
175+
*/
176+
publicfunctionshowTrailingComma():static
177+
{
178+
$this->options[self::FLAGS] = ($this->options[self::FLAGS] ??0) | AbstractDumper::DUMP_TRAILING_COMMA;
179+
180+
return$this;
181+
}
182+
183+
publicfunctioncharset(string$charset):static
184+
{
185+
$this->options[self::CHARSET] =$charset;
186+
187+
return$this;
188+
}
189+
190+
publicfunctionget(string$option):mixed
191+
{
192+
return$this->options[$option] ??null;
193+
}
194+
195+
publicfunctiontoArray():array
196+
{
197+
return$this->options;
198+
}
199+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp