Skip to content

Commit

Permalink
feature #10352 [DataCollector] Improves the readability of the collec…
Browse files Browse the repository at this point in the history
…ted arrays in the profiler (fabpot)

This PR was merged into the 2.5-dev branch.

Discussion
----------

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

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

This PR is based on #10155.

Original description:

It simply improves the readability of the collected arrays in the profiler:

__before__:
```
Array(date => Array(year => , month => , day => ), time => Array(hour => ))
```

__after__:
```
[
  date => [
      year => ,
      month => ,
      day =>
  ],
  time => [
      hour =>
  ]
]
```

Commits
-------

dce66c9 removed double-stringification of values in the profiler
1cda2d4 [HttpKernel] tweaked value exporter
3f297ea Improves the readability of the collected arrays in the profiler.
  • Loading branch information
fabpot committed Mar 1, 2014
2 parents eede330 + dce66c9 commit 65c9aca
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 21 deletions.
Expand Up @@ -8,6 +8,7 @@
<parameter key="web_profiler.controller.profiler.class">Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController</parameter> <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.router.class">Symfony\Bundle\WebProfilerBundle\Controller\RouterController</parameter>
<parameter key="web_profiler.controller.exception.class">Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController</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> </parameters>


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

<service id="twig.extension.webprofiler" class="%twig.extension.webprofiler.class%" public="false">
<tag name="twig.extension" />
</service>
</services> </services>
</container> </container>
@@ -1,16 +1,15 @@
<table {% if class is defined %}class='{{ class }}'{% endif %} > <table {% if class is defined %}class='{{ class }}'{% endif %} >
<thead> <thead>
<tr> <tr>
<th scope="col">Key</th> <th scope="col" style="width: 25%">Key</th>
<th scope="col">Value</th> <th scope="col" style="width: 75%">Value</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for key in bag.keys|sort %} {% for key in bag.keys|sort %}
<tr> <tr>
<th>{{ key }}</th> <th>{{ key }}</th>
{# JSON_UNESCAPED_SLASHES = 64, JSON_UNESCAPED_UNICODE = 256 #} <td><pre>{{ profiler_dump(bag.get(key)) }}</pre></td>
<td>{{ bag.get(key)|json_encode(64 b-or 256) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>
Expand Down
Expand Up @@ -170,6 +170,10 @@ pre, code {
margin-left: 250px; margin-left: 250px;
padding: 30px 40px 40px; padding: 30px 40px 40px;
} }
#collector-content pre {
white-space: pre-wrap;
word-break: break-all;
}
#navigation { #navigation {
float: left; float: left;
width: 250px; width: 250px;
Expand Down
@@ -1,16 +1,15 @@
<table {% if class is defined %}class='{{ class }}'{% endif %} > <table {% if class is defined %}class='{{ class }}'{% endif %} >
<thead> <thead>
<tr> <tr>
<th scope="col">Key</th> <th scope="col" style="width: 25%">Key</th>
<th scope="col">Value</th> <th scope="col" style="width: 75%">Value</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for key in data|keys|sort %} {% for key in data|keys|sort %}
<tr> <tr>
<th>{{ key }}</th> <th>{{ key }}</th>
{# JSON_UNESCAPED_SLASHES = 64, JSON_UNESCAPED_UNICODE = 256 #} <td><pre>{{ profiler_dump(data[key]) }}</pre></td>
<td>{{ data[key]|json_encode(64 b-or 256) }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </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": { "require": {
"php": ">=5.3.3", "php": ">=5.3.3",
"symfony/http-kernel": "~2.2", "symfony/http-kernel": "~2.3",
"symfony/routing": "~2.2", "symfony/routing": "~2.2",
"symfony/twig-bridge": "~2.2" "symfony/twig-bridge": "~2.2"
}, },
Expand Down
Expand Up @@ -25,7 +25,7 @@ class FormDataExtractorTest_SimpleValueExporter extends ValueExporter
/** /**
* {@inheritdoc} * {@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); 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(); $attributes = array();
foreach ($request->attributes->all() as $key => $value) { foreach ($request->attributes->all() as $key => $value) {
if ('_route' === $key && is_object($value)) { if ('_route' === $key && is_object($value)) {
$attributes['_route'] = $this->varToString($value->getPath()); $value = $value->getPath();
} elseif ('_route_params' === $key) {
foreach ($value as $key => $v) {
$attributes['_route_params'][$key] = $this->varToString($v);
}
} else {
$attributes[$key] = $this->varToString($value);
} }

$attributes[$key] = $value;
} }


$content = null; $content = null;
Expand Down
Expand Up @@ -19,23 +19,38 @@ class ValueExporter
/** /**
* Converts a PHP value to a string. * 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 * @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)) { if (is_object($value)) {
return sprintf('Object(%s)', get_class($value)); return sprintf('Object(%s)', get_class($value));
} }


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

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

$a = array(); $a = array();
foreach ($value as $k => $v) { foreach ($value as $k => $v) {
$a[] = sprintf('%s => %s', $k, $this->exportValue($v)); if (is_array($v)) {
$deep = true;
}
$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)) { if (is_resource($value)) {
Expand Down

0 comments on commit 65c9aca

Please sign in to comment.