Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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] dump() + better Symfony glue
  • Loading branch information
nicolas-grekas committed Sep 23, 2014
commit eb98c8175465a30b5758a67ee92f802a8f8b7652
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/debug.php"
"src/Symfony/Bundle/DebugBundle/Resources/functions/dump.php"
]
},
"minimum-stability": "dev",
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/DumpExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

/**
* Provides integration of the dump() function with Twig.
*
* @author Nicolas Grekas <[email protected]>
*/
class DumpExtension extends \Twig_Extension
{
public function getTokenParsers()
{
return array(new DumpTokenParser());
}

public function getName()
{
return 'dump';
}
}
89 changes: 89 additions & 0 deletions src/Symfony/Bridge/Twig/Node/DumpNode.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 <[email protected]>
*
* 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 <[email protected]>
*/
class DumpNode extends \Twig_Node
{
public function __construct(\Twig_NodeInterface $values = null, $lineno, $tag = null)
{
parent::__construct(array('values' => $values), array(), $lineno, $tag);
}

/**
* {@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("\$vars = array();\n")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

you should not name it $vars. It could conflict with other stuff in the compiled template. Instead, you should generate a var name.

In Twig master, there is $compiler->getVarName() generating one, but it is not available in released versions. In stable version, only the Twig_Parser has the method. So a solution is to have the DumpTokenParser setting a var name in the DumpNode when parsing it. This is exactly how the StopwatchNode is implemented currently

->write("foreach (\$context as \$key => \$value) {\n")
->indent()
->write("if (!\$value instanceof Twig_Template) {\n")
->indent()
->write("\$vars[\$key] = \$value;\n")
->outdent()
->write("}\n")
->outdent()
->write("}\n")
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump($vars);'."\n")
;
} elseif (1 === $values->count()) {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump(')
->subcompile($values->getNode(0))
->raw(");\n")
;
} else {
$compiler
->addDebugInfo($this)
->write('\Symfony\Component\Debug\Debug::dump(array(')
->indent()
;
foreach ($values as $node) {
$compiler->addIndentation();
if ($node->hasAttribute('name')) {
$compiler
->string($node->getAttribute('name'))
->raw(' => ')
;
}
$compiler
->subcompile($node)
->raw(",\n")
;
}
$compiler
->outdent()
->raw("));\n")
;
}

$compiler
->outdent()
->write("}\n")
;
}
}
51 changes: 51 additions & 0 deletions src/Symfony/Bridge/Twig/TokenParser/DumpTokenParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\TokenParser;

use Symfony\Bridge\Twig\Node\DumpNode;

/**
* Token Parser for the 'dump' tag.
*
* Dump variables with:
* <pre>
* {% dump %}
* {% dump foo %}
* {% dump foo, bar %}
* </pre>
*
* @author Julien Galenski <[email protected]>
*/
class DumpTokenParser extends \Twig_TokenParser
{
/**
* {@inheritdoc}
*/
public function parse(\Twig_Token $token)
{
$values = null;
if (!$this->parser->getStream()->test(\Twig_Token::BLOCK_END_TYPE)) {
$values = $this->parser->getExpressionParser()->parseMultitargetExpression();
}
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);

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

/**
* {@inheritdoc}
*/
public function getTag()
{
return 'dump';
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We already have a dump function in Twig, so this one is confusing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

What about replacing the existing dump function also? Using the tag would feed the profiler panel, and using the function would do the same as the current dump function: inline dump of vars, with a better output format.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sounds like a good idea.

}
}
2 changes: 2 additions & 0 deletions src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"symfony/security": "~2.4",
"symfony/stopwatch": "~2.2",
"symfony/console": "~2.2",
"symfony/var-dumper": "~2.6",
"symfony/expression-language": "~2.4"
},
"suggest": {
Expand All @@ -41,6 +42,7 @@
"symfony/yaml": "For using the YamlExtension",
"symfony/security": "For using the SecurityExtension",
"symfony/stopwatch": "For using the StopwatchExtension",
"symfony/var-dumper": "For using the DumpExtension",
"symfony/expression-language": "For using the ExpressionExtension"
},
"autoload": {
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/Debug/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

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 @@ -19,6 +24,7 @@
class Debug
{
private static $enabled = false;
private static $dumpHandler;

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

DebugClassLoader::enable();
}

public static function dump($var)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why putting this command in the Debug component (making it broken unless you install the optional dependency), while all the logic lives in the VarDumper component ?

{
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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why not just using call_user_func rather than having to check how the callable looks like ?

}

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: 2 additions & 0 deletions src/Symfony/Component/Debug/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
"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
Loading