8000 VarDumper and DebugBundle by nicolas-grekas · Pull Request #10640 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

VarDumper and DebugBundle #10640

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

Merged
merged 31 commits into from
Sep 23, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eec5c92
[Debug] Symfony debug extension
Mar 7, 2014
4bf9300
[Debug] a README for the debug extension
nicolas-grekas Mar 20, 2014
07135a0
[VarDumper] algo to clone any PHP variable to a breadth-first queue
nicolas-grekas Apr 5, 2014
5b7ae28
[VarDumper] symfony_debug ext. fast and memory efficient cloning algo
nicolas-grekas Apr 5, 2014
3ddbf4b
[VarDumper] add casters for per class/resource custom state extraction
nicolas-grekas Apr 5, 2014
c91bc83
[VarDumper] casters for exceptions representation
nicolas-grekas Apr 5, 2014
da3e50a
[VarDumper] casters for SPL data structures
nicolas-grekas Apr 5, 2014
0a92c08
[VarDumper] casters for PDO related objects
nicolas-grekas Apr 5, 2014
c426d8b
[VarDumper] casters for Doctrine objects
nicolas-grekas Apr 5, 2014
0266072
[VarDumper] casters for DOM objects
nicolas-grekas Aug 24, 2014
1d5e3f4
[VarDumper] interface for dumping collected variables
nicolas-grekas Apr 5, 2014
fa81544
[VarDumper] CLI dedicated dumper and related abstract
nicolas-grekas Apr 5, 2014
e6dde33
[VarDumper] HTML variant of the CLI dumper
nicolas-grekas Apr 5, 2014
5eaa187
[VarDumper] tests for CliDumper
nicolas-grekas Apr 5, 2014
a69e962
[VarDumper] tests for HtmlDumper
nicolas-grekas Jun 13, 2014
297d373
[VarDumper] README, LICENSE and composer.json
nicolas-grekas Apr 5, 2014
9dea601
[DebugBundle] global dump() function for daily use
nicolas-grekas May 29, 2014
eb98c81
[DebugBundle] dump() + better Symfony glue
nicolas-grekas Aug 26, 2014
8d5d970
[DebugBundle] adjust after review
nicolas-grekas Aug 27, 2014
c8746a4
[DebugBundle] add tests for twig and for the bundle
nicolas-grekas Aug 27, 2014
0d8a942
[VarDumper] add Stub objects for cutting cleanly and dumping consts
nicolas-grekas Sep 8, 2014
081363c
[HttpKernel] tests for DumpListener
nicolas-grekas Sep 8, 2014
49f13c6
[HttpKernel] add tests for DumpDataCollector
nicolas-grekas Sep 9, 2014
de05cd9
[DebugBundle] enhance dump excerpts
nicolas-grekas Sep 12, 2014
e4e00ef
[TwigBridge] DumpNode and Token parser
ruian Sep 12, 2014
5f59811
[DebugBundle] Add doc example for Twig usage
Sep 12, 2014
a8d81e4
[DebugBundle] Inlined assets to avoid installation issues
Sep 12, 2014
d43ae82
[VarDumper] Add workaround to https://bugs.php.net/65967
romainneutron Sep 12, 2014
0f8d30f
[VarDumper] Replace \e with \x1B in CliDumper to support colour in PH…
oscherler Sep 12, 2014
2e167ba
[TwigBridge] add Twig dump() function + tests and fixes
nicolas-grekas Sep 17, 2014
80fd736
[DebugBundle] Enhance some comments
lyrixx Sep 23, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[DebugBundle] global dump() function for daily use
  • Loading branch information
nicolas-grekas committed Sep 23, 2014
commit 9dea601234627d964b47457ff342e2b39d36c205
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"symfony/css-selector": "self.version",
"symfony/dependency-injection": "self.version",
"symfony/debug": "self.version",
"symfony/debug-bundle": "self.version",
8000 "symfony/doctrine-bridge": "self.version",
"symfony/dom-crawler": "self.version",
"symfony/event-dispatcher": "self.version",
Expand Down Expand Up @@ -63,6 +64,7 @@
"symfony/twig-bridge": "self.version",
"symfony/twig-bundle": "self.version",
"symfony/validator": "self.version",
"symfony/var-dumper": "self.version",
"symfony/web-profiler-bundle": "self.version",
"symfony/yaml": "self.version"
},
Expand All @@ -83,7 +85,10 @@
"src/Symfony/Component/HttpFoundation/Resources/stubs",
"src/Symfony/Component/Intl/Resources/stubs"
],
"files": [ "src/Symfony/Component/Intl/Resources/stubs/functions.php" ]
"files": [
"src/Symfony/Component/Intl/Resources/stubs/functions.php",
"src/Symfony/Bundle/DebugBundle/Resources/functions/debug.php"
]
},
"minimum-stability": "dev",
"extra": {
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/DebugBundle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
42 changes: 42 additions & 0 deletions src/Symfony/Bundle/DebugBundle/DebugBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\DebugBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\VarDumper;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class DebugBundle extends Bundle
{
public function boot()
{
if ($this->container->getParameter('kernel.debug')) {
$container = $this->container;

// This code is here to lazy load the dump stack. This default
// configuration for CLI mode is overridden in HTTP mode on
// 'kernel.request' event
VarDumper::setHandler(function ($var) use ($container) {
$dumper = new CliDumper();
$cloner = $container->get('var_dumper.cloner');
$handler = function ($var) use ($dumper, $cloner) {
$dumper->dump($cloner->cloneVar($var));
};
VarDumper::setHandler($handler);
$handler($var);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\DebugBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* DebugExtension configuration structure.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('debug');

$rootNode
->children()
->integerNode('max_items')
->info('Max 628C number of displayed items past the first level, -1 means no limit')
->min(-1)
->defaultValue(250)
->end()
->integerNode('max_string_length')
->info('Max length of displayed strings, -1 means no limit')
->min(-1)
->defaultValue(-1)
->end()
->end();
Copy link
Contributor

Choose a reason for hiding this comment

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

put the ; on the next line aligned to the start of the $rootNode->


return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\DebugBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* DebugExtension.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class DebugExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');

$container->setParameter(
'var_dumper.cloner.class',
'Symfony\Component\VarDumper\Cloner\\'.(function_exists('symfony_zval_info') ? 'Ext' : 'Php').'Cloner'
);

$container->getDefinition('var_dumper.cloner')
->addMethodCall('setMaxItems', array($config['max_items']))
->addMethodCall('setMaxString', array($config['max_string_length']));
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Bundle/DebugBundle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
dump() function
================

This bundle provides a better `dump()` function, that you can use instead of
`var_dump()`, *better* meaning:

- per object and resource types specialized view: e.g. filter out Doctrine noise
while dumping a single proxy entity, or get more insight on opened files with
`stream_get_meta_data()`. Add your own dedicated `Dumper\Caster` and get the
view *you* need.
- configurable output format: HTML, command line with colors or [a dedicated high
Copy link
Contributor

Choose a reason for hiding this comment

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

or goes inside the []

accuracy JSON format](Resource/doc/json-spec.md).
- ability to dump internal references, either soft ones (objects or resources)
or hard ones (`=&` on arrays or objects properties). Repeated occurrences of
the same object/array/resource won't appear again and again anymore. Moreover,
you'll be able to inspect the reference structure of your data.
- ability to operate in the context of an output buffering handler.
- full exposure of the internal mechanisms used for walking through an arbitrary
PHP data structure.

Calling `dump($myVvar)` works in all PHP code and `{% dump myVar %}` or
`{{ dump(myVar) }}` in Twig templates.
27 changes: 27 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="data_collector.dump.class">Symfony\Component\HttpKernel\DataCollector\DumpDataCollector</parameter>
<parameter key="debug.dump_listener.class">Symfony\Component\HttpKernel\EventListener\DumpListener</parameter>
</parameters>

<services>
<service id="data_collector.dump" class="%data_collector.dump.class%">
Copy link
Member

Choose a reason for hiding this comment

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

this service does not look like it is retrieved dynamically at runtime, so it should be marked as private

Copy link
Member Author

Choose a reason for hiding this comment

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

See the service just below that takes the container and this service name: data_collector.dump is dynamically lazy loaded.

<tag name="data_collector" id="dump" template="@Debug/Profiler/dump.html.twig" />
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
</service>

<service id="debug.dump_listener" class="%debug.dump_listener.class%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="service_container" />
<argument>data_collector.dump</argument>
</service>

<service id="var_dumper.cloner" class="%var_dumper.cloner.class%" />
</services>

</container>
19 changes: 19 additions & 0 deletions src/Symfony/Bundle/DebugBundle/Resources/meta/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2004-2014 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block toolbar %}
{% set dumps_count = collector.dumpsCount %}

{% if dumps_count %}
{% set icon %}
<img alt="dump()" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAcCAYAAACOGPReAAACcUlEQVRIx+1UPUwUYRB97+Pc8ychMSaE4E+BrYWVgCd7e5dYQSxoiIFEjYWVdNiJiVjaSGFhowUkhsLEH2Jhcvsd54nYaQ0USoAYG7WBvWOfhd+R87zFC40N00z2zTdv30xmBti3/2ZBECy3+pZJgXw+f0rSiKQBkt2SOkluSFoh+SqKoplyufylJdIgCNIAJgGMkUzXcEkAEJM0DtqUNAVgwlq7Vc9hGtR1ACiSHCeZlvQGwFClUjkKYAVAexzHFyXNAkiTvEXSury/lTqFRZI9kr4DuGKtfV6L53K5tTAMu+reZwDMkuwC8F5SUFNcr3TSEf4g6dcTuvIP139ba8vGmD5JawB6Adz9o/xMJnMSwJhLvhGG4acm/T/UCBQKhc9xHA8DAMkxx/Ob1PO8UdfDD8Vi8WnCQAw1A+fn599KegbgoOd5Izukkgbc354kjZi1di4pJumx84M7pCRPO/DdXhaD5ILz3QDAXC4XATiQ8H48DMP7jWA2mx00xrxMUB3Rjcs6gE5JZ621H/ewwsdIfgOwHoZhV22klpz883spX1Kf80v1czrnwKtJidlsdnCXnl6r5zEAUK1WpwFskjwXBMFws8SkHvq+f4HkEIDN7e3tmR3SUqm06o4DADzyff9MK2X39/efMMbU5vpBqVRabVzTCUmLJNvb2tpKu5XrFPamUqkFksfd7t9pevry+XxHHMcvSPa4Hr+W9LBarVrP836mUqkjlUqlF8B1kpcBUNKiMeZSoVD4+q97eg/Azdo9lRSTNDXvsC0AUwBuN97TXS9/HMejAAbcpnQC2JC0THIuiqLppMvfsrnN27d/2y+hkCBqr75LOwAAAABJRU5ErkJggg==" />
<span class="sf-toolbar-status sf-toolbar-status-yellow">{{ dumps_count }}</span>
{% endset %}

{% set text %}
<div style="width: 450px; overflow: hidden;">
<div class="sf-toolbar-info-piece">
<b>dump()</b>
</div>
{% for dump in collector.getDumps('html') %}
<div class="sf-toolbar-info-piece">
in {% if dump.file %}<abbr title="{{ dump.file }}">{{ dump.name }}</abbr>{% else %}{{ dump.name }}{% endif %}
line {{ dump.line }}:
{{ dump.data|raw }}
</div>
{% endfor %}
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" onload="var h = this.parentNode.innerHTML, rx=/<script>(.*?)<\/script>/g, s; while (s = rx.exec(h)) {eval(s[1]);};" />
</div>
{% endset %}

{% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': true } %}
{% endif %}
{% endblock %}

{% block menu %}
<span class="label">
<span class="icon">
{{- "" -}}
<img alt="dump()" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAADdUlEQVRIx71WPWwcVRD+ZneN3NzKiAgiJIrwZ9EQ2xchI+/uvb0g8SOouGtAKQAnSkHSJCZF6jSBUCQFkoNEEILGoaDCQVzeu1sLgeAI4cdyBE2qEMtE6LZA2bvbScGcWRt77+wYpntv5833vu/NzgywDVNK3VZK3d7OWRoQYC+ACgCfiEYB7JZPvzPzNQARgIvGmKt3Bej7/rjjOG8D2D8ggVqn05mJoujKlgCLxaJTKBTOENGRdT5LAH4D8KKsPwUwAWBPxoeZ+Vwcx8eazWZnfWxr/YbneSOu684T0VEB+4uZT3e73ce01k8w8+Ger9a6orV+mJn3AbjAzCkAIqKjruvOe543kstQmF0iorJc9TKA140x13s+5XL5fma+KYBrzgdBsM+yrI+J6PGexK1W67ks0zUMRcayLC/EcfxsFgwAkiRJNnufRqPxnW3bTzPzV7K1v1AonNlQ0jAMJ+TNAGCemac3egPHcdq5WVOr3bJt+yUAv+JvfY/4vj++EcPTIvGf7Xb7gDGmu1HA5eXlpG+q1mq3mPkAAAZAkun/AAZBMJZJ/VMLCwsrmwVbXFxsp2l6ME3Tg3mgxphvAHzSkzYMwycBwAEA27YrkiQJgA/6MajX6+8P+F/OAnhVYlcB/GjJwhe9I2PMH9gh01o3APTU8rNvOCrAV7DzdlXIjAIAhWGYABjKO5Gm6fP1en0+zycMw8MA3svzYebEwv9sJLe7AWA3M79jjJnZSYAwDL+UP+CG1vpBK1OUQUTj/wGpvSLn0mrSMHPUy1al1H07yC4AsEvINFYBu93unGzeQ0Sv9QtUKpWmS6XS9ACYhzIJM7cKGEXRT8z8hXw76XnerrwolmWdtyzrfB92TwF4RZaXjDG/rKmlaZqekNo34jjOh9VqddsZPDk5eS8zfwSAmDll5hP/Kt6NRuMHAGdF2hdWVlZmi8Wis1Uwz/NGhoeHP8v0xLPZWWcNi1ardVyaLgC84bru51NTUw8NCiYz0NdE5PcaRxzHM5uOGM1ms9PpdF7OgD4zNDS0pJQ6pZR6JG+qU0rN2rb9ba+EMfPldrtdWd9T84aod4nozYwPA1hi5p+JqCp7FwGMAXh00CGK+mTahDTmgcdEAG9prb+/q0E4CIIx27Yr0sZGiegBoXITwDUiiph5bpBBeLsVJJEus2W7A2Z3gfLvF5gPAAAAAElFTkSuQmCC" />
{{- "" -}}
</span>
<strong>dump()</strong>
<span class="count">
<span>{{ collector.dumpsCount }}</span>
</span>
</span>
{% endblock %}

{% block panel %}
<h2>dump()</h2>

<style>
li.sf-dump {
list-style-type: disc;
}
.sf-dump ol>li {
padding: 0;
}
.sf-dump a {
cursor: pointer;
}
.sf-dump-compact {
display: none;
}
</style>

<ul class="alt">
{% for dump in collector.getDumps('html') %}
<li class="sf-dump sf-reset">
in {% if dump.file %}<abbr title="{{ dump.file }}">{{ dump.name }}</abbr>{% else %}{{ dump.name }}{% endif %}
line {{ dump.line }}:
<a onclick="Sfjs.dump.toggle(this)">▶</a>
<span class="sf-dump-compact">
{% if dump.fileExcerpt %}{{ dump.fileExcerpt|raw }}{% else %}{{ dump.file|file_excerpt(dump.line) }}{% endif %}
</span>

{{ dump.data|raw }}
</li>
{% endfor %}
</ul>
{% endblock %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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\DebugBundle\Tests\DependencyInjection;

use Symfony\Bundle\DebugBundle\DependencyInjection\DebugExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;

class DebugExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testLoadWithoutConfiguration()
{
$container = $this->createContainer();
$container->registerExtension(new DebugExtension());
$container->loadFromExtension('debug', array());
$this->compileContainer($container);

$expectedTags = array(
array(
"id" => "dump",
"template" => "@Debug/Profiler/dump.html.twig",
),
);

$this->assertSame($expectedTags, $container->getDefinition('data_collector.dump')->getTag('data_collector'));
}

private function createContainer()
{
$container = new ContainerBuilder(new ParameterBag(array(
'kernel.cache_dir' => __DIR__,
'kernel.root_dir' => __DIR__.'/Fixtures',
'kernel.charset' => 'UTF-8',
'kernel.debug' => true,
'kernel.bundles' => array('DebugBundle' => 'Symfony\\Bundle\\DebugBundle\\DebugBundle'),
)));

return $container;
}

private function compileContainer(ContainerBuilder $container)
{
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->compile();
}
}
Loading
0