[HttpKernel] Extracted value exporting logic of DataCollector into a separate ValueExporter class

This commit is contained in:
Bernhard Schussek 2013-09-25 15:54:05 +02:00
parent 56d78eda56
commit f56c5774a8
2 changed files with 71 additions and 28 deletions

View File

@ -11,17 +11,26 @@
namespace Symfony\Component\HttpKernel\DataCollector; namespace Symfony\Component\HttpKernel\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporterInterface;
/** /**
* DataCollector. * DataCollector.
* *
* Children of this class must store the collected data in the data property. * Children of this class must store the collected data in the data property.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@symfony.com>
*/ */
abstract class DataCollector implements DataCollectorInterface, \Serializable abstract class DataCollector implements DataCollectorInterface, \Serializable
{ {
protected $data; protected $data;
/**
* @var ValueExporter
*/
private $valueExporter;
public function serialize() public function serialize()
{ {
return serialize($this->data); return serialize($this->data);
@ -41,35 +50,10 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable
*/ */
protected function varToString($var) protected function varToString($var)
{ {
if (is_object($var)) { if (null === $this->valueExporter) {
return sprintf('Object(%s)', get_class($var)); $this->valueExporter = new ValueExporter();
} }
if (is_array($var)) { $this->valueExporter->exportValue($var);
$a = array();
foreach ($var as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->varToString($v));
}
return sprintf("Array(%s)", implode(', ', $a));
}
if (is_resource($var)) {
return sprintf('Resource(%s)', get_resource_type($var));
}
if (null === $var) {
return 'null';
}
if (false === $var) {
return 'false';
}
if (true === $var) {
return 'true';
}
return (string) $var;
} }
} }

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpKernel\DataCollector\Util;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValueExporter
{
/**
* Converts a PHP value to a string.
*
* @param mixed $value The PHP value
*
* @return string The string representation of the given value
*/
public function exportValue($value)
{
if (is_object($value)) {
return sprintf('Object(%s)', get_class($value));
}
if (is_array($value)) {
$a = array();
foreach ($value as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->exportValue($v));
}
return sprintf("Array(%s)", implode(', ', $a));
}
if (is_resource($value)) {
return sprintf('Resource(%s)', get_resource_type($value));
}
if (null === $value) {
return 'null';
}
if (false === $value) {
return 'false';
}
if (true === $value) {
return 'true';
}
return (string) $value;
}
}