Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DataCollector] Improves the readability of the collected arrays in the profiler #10352

Merged
merged 3 commits into from Mar 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -8,6 +8,7 @@
<parameter key="web_profiler.controller.profiler.class">Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController</parameter>
<parameter key="web_profiler.controller.router.class">Symfony\Bundle\WebProfilerBundle\Controller\RouterController</parameter>
<parameter key="web_profiler.controller.exception.class">Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController</parameter>
<parameter key="twig.extension.webprofiler.class">Symfony\Bundle\WebProfilerBundle\Twig\WebProfilerExtension</parameter>
</parameters>

<services>
Expand All @@ -30,5 +31,9 @@
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
</service>

<service id="twig.extension.webprofiler" class="%twig.extension.webprofiler.class%" public="false">
<tag name="twig.extension" />
</service>
</services>
</container>
@@ -1,16 +1,15 @@
<table {% if class is defined %}class='{{ class }}'{% endif %} >
<thead>
<tr>
<th scope="col">Key</th>
<th scope="col">Value</th>
<th scope="col" style="width: 25%">Key</th>
<th scope="col" style="width: 75%">Value</th>
</tr>
</thead>
<tbody>
{% for key in bag.keys|sort %}
<tr>
<th>{{ key }}</th>
{# JSON_UNESCAPED_SLASHES = 64, JSON_UNESCAPED_UNICODE = 256 #}
<td>{{ bag.get(key)|json_encode(64 b-or 256) }}</td>
<td><pre>{{ profiler_dump(bag.get(key)) }}</pre></td>
</tr>
{% endfor %}
</tbody>
Expand Down
Expand Up @@ -170,6 +170,10 @@ pre, code {
margin-left: 250px;
padding: 30px 40px 40px;
}
#collector-content pre {
white-space: pre-wrap;
word-break: break-all;
}
#navigation {
float: left;
width: 250px;
Expand Down
@@ -1,16 +1,15 @@
<table {% if class is defined %}class='{{ class }}'{% endif %} >
<thead>
<tr>
<th scope="col">Key</th>
<th scope="col">Value</th>
<th scope="col" style="width: 25%">Key</th>
<th scope="col" style="width: 75%">Value</th>
</tr>
</thead>
<tbody>
{% for key in data|keys|sort %}
<tr>
<th>{{ key }}</th>
{# JSON_UNESCAPED_SLASHES = 64, JSON_UNESCAPED_UNICODE = 256 #}
<td>{{ data[key]|json_encode(64 b-or 256) }}</td>
<td><pre>{{ profiler_dump(data[key]) }}</pre></td>
</tr>
{% endfor %}
</tbody>
Expand Down
54 changes: 54 additions & 0 deletions src/Symfony/Bundle/WebProfilerBundle/Twig/WebProfilerExtension.php
@@ -0,0 +1,54 @@
<?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\Bundle\WebProfilerBundle\Twig;

use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;

/**
* Twig extension for the profiler
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class WebProfilerExtension extends \Twig_Extension
{
/**
* @var ValueExporter
*/
private $valueExporter;

/**
* {@inheritdoc}
*/
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('profiler_dump', array($this, 'dumpValue')),
);
}

public function dumpValue($value)
{
if (null === $this->valueExporter) {
$this->valueExporter = new ValueExporter();
}

return $this->valueExporter->exportValue($value);
}

/**
* {@inheritdoc}
*/
public function getName()
{
return 'profiler';
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/WebProfilerBundle/composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.3.3",
"symfony/http-kernel": "~2.2",
"symfony/http-kernel": "~2.3",
"symfony/routing": "~2.2",
"symfony/twig-bridge": "~2.2"
},
Expand Down
Expand Up @@ -25,7 +25,7 @@ class FormDataExtractorTest_SimpleValueExporter extends ValueExporter
/**
* {@inheritdoc}
*/
public function exportValue($value)
public function exportValue($value, $depth = 1, $deep = false)
{
return is_object($value) ? sprintf('object(%s)', get_class($value)) : var_export($value, true);
}
Expand Down
Expand Up @@ -51,14 +51,10 @@ public function collect(Request $request, Response $response, \Exception $except
$attributes = array();
foreach ($request->attributes->all() as $key => $value) {
if ('_route' === $key && is_object($value)) {
$attributes['_route'] = $this->varToString($value->getPath());
} elseif ('_route_params' === $key) {
foreach ($value as $key => $v) {
$attributes['_route_params'][$key] = $this->varToString($v);
}
} else {
$attributes[$key] = $this->varToString($value);
$value = $value->getPath();
}

$attributes[$key] = $value;
}

$content = null;
Expand Down
Expand Up @@ -19,23 +19,38 @@ class ValueExporter
/**
* Converts a PHP value to a string.
*
* @param mixed $value The PHP value
* @param mixed $value The PHP value
* @param integer $depth only for internal usage
* @param Boolean $deep only for internal usage
*
* @return string The string representation of the given value
*/
public function exportValue($value)
public function exportValue($value, $depth = 1, $deep = false)
{
if (is_object($value)) {
return sprintf('Object(%s)', get_class($value));
}

if (is_array($value)) {
if (empty($value)) {
return '[]';
}

$indent = str_repeat(' ', $depth);

$a = array();
foreach ($value as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->exportValue($v));
if (is_array($v)) {
$deep = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should deep be reset on each iteration?

}
$a[] = sprintf('%s => %s', $k, $this->exportValue($v, $depth + 1, $deep));
}

if ($deep) {
return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), str_repeat(' ', $depth - 1));
}

return sprintf("Array(%s)", implode(', ', $a));
return sprintf("[%s]", implode(', ', $a));
}

if (is_resource($value)) {
Expand Down