From: Fabien Potencier Date: Sun, 18 Dec 2011 19:20:13 +0000 (+0100) Subject: made the dump function registration explicit and disable its output when debug is off X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9489136c17244134d6860efbde6f2674d3e8b993;p=web%2Fkonrad%2Ftwig.git made the dump function registration explicit and disable its output when debug is off --- diff --git a/doc/functions/dump.rst b/doc/functions/dump.rst index b9d53d2..5f27b4b 100644 --- a/doc/functions/dump.rst +++ b/doc/functions/dump.rst @@ -14,7 +14,13 @@ introspecting its variables: .. note:: - The ``debug`` function is only available when Twig ``debug`` mode is set. + The ``debug`` function is not available by default. You must load it explicitly:: + + $twig = new Twig_Environment($loader, $config); + $twig->addExtension(new Twig_Extension_Debug()); + + Even when loaded explicitly, it won't do anything if the ``debug`` option + is not enabled. In an HTML context, wrap the output with a ``pre`` tag to make it easier to read: diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index de29d5a..7d22787 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -104,9 +104,6 @@ class Twig_Environment 'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']), 'optimizer' => new Twig_Extension_Optimizer($options['optimizations']), ); - if ($this->debug) { - $this->extensions[] = new Twig_Extension_Debug(); - } $this->strictVariables = (bool) $options['strict_variables']; $this->runtimeInitialized = false; $this->setCache($options['cache']); diff --git a/lib/Twig/Extension/Debug.php b/lib/Twig/Extension/Debug.php index 1a6644c..aaec125 100644 --- a/lib/Twig/Extension/Debug.php +++ b/lib/Twig/Extension/Debug.php @@ -21,7 +21,7 @@ class Twig_Extension_Debug extends Twig_Extension $isDumpOutputHtmlSafe = extension_loaded('xdebug') && get_cfg_var('xdebug.overload_var_dump') && get_cfg_var('html_errors'); return array( - 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true)), + 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true, 'needs_environment' => true)), ); } @@ -36,12 +36,16 @@ class Twig_Extension_Debug extends Twig_Extension } } -function twig_var_dump($context) +function twig_var_dump(Twig_Environment $env, $context) { + if (!$env->isDebug()) { + return; + } + ob_start(); $count = func_num_args(); - if (1 === $count) { + if (2 === $count) { $vars = array(); foreach ($context as $key => $value) { if (!$value instanceof Twig_Template) { @@ -51,7 +55,7 @@ function twig_var_dump($context) var_dump($vars); } else { - for ($i = 1; $i < $count; $i++) { + for ($i = 2; $i < $count; $i++) { var_dump(func_get_arg($i)); } } diff --git a/test/Twig/Tests/integrationTest.php b/test/Twig/Tests/integrationTest.php index fca8047..b7ce96b 100644 --- a/test/Twig/Tests/integrationTest.php +++ b/test/Twig/Tests/integrationTest.php @@ -57,6 +57,7 @@ class Twig_Tests_IntegrationTest extends PHPUnit_Framework_TestCase ), $match[2] ? eval($match[2].';') : array()); $twig = new Twig_Environment($loader, $config); $twig->addExtension(new TestExtension()); + $twig->addExtension(new Twig_Extension_Debug()); try { $template = $twig->loadTemplate('index.twig');