8000 Use VarDumper in the Form data collector · symfony/symfony@fade0ce · GitHub
[go: up one dir, main page]

Skip to content

Commit fade0ce

Browse files
committed
Use VarDumper in the Form data collector
1 parent 6955199 commit fade0ce

File tree

2 files changed

+73
-62
lines changed

2 files changed

+73
-62
lines changed

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/form.html.twig

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@
474474
</a>
475475
</h3>
476476

477-
<table id="{{ data.id }}-errors">
477+
<table id="{{ data.id }}-errors" class="dump-inline">
478478
<thead>
479479
<tr>
480480
<th>Message</th>
@@ -501,21 +501,7 @@
501501
<span class="newline">Caused by:</span>
502502
{% endif %}
503503

504-
{% if trace.root is defined %}
505-
<strong class="newline">{{ trace.class }}</strong>
506-
<pre>
507-
{{- trace.root -}}
508-
{%- if trace.path is not empty -%}
509-
{%- if trace.path|first != '[' %}.{% endif -%}
510-
{{- trace.path -}}
511-
{%- endif %} = {{ trace.value -}}
512-
</pre>
513-
{% elseif trace.message is defined %}
514-
<strong class="newline">{{ trace.class }}</strong>
515-
<pre>{{ trace.message }}</pre>
516-
{% else %}
517-
<pre>{{ trace }}</pre>
518-
{% endif %}
504+
{{ profiler_dump(trace) }}
519505
{% else %}
520506
<em>Unknown.</em>
521507
{% endfor %}
@@ -535,7 +521,7 @@
535521
</h3>
536522

537523
<div id="{{ data.id }}-default_data">
538-
<table>
524+
<table class="dump-inline">
539525
<thead>
540526
<tr>
541527
<th width="180">Property</th>
@@ -547,21 +533,21 @@
547533
<th class="font-normal" scope="row">Model Format</th>
548534
<td>
549535
{% if data.default_data.model is defined %}
550-
{{ data.default_data.model }}
536+
{{ profiler_dump(data.default_data.model) }}
551537
{% else %}
552538
<em class="font-normal text-muted">same as normalized format</em>
553539
{% endif %}
554540
</td>
555541
</tr>
556542
<tr>
557543
<th class="font-normal" scope="row">Normalized Format</th>
558-
<td>{{ data.default_data.norm }}</td>
544+
<td>{{ profiler_dump(data.default_data.norm) }}</td>
559545
</tr>
560546
<tr>
561547
<th class="font-normal" scope="row">View Format</th>
562548
<td>
563549
{% if data.default_data.view is defined %}
564-
{{ data.default_data.view }}
550+
{{ profiler_dump(data.default_data.view) }}
565551
{% else %}
566552
<em class="font-normal text-muted">same as normalized format</em>
567553
{% endif %}
@@ -581,7 +567,7 @@
581567

582568
<div id="{{ data.id }}-submitted_data">
583569
{% if data.submitted_data.norm is defined %}
584-
<table>
570+
<table class="dump-inline">
585571
<thead>
586572
<tr>
587573
<th width="180">Property</th>
@@ -593,21 +579,21 @@
593579
<th class="font-normal" scope="row">View Format</th>
594580
<td>
595581
{% if data.submitted_data.view is defined %}
596-
{{ data.submitted_data.view }}
582+
{{ profiler_dump(data.submitted_data.view) }}
597583
{% else %}
598584
<em class="font-normal text-muted">same as normalized format</em>
599585
{% endif %}
600586
</td>
601587
</tr>
602588
<tr>
603589
<th class="font-normal" scope="row">Normalized Format</th>
604-
<td>{{ data.submitted_data.norm }}</td>
590+
<td>{{ profiler_dump(data.submitted_data.norm) }}</td>
605591
</tr>
606592
<tr>
607593
<th class="font-normal" scope="row">Model Format</th>
608594
<td>
609595
{% if data.submitted_data.model is defined %}
610-
{{ data.submitted_data.model }}
596+
{{ profiler_dump(data.submitted_data.model) }}
611597
{% else %}
612598
<em class="font-normal text-muted">same as normalized format</em>
613599
{% endif %}
@@ -632,7 +618,7 @@
632618

633619
<div id="{{ data.id }}-passed_options">
634620
{% if data.passed_options|length %}
635-
<table>
621+
<table class="dump-inline">
636622
<thead>
637623
<tr>
638624
<th width="180">Option</th>
@@ -644,12 +630,12 @@
644630
{% for option, value in data.passed_options %}
645631
<tr>
646632
<th>{{ option }}</th>
647-
<td>{{ value }}</td>
633+
<td>{{ profiler_dump(value) }}</td>
648634
<td>
649-
{% if data.resolved_options[option] is same as(value) %}
635+
{% if data.resolved_options[option] == value %}
650636
<em class="font-normal text-muted">same as passed value</em>
651637
{% else %}
652-
{{ data.resolved_options[option] }}
638+
{{ profiler_dump(data.resolved_options[option]) }}
653639
{% endif %}
654640
</td>
655641
</tr>
@@ -672,7 +658,7 @@
672658
</h3>
673659

674660
<div id="{{ data.id }}-resolved_options" class="hidden">
675-
<table>
661+
<table class="dump-inline">
676662
<thead>
677663
<tr>
678664
<th width="180">Option</th>
@@ -683,7 +669,7 @@
683669
{% for option, value in data.resolved_options %}
684670
<tr>
685671
<th scope="row">{{ option }}</th>
686-
<td>{{ value }}</td>
672+
<td>{{ profiler_dump(value) }}</td>
687673
</tr>
688674
{% endfor %}
689675
</tbody>
@@ -699,7 +685,7 @@
699685
</h3>
700686

701687
<div id="{{ data.id }}-view_vars" class="hidden">
702-
<table>
688+
<table class="dump-inline">
703689
<thead>
704690
<tr>
705691
<th width="180">Variable</th>
@@ -710,7 +696,7 @@
710696
{% for variable, value in data.view_vars %}
711697
<tr>
712698
<th scope="row">{{ variable }}</th>
713-
<td>{{ value }}</td>
699+
<td>{{ profiler_dump(value) }}</td>
714700
</tr>
715701
{% endfor %}
716702
</tbody>

src/Symfony/Component/Form/Extension/DataCollector/FormDataExtractor.php

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
use Symfony\Component\Form\FormInterface;
1515
use Symfony\Component\Form\FormView;
1616
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
17+
use Symfony\Component\Validator\ConstraintViolation;
1718
use Symfony\Component\Validator\ConstraintViolationInterface;
19+
use Symfony\Component\VarDumper\Cloner\Data;
20+
use Symfony\Component\VarDumper\Cloner\VarCloner;
21+
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
1822

1923
/**
2024
* Default implementation of {@link FormDataExtractorInterface}.
@@ -24,16 +28,27 @@
2428
class FormDataExtractor implements FormDataExtractorInterface
2529
{
2630
/**
27-
* @var ValueExporter
31+
* @var VarCloner
2832
*/
29-
private $valueExporter;
33+
private $varCloner;
3034

35+
private $varExporter;
3136
/**
3237
* Constructs a new data extractor.
38+
*
39+
* @param VarCloner|null $varCloner Passing a ValueExporter instance is
40+
* deprecated since version 3.2 and will
41+
* not be possible in 4.0.
3342
*/
34-
public function __construct(ValueExporter $valueExporter = null)
43+
public function __construct($varCloner = null)
3544
{
36-
$this->valueExporter = $valueExporter ?: new ValueExporter();
45+
if ($varCloner instanceof ValueExporter) {
46+
@trigger_error('Passing a ValueExporter instance to '.__METHOD__.'() is deprecated in version 3.2 and will be removed in 4.0. Pass a ClonerInterface instance instead.', E_USER_DEPRECATED);
47+
$varCloner = null;
48+
}
49+
50+
$this->valueExporter = new ValueExporter();
51+
$this->varCloner = $varCloner ?: new VarCloner();
3752
}
3853

3954
/**
@@ -45,17 +60,17 @@ public function extractConfiguration(FormInterface $form)
4560
'id' => $this->buildId($form),
4661
'name' => $form->getName(),
4762
'type_class' => get_class($form->getConfig()->getType()->getInnerType()),
48-
'synchronized' => $this->valueExporter->exportValue($form->isSynchronized()),
63+
'synchronized' => $this->cloneVar($form->isSynchronized()),
4964
'passed_options' => array(),
5065
'resolved_options' => array(),
5166
);
5267

5368
foreach ($form->getConfig()->getAttribute('data_collector/passed_options', array()) as $option => $value) {
54-
$data['passed_options'][$option] = $this->valueExporter->exportValue($value);
69+
$data['passed_options'][$option] = $this->cloneVar($value);
5570
}
5671

5772
foreach ($form->getConfig()->getOptions() as $option => $value) {
58-
$data['resolved_options'][$option] = $this->valueExporter->exportValue($value);
73+
$data['resolved_options'][$option] = $this->cloneVar($value);
5974
}
6075

6176
ksort($data['passed_options']);
@@ -71,17 +86,17 @@ public function extractDefaultData(FormInterface $form)
7186
{
7287
$data = array(
7388
'default_data' => array(
74-
'norm' => $this->valueExporter->exportValue($form->getNormData()),
89+
'norm' => $this->cloneVar($form->getNormData()),
7590
),
7691
'submitted_data' => array(),
7792
);
7893

7994
if ($form->getData() !== $form->getNormData()) {
80-
$data['default_data']['model'] = $this->valueExporter->exportValue($form->getData());
95+
$data['default_data']['model'] = $this->cloneVar($form->getData());
8196
}
8297

8398
if ($form->getViewData() !== $form->getNormData()) {
84-
$data['default_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
99+
$data['default_data']['view'] = $this->cloneVar($form->getViewData());
85100
}
86101

87102
return $data;
@@ -94,17 +109,17 @@ public function extractSubmittedData(FormInterface $form)
94109
{
95110
$data = array(
96111
'submitted_data' => array(
97-
'norm' => $this->valueExporter->exportValue($form->getNormData()),
112+
'norm' => $this->cloneVar($form->getNormData()),
98113
),
99114
'errors' => array(),
100115
);
101116

102117
if ($form->getViewData() !== $form->getNormData()) {
103-
$data['submitted_data']['view'] = $this->valueExporter->exportValue($form->getViewData());
118+
$data['submitted_data']['view'] = $this->cloneVar($form->getViewData());
104119
}
105120

106121
if ($form->getData() !== $form->getNormData()) {
107-
$data['submitted_data']['model'] = $this->valueExporter->exportValue($form->getData());
122+
$data['submitted_data']['model'] = $this->cloneVar($form->getData());
108123
}
109124

110125
foreach ($form->getErrors() as $error) {
@@ -119,39 +134,29 @@ public function extractSubmittedData(FormInterface $form)
119134
$cause = $error->getCause();
120135

121136
while (null !== $cause) {
122-
if ($cause instanceof ConstraintViolationInterface) {
123-
$errorData['trace'][] = array(
124-
'class' => $this->valueExporter->exportValue(get_class($cause)),
125-
'root' => $this->valueExporter->exportValue($cause->getRoot()),
126-
'path' => $this->valueExporter->exportValue($cause->getPropertyPath()),
127-
'value' => $this->valueExporter->exportValue($cause->getInvalidValue()),
128-
);
137+
$levelCause = new ConstraintViolation($cause->getMessage(), $cause->getMessageTemplate(), $cause->getParameters(), $cause->getRoot(), $cause->getPropertyPath(), $cause->getInvalidValue(), $cause->getPlural(), $cause->getCode(), $cause->getConstraint());
138+
139+
$errorData['trace'][] = $this->cloneVar($levelCause);
129140

141+
if ($cause instanceof ConstraintViolationInterface) {
130142
$cause = method_exists($cause, 'getCause') ? $cause->getCause() : null;
131143

132144
continue;
133145
}
134146

135147
if ($cause instanceof \Exception) {
136-
$errorData['trace'][] = array(
137-
'class' => $this->valueExporter->exportValue(get_class($cause)),
138-
'message' => $this->valueExporter->exportValue($cause->getMessage()),
139-
);
140-
141-
$cause = $cause->getPrevious();
148+
$cause = $this->cloneVar($cause->getPrevious());
142149

143150
continue;
144151
}
145152

146-
$errorData['trace'][] = $cause;
147-
148153
break;
149154
}
150155

151156
$data['errors'][] = $errorData;
152157
}
153158

154-
$data['synchronized'] = $this->valueExporter->exportValue($form->isSynchronized());
159+
$data['synchronized'] = $this->cloneVar($form->isSynchronized());
155160

156161
return $data;
157162
}
@@ -174,7 +179,7 @@ public function extractViewVariables(FormView $view)
174179
}
175180

176181
foreach ($view->vars as $varName => $value) {
177-
$data['view_vars'][$varName] = $this->valueExporter->exportValue($value);
182+
$data['view_vars'][$varName] = $this->cloneVar($value);
178183
}
179184

180185
ksort($data['view_vars']);
@@ -199,4 +204,24 @@ private function buildId(FormInterface $form)
199204

200205
return $id;
201206
}
207+
208+
/**
209+
* Converts the variable into a serializable Data instance.
210+
*
211+
* This array can be displayed in the template using
212+
* the VarDumper component.
213+
*
214+
* @param mixed $var
215+
*
216+
* @return Data
217+
*/
218+
private function cloneVar($var)
219+
{
220+
$traceArgs = ExceptionCaster::$traceArgs;
221+
ExceptionCaster::$traceArgs = false;
222+
$data = $this->varCloner->cloneVar($var);
223+
ExceptionCaster::$traceArgs = $traceArgs;
224+
225+
return $data;
226+
}
202227
}

0 commit comments

Comments
 (0)
0