Optionally use symfony/var-dumper to pretty print var-dumping

Closes #308
This commit is contained in:
Markus Staab
2015-11-21 14:04:59 +01:00
committed by Markus Staab
parent 7285e257ec
commit 6a1c86bb44
4 changed files with 51 additions and 1 deletions
+3
View File
@@ -17,6 +17,9 @@
"require-dev": {
"mockery/mockery": "0.9.*"
},
"suggest": {
"symfony/var-dumper": "^2.6"
},
"autoload": {
"psr-4": {
"Whoops\\": "src/Whoops/"
+5
View File
@@ -458,3 +458,8 @@ kbd {
margin-right: -5px;
border-bottom-color: rgba(0, 0, 0, 0.8)
}
pre.sf-dump {
padding: 0px !important;
margin: 0px !important;
}
@@ -17,7 +17,7 @@
<?php foreach ($data as $k => $value): ?>
<tr>
<td><?php echo $tpl->escape($k) ?></td>
<td><?php echo $tpl->escape(print_r($value, true)) ?></td>
<td><?php echo $tpl->escape($tpl->dump($value)) ?></td>
</tr>
<?php endforeach ?>
</table>
+42
View File
@@ -6,6 +6,10 @@
namespace Whoops\Util;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
/**
* Exposes useful tools for working with/in templates
*/
@@ -58,6 +62,44 @@ class TemplateHelper
);
}
/**
* Format the given value into a human readable string.
*
* @param mixed $value
* @return string
*/
public function dump($value)
{
if (class_exists('Symfony\Component\VarDumper\Cloner\VarCloner')) {
static $dumper = null;
// re-use the same var-dumper instance, so it won't re-render the global styles/scripts on each dump.
if (!$dumper) {
$dumper = new HtmlDumper();
$styles = array(
'default' => '',
'num' => '',
'const' => '',
'str' => '',
'note' => '',
'ref' => '',
'public' => '',
'protected' => '',
'private' => '',
'meta' => '',
'key' => '',
'index' => '',
);
$dumper->setStyles($styles);
}
$cloner = new VarCloner();
return $dumper->dump($cloner->cloneVar($value));
}
return print_r($value, true);
}
/**
* Convert a string to a slug version of itself
*