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 all commits
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
[VarDu 8000 mper] 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
2 changes: 2 additions & 0 deletions 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",
"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 Down
80 changes: 80 additions & 0 deletions 80 src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?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\Bridge\Twig\Extension;

use Symfony\Bridge\Twig\TokenParser\DumpTokenParser;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

/**
* Provides integration of the dump() function with Twig.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class DumpExtension extends \Twig_Extension
{
public function __construct(ClonerInterface $cloner = null)
{
$this->cloner = $cloner;
}

public function getFunctions()
{
return array(
new \Twig_SimpleFunction('dump', array($this, 'dump'), array('is_safe' => array('html'), 'needs_context' => true, 'needs_environment' => true)),
);
}

public function getTokenParsers()
{
return array(new DumpTokenParser());
}

public function getName()
{
return 'dump';
}

public function dump(\Twig_Environment $env, $context)
{
if (!$env->isDebug() || !$this->cloner) {
return;
}

if (2 === func_num_args()) {
$vars = array();
foreach ($context as $key => $value) {
if (!$value instanceof \Twig_Template) {
$vars[$key] = $value;
}
}

$vars = array($vars);
} else {
$vars = func_get_args();
unset($vars[0], $vars[1]);
}

$html = '';
$dumper = new HtmlDumper(function ($line, $depth) use (&$html) {
if (-1 !== $depth) {
$html .= str_repeat(' ', $depth).$line."\n";
}
});

foreach ($vars as $value) {
$dumper->dump($this->cloner->cloneVar($value));
}

return $html;
}
}
84 changes: 84 additions & 0 deletions src/Symfony/Bridge/Twig/Node/DumpNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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\Bridge\Twig\Node;

/**
* @author Julien Galenski <julien.galenski@gmail.com>
*/
class DumpNode extends \Twig_Node
{
private $varPrefix;

public function __construct($varPrefix, \Twig_NodeInterface $values = null, $lineno, $tag = null)
Copy link

Choose a reason for hiding this comment

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

Hey

$lineno without default value after an optional argument?

Example #5 Incorrect usage of default function arguments php.net

Copy link
Member

Choose a reason for hiding this comment

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

The default value for $values is required. Otherwise, you wouldn't be able to pass null as an argument because of the type hint.

Copy link

Choose a reason for hiding this comment

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

That's ok, but mandatory parameter should not follow an optional (not php error but...). Twig_Node has $lineno = 0 as default, so this should have too :) Nothing big, just noticed.

{
parent::__construct(array('values' => $values), array(), $lineno, $tag);
$this->varPrefix = $varPrefix;
}

/**
* {@inheritdoc}
*/
public function compile(\Twig_Compiler $compiler)
{
$compiler
->write("if (\$this->env->isDebug()) {\n")
->indent();

$values = $this->getNode('values');

if (null === $values) {
// remove embedded templates (macros) from the context
$compiler
->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $this->varPrefix))
->indent()
->write(sprintf('if (!$%sval instanceof \Twig_Template) {'."\n", $this->varPrefix))
->indent()
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $this->varPrefix))
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->addDebugInfo($this)
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix));
} elseif (1 === $values->count()) {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\VarDumper\VarDumper::dump(')
->subcompile($values- F987 >getNode(0))
->raw(");\n");
} else {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\VarDumper\VarDumper::dump(array('."\n")
->indent();
foreach ($values as $node) {
$compiler->addIndentation();
if ($node->hasAttribute('name')) {
$compiler
->string($node->getAttribute('name'))
->raw(' => ');
}
$compiler
->subcompile($node)
->raw(",\n");
}
$compiler
->outdent()
->write("));\n");
}

$compiler
->outdent()
->raw("}\n");
}
}
105 changes: 105 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/DumpExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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\Bridge\Twig\Tests\Extension;

use Symfony\Bridge\Twig\Extension\DumpExtension;
use Symfony\Component\VarDumper\VarDumper;
use Symfony\Component\VarDumper\Cloner\PhpCloner;

class DumpExtensionTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getDumpTags
*/
public function testDumpTag($template, $debug, $expectedOutput, $expectedDumped)
{
$extension = new DumpExtension(new PhpCloner());
$twig = new \Twig_Environment(new \Twig_Loader_String(), array(
'debug' => $debug,
'cache' => false,
'optimizations' => 0,
));
$twig->addExtension($extension);

$dumped = null;
$exception = null;
$prevDumper = VarDumper::setHandler(function ($var) use (&$dumped) {$dumped = $var;});

try {
$this->assertEquals($expectedOutput, $twig->render($template));
} catch (\Exception $exception) {
}

VarDumper::setHandler($prevDumper);

if (null !== $exception) {
throw $exception;
}

$this->assertSame($expectedDumped, $dumped);
}

public function getDumpTags()
{
return array(
array('A{% dump %}B', true, 'AB', array()),
array('A{% set foo="bar"%}B{% dump %}C', true, 'ABC', array('foo' => 'bar')),
array('A{% dump %}B', false, 'AB', null),
);
}

/**
* @dataProvider getDumpArgs
*/
public function testDump($context, $args, $expectedOutput, $debug = true)
{
$extension = new DumpExtension(new PhpCloner());
$twig = new \Twig_Environment(new \Twig_Loader_String(), array(
'debug' => $debug,
'cache' =& 1241 gt; false,
'optimizations' => 0,
));

array_unshift($args, $context);
array_unshift($args, $twig);

$dump = call_user_func_array(array($extension, 'dump'), $args);

if ($debug) {
$this->assertStringStartsWith('<script>', $dump);
$dump = preg_replace('/^.*?<pre/', '<pre', $dump);
}
$this->assertEquals($expectedOutput, $dump);
}

public function getDumpArgs()
{
return array(
array(array(), array(), '', false),
array(array(), array(), "<pre id=sf-dump><span class=sf-dump-0>[]\n</span></pre><script>Sfjs.dump.instrument()</script>\n"),
array(
array(),
array(123, 456),
"<pre id=sf-dump><span class=sf-dump-0><span class=sf-dump-num>123</span>\n</span></pre><script>Sfjs.dump.instrument()</script>\n"
."<pre id=sf-dump><span class=sf-dump-0><span class=sf-dump-num>456</span>\n</span></pre><script>Sfjs.dump.instrument()</script>\n",
),
array(
array('foo' => 'bar'),
array(),
"<pre id=sf-dump><span class=sf-dump-0><span class=sf-dump-note>array:1</span> [<span name=sf-dump-child>\n"
." <span class=sf-dump-1>\"<span class=sf-dump-meta>foo</span>\" => \"<span class=sf-dump-str>bar</span>\"\n"
."</span></span>]\n"
."</span></pre><script>Sfjs.dump.instrument()</script>\n",
),
);
}
}
89 changes: 89 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Node/DumpNodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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\Bridge\Twig\Tests\Node;

use Symfony\Bridge\Twig\Node\DumpNode;

class DumpNodeTest extends \PHPUnit_Framework_TestCase
{
public function testNoVar()
{
$node = new DumpNode('bar', null, 7);

$env = new \Twig_Environment();
$compiler = new \Twig_Compiler($env);

$expected = <<<'EOTXT'
Copy link
Contributor

Choose a reason for hiding this comment

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

why this is quoted, just curious, thanks in advance.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's the NOWDOC php syntax

Copy link
Member

Choose a reason for hiding this comment

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

and the goal of using NOWDOC is to avoid having to escape $ in the string.

NOWDOC vs HEREDOC is similar too single-quotes vs double-quotes for strings (not exactly though)

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah what throws me off is that the closing EOTXT was not quoted also, i would expect it to be. Reference http://php.net/manual/en/language.types.string.php#language.types.string.syntax.single

if ($this->env->isDebug()) {
$barvars = array();
foreach ($context as $barkey => $barval) {
if (!$barval instanceof \Twig_Template) {
$barvars[$barkey] = $barval;
}
}
// line 7
\Symfony\Component\VarDumper\VarDumper::dump($barvars);
}

EOTXT;

$this->assertSame($expected, $compiler->compile($node)->getSource());
}

public function testOneVar()
{
$vars = new \Twig_Node(array(
new \Twig_Node_Expression_Name('foo', 7),
));
$node = new DumpNode('bar', $vars, 7);

$env = new \Twig_Environment();
$compiler = new \Twig_Compiler($env);

$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
// line 7
\Symfony\Component\VarDumper\VarDumper::dump(%foo%);
}

EOTXT;
$expected = preg_replace('/%(.*?)%/', version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["$1"]) ? $context["$1"] : null)' : '$this->getContext($context, "$1")', $expected);

$this->assertSame($expected, $compiler->compile($node)->getSource());
}

public function testMultiVars()
{
$vars = new \Twig_Node(array(
new \Twig_Node_Expression_Name('foo', 7),
new \Twig_Node_Expression_Name('bar', 7),
));
$node = new DumpNode('bar', $vars, 7);

$env = new \Twig_Environment();
$compiler = new \Twig_Compiler($env);

$expected = <<<'EOTXT'
if ($this->env->isDebug()) {
// line 7
\Symfony\Component\VarDumper\VarDumper::dump(array(
"foo" => %foo%,
"bar" => %bar%,
));
}

EOTXT;
$expected = preg_replace('/%(.*?)%/', version_compare(PHP_VERSION, '5.4.0') >= 0 ? '(isset($context["$1"]) ? $context["$1"] : null)' : '$this->getContext($context, "$1")', $expected);

$this->assertSame($expected, $compiler->compile($node)->getSource());
}
}
Loading
0