8000 [VarDumper] Allow seamless use of Data clones · symfony/symfony@b8bc469 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8bc469

Browse files
[VarDumper] Allow seamless use of Data clones
1 parent 42de145 commit b8bc469

32 files changed

+384
-207
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,19 @@ public function collect(Request $request, Response $response, \Exception $except
109109
$this->data = array(
110110
'enabled' => true,
111111
'authenticated' => $token->isAuthenticated(),
112-
'token' => $this->cloneVar($token),
112+
'token' => $token,
113113
'token_class' => get_class($token),
114114
'logout_url' => $logoutUrl,
115115
'user' => $token->getUsername(),
116-
'roles' => $this->cloneVar(array_map(function (RoleInterface $role) { return $role->getRole(); }, $assignedRoles)),
117-
'inherited_roles' => $this->cloneVar(array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles)),
116+
'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $assignedRoles),
117+
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
118118
'supports_role_hierarchy' => null !== $this->roleHierarchy,
119119
);
120120
}
121121

122122
// collect voters and access decision manager information
123123
if ($this->accessDecisionManager instanceof TraceableAccessDecisionManager) {
124-
$this->data['access_decision_log'] = array_map(function ($decision) {
125-
$decision['object'] = $this->cloneVar($decision['object']);
126-
127-
return $decision;
128-
}, $this->accessDecisionManager->getDecisionLog());
129-
124+
$this->data['access_decision_log'] = $this->accessDecisionManager->getDecisionLog();
130125
$this->data['voter_strategy'] = $this->accessDecisionManager->getStrategy();
131126

132127
foreach ($this->accessDecisionManager->getVoters() as $voter) {
@@ -155,10 +150,12 @@ public function collect(Request $request, Response $response, \Exception $except
155150
'access_denied_handler' => $firewallConfig->getAccessDeniedHandler(),
156151
'access_denied_url' => $firewallConfig->getAccessDeniedUrl(),
157152
'user_checker' => $firewallConfig->getUserChecker(),
158-
'listeners' => $this->cloneVar($firewallConfig->getListeners()),
153+
'listeners' => $firewallConfig->getListeners(),
159154
);
160155
}
161156
}
157+
158+
$this->data = $this->cloneVar($this->data);
162159
}
163160

164161
/**

src/Symfony/Bundle/SecurityBundle/Tests/DataCollector/SecurityDataCollectorTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,10 @@ public function testCollectAuthenticationTokenAndRoles(array $roles, array $norm
6666

6767
$this->assertTrue($collector->isEnabled());
6868
$this->assertTrue($collector->isAuthenticated());
69-
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass());
69+
$this->assertSame('Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken', $collector->getTokenClass()->getValue());
7070
$this->assertTrue($collector->supportsRoleHierarchy());
71-
$this->assertSame($normalizedRoles, $collector->getRoles()->getRawData()[1]);
72-
if ($inheritedRoles) {
73-
$this->assertSame($inheritedRoles, $collector->getInheritedRoles()->getRawData()[1]);
74-
} else {
75-
$this->assertSame($inheritedRoles, $collector->getInheritedRoles()->getRawData()[0][0]);
76-
}
71+
$this->assertSame($normalizedRoles, $collector->getRoles()->getValue(true));
72+
$this->assertSame($inheritedRoles, $collector->getInheritedRoles()->getValue(true));
7773
$this->assertSame('hhamon', $collector->getUser());
7874
}
7975

@@ -107,7 +103,7 @@ public function testGetFirewall()
107103
$this->assertSame($firewallConfig->getAccessDeniedHandler(), $collected['access_denied_handler']);
108104
$this->assertSame($firewallConfig->getAccessDeniedUrl(), $collected['access_denied_url']);
109105
$this->assertSame($firewallConfig->getUserChecker(), $collected['user_checker']);
110-
$this->assertSame($firewallConfig->getListeners(), $collected['listeners']->getRawData()[0][0]);
106+
$this->assertSame($firewallConfig->getListeners(), $collected['listeners']->getValue());
111107
}
112108

113109
public function testGetFirewallReturnsNull()

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@
3636
"symfony/twig-bridge": "~2.8|~3.0",
3737
"symfony/process": "~2.8|~3.0",
3838
"symfony/validator": "~2.8|~3.0",
39-
"symfony/var-dumper": "~3.2",
39+
"symfony/var-dumper": "~3.3",
4040
"symfony/yaml": "~2.8|~3.0",
4141
"symfony/expression-language": "~2.8|~3.0",
4242
"doctrine/doctrine-bundle": "~1.4",
4343
"twig/twig": "~1.28|~2.0"
4444
},
45+
"conflict": {
46+
"symfony/var-dumper": "<3.3"
47+
},
4548
"suggest": {
4649
"symfony/security-acl": "For using the ACL functionality of this bundle"
4750
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
<div class="metric">
9191
<span class="value">
9292
{% if key == 'time' %}
93-
{{ '%0.2f'|format(1000*value) }} ms
93+
{{ '%0.2f'|format(1000*value.value) }} ms
9494
{% else %}
9595
{{ value }}
9696
{% endif %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@
267267
{% for name in collector.bundles|keys|sort %}
268268
<tr>
269269
<th scope="row" class="font-normal">{{ name }}</th>
270-
<td class="font-normal">{{ collector.bundles[name] }}</td>
270+
<td class="font-normal">{{ profiler_dump(collector.bundles[name]) }}</td>
271271
</tr>
272272
{% endfor %}
273273
</tbody>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
<tr>
7777
<td class="text-right">{{ listener.priority|default('-') }}</td>
78-
<td class="font-normal">{{ profiler_dump(listener.data) }}</td>
78+
<td class="font-normal">{{ profiler_dump(listener.stub) }}</td>
7979
</tr>
8080

8181
{% if loop.last %}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
<a class="btn btn-link text-small sf-toggle" data-toggle-selector="#{{ context_id }}" data-toggle-alt-content="Hide trace">Show trace</a>
183183

184184
<div id="{{ context_id }}" class="context sf-toggle-content sf-toggle-hidden">
185-
{{ profiler_dump(log.context.seek('exception').seek('\0Exception\0trace'), maxDepth=2) }}
185+
{{ profiler_dump(log.context.exception['\0Exception\0trace'], maxDepth=2) }}
186186
</div>
187187
</span>
188188
{% elseif log.context is defined and log.context is not empty %}

src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public function dumpLog(\Twig_Environment $env, $message, Data $context)
9898
}
9999

100100
$replacements = array();
101-
foreach ($context->getRawData()[1] as $k => $v) {
102-
$v = '{'.twig_escape_filter($env, $k).'}';
103-
$replacements['&quot;'.$v.'&quot;'] = $replacements[$v] = $this->dumpData($env, $context->seek($k));
101+
foreach ($context as $k => $v) {
102+
$k = '{'.twig_escape_filter($env, $k).'}';
103+
$replacements['&quot;'.$k.'&quot;'] = $replacements[$k] = $this->dumpData($env, $v);
104104
}
105105

106106
return '<span class="dump-inline">'.strtr($message, $replacements).'</span>';

src/Symfony/Bundle/WebProfilerBundle/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"symfony/routing": "~2.8|~3.0",
2323
"symfony/twig-bridge": "~2.8|~3.0",
2424
"twig/twig": "~1.28|~2.0",
25-
"symfony/var-dumper": "~3.2"
25+
"symfony/var-dumper": "~3.3"
2626
},
2727
"require-dev": {
2828
"symf 10000 ony/config": "~2.8|~3.0",
@@ -31,7 +31,8 @@
3131
"symfony/stopwatch": "~2.8|~3.0"
3232
},
3333
"conflict": {
34-
"symfony/event-dispatcher": "<3.2"
34+
"symfony/event-dispatcher": "<3.2",
35+
"symfony/var-dumper": "<3.3"
3536
},
3637
"autoload": {
3738
"psr-4": { "Symfony\\Bundle\\WebProfilerBundle\\": "" },

src/Symfony/Component/Cache/DataCollector/CacheDataCollector.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,13 @@ public function collect(Request $request, Response $response, \Exception $except
4545
$empty = array('calls' => array(), 'config' => array(), 'options' => array(), 'statistics' => array());
4646
$this->data = array('instances' => $empty, 'total' => $empty);
4747
foreach ($this->instances as $name => $instance) {
48-
$calls = $instance->getCalls();
49-
foreach ($calls as $call) {
50-
if (isset($call->result)) {
51-
$call->result = $this->cloneVar($call->result);
52-
}
53-
if (isset($call->argument)) {
54-
$call->argument = $this->cloneVar($call->argument);
55-
}
56-
}
57-
$this->data['instances']['calls'][$name] = $calls;
48+
$this->data['instances']['calls'][$name] = $instance->getCalls();
5849
}
5950

6051
$this->data['instances']['statistics'] = $this->calculateStatistics();
6152
$this->data['total']['statistics'] = $this->calculateTotalStatistics();
53+
54+
$this->data = $this->cloneVar($this->data);
6255
}
6356

6457
/**
@@ -133,7 +126,7 @@ private function calculateStatistics()
133126
$statistics[$name]['misses'] += $count - $call->misses;
134127
} elseif ('hasItem' === $call->name) {
135128
$statistics[$name]['reads'] += 1;
136-
if (false === $call->result->getRawData()[0][0]) {
129+
if (false === $call->result) {
137130
$statistics[$name]['misses'] += 1;
138131
} else {
139132
$statistics[$name]['hits'] += 1;

src/Symfony/Component/Cache/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
"doctrine/dbal": "~2.4",
3232
"predis/predis": "~1.0"
3333
},
34+
"conflict": {
35+
"symfony/var-dumper": "<3.3"
36+
},
3437
"suggest": {
3538
"symfony/polyfill-apcu": "For using ApcuAdapter on HHVM"
3639
},

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class WrappedListener
2929
private $stopwatch;
3030
private $dispatcher;
3131
private $pretty;
32-
private $data;
32+
private $stub;
3333

3434
private D7AE static $cloner;
3535

@@ -91,15 +91,15 @@ public function getPretty()
9191

9292
public function getInfo($eventName)
9393
{
94-
if (null === $this->data) {
95-
$this->data = false !== self::$cloner ? self::$cloner->cloneVar(array(new ClassStub($this->pretty.'()', $this->listener)))->seek(0) : $this->pretty;
94+
if (null === $this->stub) {
95+
$this->stub = false === self::$cloner ? $this->pretty.'()' : new ClassStub($this->pretty.'()', $this->listener);
9696
}
9797

9898
return array(
9999
'event' => $eventName,
100100
'priority' => null !== $this->dispatcher ? $this->dispatcher->getListenerPriority($eventName, $this->listener) : null,
101101
'pretty' => $this->pretty,
102-
'data' => $this->data,
102+
'stub' => $this->stub,
103103
);
104104
}
105105

src/Symfony/Component/EventDispatcher/Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ public function testGetCalledListeners()
9797
$tdispatcher->addListener('foo', $listener = function () {});
9898

9999
$listeners = $tdispatcher->getNotCalledListeners();
100-
$this->assertArrayHasKey('data', $listeners['foo.closure']);
101-
unset($listeners['foo.closure']['data']);
100+
$this->assertArrayHasKey('stub', $listeners['foo.closure']);
101+
unset($listeners['foo.closure']['stub']);
102102
$this->assertEquals(array(), $tdispatcher->getCalledListeners());
103103
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => 0)), $listeners);
104104

105105
$tdispatcher->dispatch('foo');
106106

107107
$listeners = $tdispatcher->getCalledListeners();
108-
$this->assertArrayHasKey('data', $listeners['foo.closure']);
109-
unset($listeners['foo.closure']['data']);
108+
$this->assertArrayHasKey('stub', $listeners['foo.closure']);
109+
unset($listeners['foo.closure']['stub']);
110110
$this->assertEquals(array('foo.closure' => array('event' => 'foo', 'pretty' => 'closure', 'priority' => null)), $listeners);
111111
$this->assertEquals(array(), $tdispatcher->getNotCalledListeners());
112112
}

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

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class FormDataCollector extends DataCollector implements FormDataCollectorInterf
8080
* @var ClonerInterface
8181
*/
8282
private $cloner;
83-
private $clonerCache = array();
83+
84+
private $hasVarDumper;
8485

8586
public function __construct(FormDataExtractorInterface $dataExtractor)
8687
{
@@ -90,6 +91,7 @@ public function __construct(FormDataExtractorInterface $dataExtractor)
9091
'forms_by_hash' => array(),
9192
'nb_errors' => 0,
9293
);
94+
$this->hasVarDumper = class_exists(ClassStub::class);
9395
}
9496

9597
/**
@@ -238,38 +240,15 @@ public function getData()
238240

239241
public function serialize()
240242
{
241-
$cloneVar = array($this 10000 , 'cloneVar');
242-
243-
foreach ($this->data['forms_by_hash'] as &$form) {
244-
foreach ($form as $k => $v) {
245-
switch ($k) {
246-
case 'type_class':
247-
$form[$k] = $cloneVar($v, true);
248-
break;
249-
case 'synchronized':
250-
$form[$k] = $cloneVar($v);
251-
break;
252-
case 'view_vars':
253-
case 'passed_options':
254-
case 'resolved_options':
255-
case 'default_data':
256-
case 'submitted_data':
257-
if ($v && is_array($v)) {
258-
$form[$k] = array_map($cloneVar, $v);
259-
}
260-
break;
261-
case 'errors':
262-
foreach ($v as $i => $e) {
263-
if (!empty($e['trace'])) {
264-
$form['errors'][$i]['trace'] = array_map($cloneVar, $e['trace']);
265-
}
266-
}
267-
break;
243+
if ($this->hasVarDumper) {
244+
foreach ($this->data['forms_by_hash'] as &$form) {
245+
if (isset($form['type_class'])) {
246+
$form['type_class'] = new ClassStub($form['type_class']);
268247
}
269248
}
270249
}
271250

272-
return serialize($this->data);
251+
return serialize($this->cloneVar($this->data));
273252
}
274253

275254
/**
@@ -281,9 +260,8 @@ protected function cloneVar($var, $isClass = false)
281260
return $var;
282261
}
283262
if (null === $this->cloner) {
284-
if (class_exists(ClassStub::class)) {
263+
if ($this->hasVarDumper) {
285264
$this->cloner = new VarCloner();
286-
$this->cloner->setMaxItems(25);
287265
$this->cloner->addCasters(array(
288266
'*' => function ($v, array $a, Stub $s, $isNested) {
289267
if ($isNested && !$v instanceof \DateTimeInterface) {
@@ -320,34 +298,15 @@ protected function cloneVar($var, $isClass = false)
320298
$this->cloner = false;
321299
}
322300
}
323-
if (false === $this->cloner) {
324-
if (null === $this->valueExporter) {
325-
$this->valueExporter = new ValueExporter();
326-
}
327-
328-
return $this->valueExporter->exportValue($var);
329-
}
330-
if (null === $var) {
331-
$type = $hash = 'null';
332-
} elseif (array() === $var) {
333-
$type = $hash = 'array';
334-
} elseif ('object' === $type = gettype($var)) {
335-
$hash = spl_object_hash($var);
336-
} elseif ('double' === $type) {
337-
$hash = (string) $var;
338-
} elseif ('integer' === $type || 'string' === $type) {
339-
$hash = $var;
340-
} else {
341-
$type = null;
301+
if (false !== $this->cloner) {
302+
return $this->cloner->cloneVar($var);
342303
}
343-
if (null !== $type && null !== $cache = &$this->clonerCache[$type][$hash]) {
344-
return $cache;
345-
}
346-
if ($isClass) {
347-
return $cache = $this->cloner->cloneVar(array(new ClassStub($var)))->seek(0);
304+
305+
if (null === $this->valueExporter) {
306+
$this->valueExporter = new ValueExporter();
348307
}
349308

350-
return $cache = $this->cloner->cloneVar($var);
309+
return $this->valueExporter->exportValue($var);
351310
}
352311

353312
private function &recursiveBuildPreliminaryFormTree(FormInterface $form, array &$outputByHash)

src/Symfony/Component/Form/composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
"symfony/http-kernel": "~2.8|~3.0",
3333
"symfony/security-csrf": "~2.8|~3.0",
3434
"symfony/translation": "~2.8|~3.0",
35-
"symfony/var-dumper": "~3.2"
35+
"symfony/var-dumper": "~3.3"
3636
},
3737
"conflict": {
3838
"symfony/doctrine-bridge": "<2.7",
3939
"symfony/framework-bundle": "<2.7",
40-
"symfony/twig-bridge": "<2.7"
40+
"symfony/twig-bridge": "<2.7",
41+
"symfony/var-dumper": "~3.3"
4142
},
4243
"suggest": {
4344
"symfony/validator": "For form validation.",

0 commit comments

Comments
 (0)
0