From b86575cfd6ad0c1872a2c16aefaeb6a0c8ef8174 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 7 Sep 2024 14:00:41 +0200 Subject: [PATCH] Deprecate Environment::mergeGlobals() --- CHANGELOG | 2 +- doc/deprecated.rst | 14 ++++++++++++++ src/Environment.php | 13 +++++-------- src/Node/MacroNode.php | 4 ++-- src/Node/WithNode.php | 2 +- src/Template.php | 2 +- src/TemplateWrapper.php | 4 ++-- tests/Node/MacroTest.php | 8 ++++---- 8 files changed, 30 insertions(+), 19 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 70dc4acd8..c0f26c20d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,6 @@ # 3.13.1 (2024-XX-XX) - * n/a + * Deprecate `Environment::mergeGlobals()` # 3.13.0 (2024-09-07) diff --git a/doc/deprecated.rst b/doc/deprecated.rst index e28ecc62b..0490c2ded 100644 --- a/doc/deprecated.rst +++ b/doc/deprecated.rst @@ -223,3 +223,17 @@ Testing Utilities * The data providers ``getTests()`` and ``getLegacyTests()`` on ``Twig\Test\IntegrationTestCase`` are considered final als of Twig 3.13. + +Environment +----------- + +* The ``Twig\Environment::mergeGlobals()`` method is deprecated as of Twig 3.13 + and will be removed in Twig 4.0: + + Before:: + + $context = $twig->mergeGlobals($context); + + After:: + + $context += $twig->getGlobals(); diff --git a/src/Environment.php b/src/Environment.php index 1456d3744..297efa9cc 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -830,17 +830,14 @@ class Environment return array_merge($this->extensionSet->getGlobals(), $this->globals); } + /** + * @deprecated since Twig 3.13 + */ public function mergeGlobals(array $context): array { - // 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; - } - } + trigger_deprecation('twig/twig', '3.13', 'The "%s" method is deprecated.', __METHOD__); - return $context; + return $context + $this->getGlobals(); } /** diff --git a/src/Node/MacroNode.php b/src/Node/MacroNode.php index f3e9d7a10..5a2543a9f 100644 --- a/src/Node/MacroNode.php +++ b/src/Node/MacroNode.php @@ -73,7 +73,7 @@ class MacroNode extends Node ->write("{\n") ->indent() ->write("\$macros = \$this->macros;\n") - ->write("\$context = \$this->env->mergeGlobals([\n") + ->write("\$context = [\n") ->indent() ; @@ -94,7 +94,7 @@ class MacroNode extends Node ->raw(' => ') ->raw("\$__varargs__,\n") ->outdent() - ->write("]);\n\n") + ->write("] + \$this->env->getGlobals();\n\n") ->write("\$blocks = [];\n\n") ->write('return ') ->subcompile($node) diff --git a/src/Node/WithNode.php b/src/Node/WithNode.php index f9104948b..487e2800b 100644 --- a/src/Node/WithNode.php +++ b/src/Node/WithNode.php @@ -61,7 +61,7 @@ class WithNode extends Node $compiler->write("\$context = [];\n"); } - $compiler->write(\sprintf("\$context = \$this->env->mergeGlobals(array_merge(\$context, \$%s));\n", $varsName)); + $compiler->write(\sprintf("\$context = \$%s + \$context + \$this->env->getGlobals();\n", $varsName)); } $compiler diff --git a/src/Template.php b/src/Template.php index e8368c5d9..7b3ce8161 100644 --- a/src/Template.php +++ b/src/Template.php @@ -386,7 +386,7 @@ abstract class Template */ public function yield(array $context, array $blocks = []): iterable { - $context = $this->env->mergeGlobals($context); + $context += $this->env->getGlobals(); $blocks = array_merge($this->blocks, $blocks); try { diff --git a/src/TemplateWrapper.php b/src/TemplateWrapper.php index c31f50161..135c59188 100644 --- a/src/TemplateWrapper.php +++ b/src/TemplateWrapper.php @@ -57,12 +57,12 @@ final class TemplateWrapper public function renderBlock(string $name, array $context = []): string { - return $this->template->renderBlock($name, $this->env->mergeGlobals($context)); + return $this->template->renderBlock($name, $context + $this->env->getGlobals()); } public function displayBlock(string $name, array $context = []) { - $context = $this->env->mergeGlobals($context); + $context += $this->env->getGlobals(); foreach ($this->template->yieldBlock($name, $context) as $data) { echo $data; } diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index f738c275f..6fb6062cb 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -49,11 +49,11 @@ class MacroTest extends NodeTestCase public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) { \$macros = \$this->macros; - \$context = \$this->env->mergeGlobals([ + \$context = [ "foo" => \$__foo__, "bar" => \$__bar__, "varargs" => \$__varargs__, - ]); + ] + \$this->env->getGlobals(); \$blocks = []; @@ -71,11 +71,11 @@ EOF public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__) { \$macros = \$this->macros; - \$context = \$this->env->mergeGlobals([ + \$context = [ "foo" => \$__foo__, "bar" => \$__bar__, "varargs" => \$__varargs__, - ]); + ] + \$this->env->getGlobals(); \$blocks = [];