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] adjust after review
  • Loading branch information
nicolas-grekas committed Sep 23, 2014
commit 8d5d970eea9804982dda976f5f66ac1b02a2a529
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
],
"files": [
"src/Symfony/Component/Intl/Resources/stubs/functions.php",
"src/Symfony/Bundle/DebugBundle/Resources/functions/dump.php"
"src/Symfony/Component/VarDumper/Resources/functions/dump.php"
Copy link
Member

Choose a reason for hiding this comment

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

I'm not comfortable adding yet another file here.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll look how I could inline the function somewhere instead (bootstrap.cache.php?), it's only a few lines.

Copy link
Member

Choose a reason for hiding this comment

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

not sure that bootstrap.cache.php is the best place as not everyone uses it. ref: http://symfony.com/doc/current/book/performance.html#bootstrap-files-and-byte-code-caches

]
},
"minimum-stability": "dev",
Expand Down
19 changes: 11 additions & 8 deletions src/Symfony/Bridge/Twig/Node/DumpNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
*/
class DumpNode extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $values = null, $lineno, $tag = null)
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;
}

/**
Expand All @@ -36,30 +39,30 @@ public function compile(\Twig_Compiler $compiler)
if (null === $values) {
// remove embedded templates (macros) from the context
$compiler
->write("\$vars = array();\n")
->write("foreach (\$context as \$key => \$value) {\n")
->write(sprintf('$%svars = array();'."\n", $this->varPrefix))
->write(sprintf('foreach ($context as $%1$skey => $%1$sval) {'."\n", $this->varPrefix))
->indent()
->write("if (!\$value instanceof Twig_Template) {\n")
->write(sprintf('if (!$%sval instanceof \Twig_Template) {'."\n", $this->varPrefix))
->indent()
->write("\$vars[\$key] = \$value;\n")
->write(sprintf('$%1$svars[$%1$skey] = $%1$sval;'."\n", $this->varPrefix))
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump($vars);'."\n")
->write(sprintf('\Symfony\Component\VarDumper\VarDumper::dump($%svars);'."\n", $this->varPrefix))
;
} elseif (1 === $values->count()) {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump(')
->write('\Symfony\Component\VarDumper\VarDumper::dump(')
->subcompile($values->getNode(0))
->raw(");\n")
;
} else {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump(array(')
->write('\Symfony\Component\VarDumper\VarDumper::dump(array(')
->indent()
;
foreach ($values as $node) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function parse(\Twig_Token $token)
}
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);

return new DumpNode($values, $token->getLine(), $this->getTag());
return new DumpNode($this->parser->getVarName(), $values, $token->getLine(), $this->getTag());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<parameters>
<parameter key="debug.templating.engine.twig.class">Symfony\Bundle\TwigBundle\Debug\TimedTwigEngine</parameter>
<parameter key="twig.extension.dump.class">Symfony\Bridge\Twig\Extension\DumpExtension</parameter>
</parameters>

<services>
Expand All @@ -19,5 +20,9 @@
<service id="twig.extension.debug" class="Twig_Extension_Debug" public="false">
<tag name="twig.extension" />
</service>

<service id="twig.extension.dump" class="%twig.extension.dump.class%" public="false">
<tag name=&q 8000 uot;twig.extension" />
</service>
</services>
</container>
44 changes: 0 additions & 44 deletions src/Symfony/Component/Debug/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

namespace Symfony\Component\Debug;

use Symfony\Component\VarDumper\Cloner\ExtCloner;
use Symfony\Component\VarDumper\Cloner\PhpCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

/**
* Registers all the debug tools.
*
Expand All @@ -24,7 +19,6 @@
class Debug
{
private static $enabled = false;
private static $dumpHandler;

/**
* Enables the debug tools.
Expand Down Expand Up @@ -65,42 +59,4 @@ public static function enable($errorReportingLevel = null, $displayErrors = true

DebugClassLoader::enable();
}

public static function dump($var)
{
if (null === self::$dumpHandler) {
$cloner = extension_loaded('symfony_debug') ? new ExtCloner() : new PhpCloner();
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
self::$dumpHandler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
}

$h = self::$dumpHandler;

if (is_array($h)) {
return $h[0]->{$h[1]}($var);
}

return $h($var);
}

public static function setDumpHandler($callable)
{
if (!is_callable($callable)) {
throw new \InvalidArgumentException('Invalid PHP callback.');
}

$prevHandler = self::$dumpHandler;

if (is_array($callable)) {
if (!is_object($callable[0])) {
self::$dumpHandler = $callable[0].'::'.$callable[1];
}
} else {
self::$dumpHandler = $callable;
}

return $prevHandler;
}
}
2 changes: 0 additions & 2 deletions src/Symfony/Component/Debug/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
"psr/log": "~1.0"
},
"require-dev": {
"symfony/var-dumper": "~2.6",
"symfony/http-kernel": "~2.1",
"symfony/http-foundation": "~2.1"
},
"suggest": {
"symfony/var-dumper": "For using Debug::dump()",
"symfony/http-foundation": "",
"symfony/http-kernel": ""
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public function dump(Data $data)
$name = false;
$fileExcerpt = false;

for ($i = 1; $i < 6; ++$i) {
for ($i = 1; $i < 7; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& 'dump' === $trace[$i]['function']
&& 'Symfony\Bundle\DebugBundle\DebugBundle' === $trace[$i]['class']
&& 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
) {
$file = $trace[$i]['file'];
$line = $trace[$i]['line'];
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/DOMCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static function castImplementation($dom, array $a, $isNested, &$cut)

public static function castNode(\DOMNode $dom, array $a, $isNested, &$cut)
{
// Commented lines denote properties that exist but are better not dumped for clarity.

$a += array(
'nodeName' => $dom->nodeName,
//'nodeValue' => $dom->nodeValue,
Expand All @@ -93,6 +95,8 @@ public static function castNode(\DOMNode $dom, array $a, $isNested, &$cut)

public static function castNameSpaceNode(\DOMNameSpaceNode $dom, array $a, $isNested, &$cut)
{
// Commented lines denote properties that exist but are better not dumped for clarity.

$a += array(
'nodeName' => $dom->nodeName,
//'nodeValue' => $dom->nodeValue,
Expand Down
24 changes: 12 additions & 12 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,19 @@ public static function utf8Encode($s)
{
if (function_exists('iconv')) {
return iconv('CP1252', 'UTF-8', $s);
Copy link
Member

Choose a reason for hiding this comment

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

why CP1252 here ?

Copy link
Member Author

Choose a reason for hiding this comment

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

CP1252 is a better ISO-8859-1 (in fact, HTML5 recommends auto swapping ISO-8859-1 with CP1252).

} else {
$s .= $s;
$len = strlen($s);

for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
switch (true) {
case $s[$i] < "\x80": $s[$j] = $s[$i]; break;
case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break;
default: $s[$j] = "\xC3"; $s[++$j] = chr(ord($s[$i]) - 64); break;
}
}
}

return substr($s, 0, $j);
$s .= $s;
$len = strlen($s);

for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
switch (true) {
case $s[$i] < "\x80": $s[$j] = $s[$i]; break;
case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break;
default: $s[$j] = "\xC3"; $s[++$j] = chr(ord($s[$i]) - 64); break;
}
}

return substr($s, 0, $j);
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/VarDumper/Resources/functions/dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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.
*/

use Symfony\Component\VarDumper\VarDumper;

if (!function_exists('dump')) {
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
function dump($var)
{
foreach (func_get_args() as $var) {
VarDumper::dump($var);
}
}
}
50 changes: 50 additions & 0 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\Component\VarDumper;

use Symfony\Component\VarDumper\Cloner\ExtCloner;
use Symfony\Component\VarDumper\Cloner\PhpCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class VarDumper
{
private static $handler;

public static function dump($var)
{
if (null === self::$handler) {
$cloner = extension_loaded('symfony_debug') ? new ExtCloner() : new PhpCloner();
$dumper = 'cli' === PHP_SAPI ? new CliDumper() : new HtmlDumper();
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
}

return call_user_func(self::$handler, $h);
}

public static function setHandler($callable)
{
if (!is_callable($callable, true)) {
throw new \InvalidArgumentException('Invalid PHP callback.');
}

$prevHandler = self::$handler;
self::$handler = $callable;

return $prevHandler;
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"ext-symfony_debug": ""
},
"autoload": {
"files": [ "Resources/functions/dump.php" ],
Copy link
Member Author

Choose a reason for hiding this comment

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

Does this line address your comment on the readme @fabpot?

"psr-0": { "Symfony\\Component\\VarDumper\\": "" }
},
"target-dir": "Symfony/Component/VarDumper",
Expand Down
0