-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
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
VarDumper and DebugBundle #10640
Changes from 1 commit
eec5c92
4bf9300
07135a0
5b7ae28
3ddbf4b
c91bc83
da3e50a
0a92c08
c426d8b
0266072
1d5e3f4
fa81544
e6dde33
5eaa187
a69e962
297d373
9dea601
eb98c81
8d5d970
c8746a4
0d8a942
081363c
49f13c6
de05cd9
e4e00ef
5f59811
a8d81e4
d43ae82
0f8d30f
2e167ba
80fd736
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default value for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
/** | ||
|
@@ -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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -183,19 +183,19 @@ public static function utf8Encode($s) | |
{ | ||
if (function_exists('iconv')) { | ||
return iconv('CP1252', 'UTF-8', $s); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"ext-symfony_debug": "" | ||
}, | ||
"autoload": { | ||
"files": [ "Resources/functions/dump.php" ], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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