From f2f1f32b410e7ccf0d9f1591ec42eb300fdee339 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 3 Apr 2012 19:13:43 +0200 Subject: [PATCH] added Twig_Environment::mergeGlobals() --- CHANGELOG | 1 + lib/Twig/Environment.php | 20 ++++++++++++++++++++ lib/Twig/Node/Macro.php | 2 +- lib/Twig/Template.php | 15 +-------------- test/Twig/Tests/Fixtures/tags/macro/global.test | 14 ++++++++++++++ test/Twig/Tests/Node/MacroTest.php | 2 +- 6 files changed, 38 insertions(+), 16 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/macro/global.test diff --git a/CHANGELOG b/CHANGELOG index b0e96ec..e457b35 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.7.0 (2012-XX-XX) + * added Twig_Environment::mergeGlobals() * fixed a regression when a template only extends another one without defining any blocks * added compilation checks to avoid misuses of the sandbox tag * fixed filesystem loader freshness logic for high traffic websites diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index e69ff9d..6c47326 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -973,6 +973,26 @@ class Twig_Environment } /** + * Merges a context with the defined globals. + * + * @param array $context An array representing the context + * + * @return array The context merged with the globals + */ + public function mergeGlobals(array $context) + { + // we don't use array_merge as the context being generally + // bigger than globals, this code is faster. + foreach ($this->getGlobals() as $key => $value) { + if (!array_key_exists($key, $context)) { + $context[$key] = $value; + } + } + + return $context; + } + + /** * Gets the registered unary Operators. * * @return array An array of unary operators diff --git a/lib/Twig/Node/Macro.php b/lib/Twig/Node/Macro.php index 11f388b..e0c3dca 100644 --- a/lib/Twig/Node/Macro.php +++ b/lib/Twig/Node/Macro.php @@ -44,7 +44,7 @@ class Twig_Node_Macro extends Twig_Node $compiler->write("\$context = \$this->env->getGlobals();\n\n"); } else { $compiler - ->write("\$context = \$this->mergeContextWithGlobals(array(\n") + ->write("\$context = \$this->env->mergeGlobals(array(\n") ->indent() ; diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 1862064..3cce8bb 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -236,7 +236,7 @@ abstract class Twig_Template implements Twig_TemplateInterface */ public function display(array $context, array $blocks = array()) { - $this->displayWithErrorHandling($this->mergeContextWithGlobals($context), $blocks); + $this->displayWithErrorHandling($this->env->mergeGlobals($context), $blocks); } /** @@ -259,19 +259,6 @@ abstract class Twig_Template implements Twig_TemplateInterface return ob_get_clean(); } - protected function mergeContextWithGlobals(array $context) - { - // we don't use array_merge as the context being generally - // bigger than globals, this code is faster. - foreach ($this->env->getGlobals() as $key => $value) { - if (!array_key_exists($key, $context)) { - $context[$key] = $value; - } - } - - return $context; - } - protected function displayWithErrorHandling(array $context, array $blocks = array()) { try { diff --git a/test/Twig/Tests/Fixtures/tags/macro/global.test b/test/Twig/Tests/Fixtures/tags/macro/global.test new file mode 100644 index 0000000..6b37176 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/macro/global.test @@ -0,0 +1,14 @@ +--TEST-- +"macro" tag +--TEMPLATE-- +{% from 'forms.twig' import foo %} + +{{ foo('foo') }} +{{ foo() }} +--TEMPLATE(forms.twig)-- +{% macro foo(name) %}{{ name|default('foo') }}{{ global }}{% endmacro %} +--DATA-- +return array() +--EXPECT-- +fooglobal +fooglobal diff --git a/test/Twig/Tests/Node/MacroTest.php b/test/Twig/Tests/Node/MacroTest.php index f61c698..3dd57a6 100644 --- a/test/Twig/Tests/Node/MacroTest.php +++ b/test/Twig/Tests/Node/MacroTest.php @@ -46,7 +46,7 @@ class Twig_Tests_Node_MacroTest extends Twig_Tests_Node_TestCase array($node, <<mergeContextWithGlobals(array( + \$context = \$this->env->mergeGlobals(array( "foo" => \$foo, )); -- 1.7.2.5