8000 [HttpKernel] Extracted value exporting logic of DataCollector into a … · symfony/symfony@f56c577 · GitHub
[go: up one dir, main page]

Skip to content

Commit f56c577

Browse files
committed
[HttpKernel] Extracted value exporting logic of DataCollector into a separate ValueExporter class
1 parent 56d78ed commit f56c577

File tree

2 files changed

+71
-28
lines changed

2 files changed

+71
-28
lines changed

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,26 @@
1111

1212
namespace Symfony\Component\HttpKernel\DataCollector;
1313

14+
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
15+
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporterInterface;
16+
1417
/**
1518
* DataCollector.
1619
*
1720
* Children of this class must store the collected data in the data property.
1821
*
1922
* @author Fabien Potencier <fabien@symfony.com>
23+
* @author Bernhard Schussek <bschussek@symfony.com>
2024
*/
2125
abstract class DataCollector implements DataCollectorInterface, \Serializable
2226
{
2327
protected $data;
2428

29+
/**
30+
* @var ValueExporter
31+
*/
32+
private $valueExporter;
33+
2534
public function serialize()
2635
{
2736
return serialize($this->data);
@@ -41,35 +50,10 @@ public function unserialize($data)
4150
*/
4251
protected function varToString($var)
4352
{
44-
if (is_object($var)) {
45-
return sprintf('Object(%s)', get_class($var));
46-
}
47-
48-
if (is_array($var)) {
49-
$a = array();
50-
foreach ($var as $k => $v) {
51-
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
52-
}
53-
54-
return sprintf("Array(%s)", implode(', ', $a));
55-
}
56-
57-
if (is_resource($var)) {
58-
return sprintf('Resource(%s)', get_resource_type($var));
59-
}
60-
61-
if (null === $var) {
62-
return 'null';
63-
}
64-
65-
if (false === $var) {
66-
return 'false';
67-
}
68-
69-
if (true === $var) {
70-
return 'true';
53+
if (null === $this->valueExporter) {
54+
$this->valueExporter = new ValueExporter();
7155
}
7256

73-
return (string) $var;
57+
$this->valueExporter->exportValue($var);
7458
}
7559
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
namespace Symfony\Component\HttpKernel\DataCollector\Util;
13+
14+
/**
15+
* @author Bernhard Schussek <bschussek@gmail.com>
16+
*/
17+
class ValueExporter
18+
{
19+
/**
20+
* Converts a PHP value to a string.
21+
*
22+
* @param mixed $value The PHP value
23+
*
24+
* @return string The string representation of the given value
25+
*/
26+
public function exportValue($value)
27+
{
28+
if (is_object($value)) {
29+
return sprintf('Object(%s)', get_class($value));
30+
}
31+
32+
if (is_array($value)) {
33+
$a = array();
34+
foreach ($value as $k => $v) {
35+
$a[] = sprintf('%s => %s', $k, $this->exportValue($v));
36+
}
37+
38+
return sprintf("Array(%s)", implode(', ', $a));
39+
}
40+
41+
if (is_resource($value)) {
42+
return sprintf('Resource(%s)', get_resource_type($value));
43+
}
44+
45+
if (null === $value) {
46+
return 'null';
47+
}
48+
49+
if (false === $value) {
50+
return 'false';
51+
}
52+
53+
if (true === $value) {
54+
return 'true';
55+
}
56+
57+
return (string) $value;
58+
}
59+
}

0 commit comments

Comments
 (0)
0