1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-17 05:18:32 +01:00

added DataFormater and now using kint to format variables (fixed #93 and #95)

This commit is contained in:
maximebf 2014-03-04 13:55:36 -03:00
parent 5bbb3bc78b
commit cae98bc37a
12 changed files with 217 additions and 67 deletions

View File

@ -12,7 +12,8 @@
}],
"require": {
"php": ">=5.3.0",
"psr/log": "~1.0"
"psr/log": "~1.0",
"raveren/kint": "0.9"
},
"require-dev": {
"php": ">=5.3.0"

View File

@ -46,7 +46,10 @@ class ConfigCollector extends DataCollector implements Renderable
{
$data = array();
foreach ($this->data as $k => $v) {
$data[$k] = $this->formatVar($v);
if (!is_string($v)) {
$v = $this->getDataFormater()->formatVar($v);
}
$data[$k] = $v;
}
return $data;
}

View File

@ -10,75 +10,81 @@
namespace DebugBar\DataCollector;
use DebugBar\DataFormater\DataFormaterInterface;
use DebugBar\DataFormater\DataFormater;
/**
* Abstract class for data collectors
*/
abstract class DataCollector implements DataCollectorInterface
{
private static $defaultDataFormater;
protected $dataFormater;
/**
* Transforms a PHP variable to a string representation
* Sets the default data formater instance used by all collectors subclassing this class
*
* @param mixed $var
* @return string
* @param DataFormaterInterface $formater
*/
public static function setDefaultDataFormater(DataFormaterInterface $formater)
{
self::$defaultDataFormater = $formater;
}
/**
* Returns the default data formater
*
* @return DataFormaterInterface
*/
public static function getDefaultDataFormater()
{
if (self::$defaultDataFormater === null) {
self::$defaultDataFormater = new DataFormater();
}
return self::$defaultDataFormater;
}
/**
* Sets the data formater instance used by this collector
*
* @param DataFormaterInterface $formater
*/
public function setDataFormater(DataFormaterInterface $formater)
{
$this->dataFormater = $formater;
return $this;
}
public function getDataFormater()
{
if ($this->dataFormater === null) {
$this->dataFormater = self::getDefaultDataFormater();
}
return $this->dataFormater;
}
/**
* @deprecated
*/
public function formatVar($var)
{
$var = $this->flattenVar($var);
return print_r($var, true);
return $this->getDataFormater()->formatVar($var);
}
/**
* Flatten a var recursively
*
* @param $var
* @return mixed
*/
public function flattenVar($var)
{
if (is_array($var)) {
foreach ($var as &$v) {
$v = $this->flattenVar($v);
}
} else if (is_object($var)) {
$var = "Object(" . get_class($var) . ")";
} else if (mb_check_encoding($var, 'UTF-8')) {
$var = htmlentities($var, ENT_QUOTES, 'UTF-8', false);
} else {
$var = '[Invalid UTF-8 string]';
}
return $var;
}
/**
* Transforms a duration in seconds in a readable string
*
* @param float $seconds
* @return string
* @deprecated
*/
public function formatDuration($seconds)
{
if ($seconds < 0.001) {
return round($seconds * 1000000) . 'μs';
} else if ($seconds < 1) {
return round($seconds * 1000, 2) . 'ms';
}
return round($seconds, 2) . 's';
return $this->getDataFormater()->formatDuration($seconds);
}
/**
* Transforms a size in bytes to a human readable string
*
* @param string $size
* @param integer $precision
* @return string
* @deprecated
*/
public function formatBytes($size, $precision = 2)
{
if ($size === 0 || $size === null) {
return "0B";
}
$base = log($size) / log(1024);
$suffixes = array('', 'KB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
return $this->getDataFormater()->formatBytes($size, $precision);
}
}

View File

@ -43,7 +43,7 @@ class MemoryCollector extends DataCollector implements Renderable
$this->updatePeakUsage();
return array(
'peak_usage' => $this->peakUsage,
'peak_usage_str' => $this->formatBytes($this->peakUsage)
'peak_usage_str' => $this->getDataFormater()->formatBytes($this->peakUsage)
);
}

View File

@ -23,6 +23,8 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
protected $aggregates = array();
protected $dataFormater;
/**
* @param string $name
*/
@ -31,6 +33,25 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
$this->name = $name;
}
/**
* Sets the data formater instance used by this collector
*
* @param DataFormaterInterface $formater
*/
public function setDataFormater(DataFormaterInterface $formater)
{
$this->dataFormater = $formater;
return $this;
}
public function getDataFormater()
{
if ($this->dataFormater === null) {
$this->dataFormater = DataCollector::getDefaultDataFormater();
}
return $this->dataFormater;
}
/**
* Adds a message
*
@ -41,9 +62,14 @@ class MessagesCollector extends AbstractLogger implements DataCollectorInterface
*/
public function addMessage($message, $label = 'info')
{
$isString = true;
if (!is_string($message)) {
$message = $this->getDataFormater()->formatVar($message);
$isString = false;
}
$this->messages[] = array(
'message' => print_r($message, true),
'is_string' => is_string($message),
'message' => $message,
'is_string' => $isString,
'label' => $label,
'time' => microtime(true)
);

View File

@ -101,9 +101,9 @@ class PDOCollector extends DataCollector implements Renderable
array_map(function($s) use ($name) { $s['connection'] = $name; return $s; }, $pdodata['statements']));
}
$data['accumulated_duration_str'] = $this->formatDuration($data['accumulated_duration']);
$data['memory_usage_str'] = $this->formatBytes($data['memory_usage']);
$data['peak_memory_usage_str'] = $this->formatBytes($data['peak_memory_usage']);
$data['accumulated_duration_str'] = $this->getDataFormater()->formatDuration($data['accumulated_duration']);
$data['memory_usage_str'] = $this->getDataFormater()->formatBytes($data['memory_usage']);
$data['peak_memory_usage_str'] = $this->getDataFormater()->formatBytes($data['peak_memory_usage']);
return $data;
}
@ -126,11 +126,11 @@ class PDOCollector extends DataCollector implements Renderable
'prepared_stmt' => $stmt->getSql(),
'params' => (object) $stmt->getParameters(),
'duration' => $stmt->getDuration(),
'duration_str' => $this->formatDuration($stmt->getDuration()),
'duration_str' => $this->getDataFormater()->formatDuration($stmt->getDuration()),
'memory' => $stmt->getMemoryUsage(),
'memory_str' => $this->formatBytes($stmt->getMemoryUsage()),
'memory_str' => $this->getDataFormater()->formatBytes($stmt->getMemoryUsage()),
'end_memory' => $stmt->getEndMemory(),
'end_memory_str' => $this->formatBytes($stmt->getEndMemory()),
'end_memory_str' => $this->getDataFormater()->formatBytes($stmt->getEndMemory()),
'is_success' => $stmt->isSuccess(),
'error_code' => $stmt->getErrorCode(),
'error_message' => $stmt->getErrorMessage()
@ -144,11 +144,11 @@ class PDOCollector extends DataCollector implements Renderable
'nb_statements' => count($stmts),
'nb_failed_statements' => count($pdo->getFailedExecutedStatements()),
'accumulated_duration' => $pdo->getAccumulatedStatementsDuration(),
'accumulated_duration_str' => $this->formatDuration($pdo->getAccumulatedStatementsDuration()),
'accumulated_duration_str' => $this->getDataFormater()->formatDuration($pdo->getAccumulatedStatementsDuration()),
'memory_usage' => $pdo->getMemoryUsage(),
'memory_usage_str' => $this->formatBytes($pdo->getPeakMemoryUsage()),
'memory_usage_str' => $this->getDataFormater()->formatBytes($pdo->getPeakMemoryUsage()),
'peak_memory_usage' => $pdo->getPeakMemoryUsage(),
'peak_memory_usage_str' => $this->formatBytes($pdo->getPeakMemoryUsage()),
'peak_memory_usage_str' => $this->getDataFormater()->formatBytes($pdo->getPeakMemoryUsage()),
'statements' => $stmts
);
}

View File

@ -25,7 +25,7 @@ class RequestDataCollector extends DataCollector implements Renderable
foreach ($vars as $var) {
if (isset($GLOBALS[$var])) {
$data["$" . $var] = $this->formatVar($GLOBALS[$var]);
$data["$" . $var] = $this->getDataFormater()->formatVar($GLOBALS[$var]);
}
}

View File

@ -87,7 +87,7 @@ class TimeDataCollector extends DataCollector implements Renderable
'end' => $end,
'relative_end' => $end - $this->requestEndTime,
'duration' => $end - $start,
'duration_str' => $this->formatDuration($end - $start)
'duration_str' => $this->getDataFormater()->formatDuration($end - $start)
);
}
@ -162,7 +162,7 @@ class TimeDataCollector extends DataCollector implements Renderable
'start' => $this->requestStartTime,
'end' => $this->requestEndTime,
'duration' => $this->getRequestDuration(),
'duration_str' => $this->formatDuration($this->getRequestDuration()),
'duration_str' => $this->getDataFormater()->formatDuration($this->getRequestDuration()),
'measures' => array_values($this->measures)
);
}

View File

@ -0,0 +1,39 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\DataFormater;
class DataFormater implements DataFormaterInterface
{
public function formatVar($data)
{
return \kintLite($data);
}
public function formatDuration($seconds)
{
if ($seconds < 0.001) {
return round($seconds * 1000000) . 'μs';
} else if ($seconds < 1) {
return round($seconds * 1000, 2) . 'ms';
}
return round($seconds, 2) . 's';
}
public function formatBytes($size, $precision = 2)
{
if ($size === 0 || $size === null) {
return "0B";
}
$base = log($size) / log(1024);
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}
}

View File

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\DataFormater;
/**
* Formats data to be outputed as string
*/
interface DataFormaterInterface
{
/**
* Transforms a PHP variable to a string representation
*
* @param mixed $var
* @return string
*/
function formatVar($data);
/**
* Transforms a duration in seconds in a readable string
*
* @param float $seconds
* @return string
*/
function formatDuration($seconds);
/**
* Transforms a size in bytes to a human readable string
*
* @param string $size
* @param integer $precision
* @return string
*/
function formatBytes($size, $precision = 2);
}

View File

@ -15,9 +15,9 @@ class ConfigCollectorTest extends DebugBarTestCase
$this->assertArrayHasKey('s', $data);
$this->assertEquals('bar', $data['s']);
$this->assertArrayHasKey('a', $data);
$this->assertEquals("Array\n(\n)\n", $data['a']);
$this->assertEquals("array()", $data['a']);
$this->assertArrayHasKey('o', $data);
$this->assertEquals('Object(stdClass)', $data['o']);
$this->assertEquals('object stdClass {}', $data['o']);
}
public function testName()

View File

@ -0,0 +1,33 @@
<?php
namespace DebugBar\Tests\DataFormater;
use DebugBar\Tests\DebugBarTestCase;
use DebugBar\DataFormater\DataFormater;
class DataFormaterTest extends DebugBarTestCase
{
public function testFormatVar()
{
$f = new DataFormater();
$this->assertEquals("bool TRUE", $f->formatVar(true));
}
public function testFormatDuration()
{
$f = new DataFormater();
$this->assertEquals("100μs", $f->formatDuration(0.0001));
$this->assertEquals("100ms", $f->formatDuration(0.1));
$this->assertEquals("1s", $f->formatDuration(1));
$this->assertEquals("1.35s", $f->formatDuration(1.345));
}
public function testFormatBytes()
{
$f = new DataFormater();
$this->assertEquals("0B", $f->formatBytes(0));
$this->assertEquals("1B", $f->formatBytes(1));
$this->assertEquals("1KB", $f->formatBytes(1024));
$this->assertEquals("1MB", $f->formatBytes(1024 * 1024));
}
}