From 9d2e3402355f202df1ef14bb55cca9a1b5fb3a74 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 5 Jan 2024 19:16:08 +0100 Subject: [PATCH] Tweak code --- src/Environment.php | 10 ++++++++-- src/Test/NodeTestCase.php | 5 ++++- src/YieldingTemplate.php | 8 ++++++++ tests/Node/BlockTest.php | 16 +++++++++------- tests/Node/MacroTest.php | 20 +++++++++++--------- tests/Node/SetTest.php | 16 ++++++++++------ 6 files changed, 50 insertions(+), 25 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 48fefe675ee..efe9892b561 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -66,6 +66,7 @@ class Environment private $runtimeLoaders = []; private $runtimes = []; private $optionsHash; + /** @var bool */ private $useYield; /** @@ -99,7 +100,9 @@ class Environment * (default to -1 which means that all optimizations are enabled; * set it to 0 to disable). * - * * use_yield: Enable the Twig 4 mode where template are using yield instead of echo + * * use_yield: Enable a new mode where template are using "yield" instead of "echo" + * (default to "false", but switch it to "true" when possible + * as this will be the only supported mode in Twig 4.0) */ public function __construct(LoaderInterface $loader, $options = []) { @@ -117,7 +120,9 @@ public function __construct(LoaderInterface $loader, $options = []) ], $options); $this->useYield = (bool) $options['use_yield']; - // FIXME: deprecation if use_yield is false + if (!$this->useYield) { + trigger_deprecation('twig/twig', '3.9.0', 'Not setting "use_yield" to "true" is deprecated.'); + } $this->debug = (bool) $options['debug']; $this->setCharset($options['charset'] ?? 'UTF-8'); @@ -850,6 +855,7 @@ private function updateOptionsHash(): void self::VERSION, (int) $this->debug, (int) $this->strictVariables, + $this->useYield ? '1' : '0', ]); } } diff --git a/src/Test/NodeTestCase.php b/src/Test/NodeTestCase.php index b10ae11d7e1..8df0e9fee3a 100644 --- a/src/Test/NodeTestCase.php +++ b/src/Test/NodeTestCase.php @@ -19,7 +19,10 @@ abstract class NodeTestCase extends TestCase { - private Environment $currentEnv; + /** + * @var Environment + */ + private $currentEnv; abstract public function getTests(); diff --git a/src/YieldingTemplate.php b/src/YieldingTemplate.php index e614d6bd512..9f8cb418b8b 100644 --- a/src/YieldingTemplate.php +++ b/src/YieldingTemplate.php @@ -21,6 +21,9 @@ */ abstract class YieldingTemplate extends Template { + /** + * @return iterable + */ public function yield(array $context, array $blocks = []): iterable { $context = $this->env->mergeGlobals($context); @@ -65,6 +68,9 @@ public function display(array $context, array $blocks = []): void } } + /** + * @return iterable + */ public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, Template $templateContext = null) { if ($useBlocks && isset($blocks[$name])) { @@ -133,6 +139,8 @@ public function renderBlock($name, array $context, array $blocks = [], $useBlock * @param string $name The block name to display from the parent * @param array $context The context * @param array $blocks The current set of blocks + * + * @return iterable */ public function yieldParentBlock($name, array $context, array $blocks = []) { diff --git a/tests/Node/BlockTest.php b/tests/Node/BlockTest.php index 29b2e0f9d35..0edc0fc033d 100644 --- a/tests/Node/BlockTest.php +++ b/tests/Node/BlockTest.php @@ -44,7 +44,8 @@ public function block_foo(\$context, array \$blocks = []) , new Environment(new ArrayLoader(), ['use_yield' => true]) ]; - $tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<getEnvironment()->useYield()) { + $tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), << false]) - ]; - - $tests[] = [new BlockNode('foo', new Node(), 1), << true]) - ]; + , new Environment(new ArrayLoader()) + ]; + } return $tests; } diff --git a/tests/Node/MacroTest.php b/tests/Node/MacroTest.php index 7efea3cefc6..948949af66d 100644 --- a/tests/Node/MacroTest.php +++ b/tests/Node/MacroTest.php @@ -45,7 +45,8 @@ public function getTests() $body = new TextNode('foo', 1); $node = new MacroNode('foo', $body, $arguments, 1); - $text[] = [$node, <<getEnvironment()->useYield()) { + $text[] = [$node, <<env->getCharset()); } EOF - , new Environment(new ArrayLoader(), ['use_yield' => true]), - ]; + , new Environment(new ArrayLoader()), + ]; + } else { + $body = new TextNode('foo', 1); + $node = new MacroNode('foo', $body, $arguments, 1); - $body = new TextNode('foo', 1); - $node = new MacroNode('foo', $body, $arguments, 1); - - $tests[] = [$node, << false]), - ]; + , new Environment(new ArrayLoader()), + ]; + } return $tests; } diff --git a/tests/Node/SetTest.php b/tests/Node/SetTest.php index 70b3530bd78..84d8a77c087 100644 --- a/tests/Node/SetTest.php +++ b/tests/Node/SetTest.php @@ -48,19 +48,22 @@ public function getTests() EOF ]; + $names = new Node([new AssignNameExpression('foo', 1)], [], 1); $values = new Node([new PrintNode(new ConstantExpression('foo', 1), 1)], [], 1); $node = new SetNode(true, $names, $values, 1); - $tests[] = [$node, <<getEnvironment()->useYield()) { + $tests[] = [$node, <<env->getCharset()); EOF - , new Environment(new ArrayLoader(), ['use_yield' => true]), - ]; - $tests[] = [$node, << false]), - ]; + , new Environment(new ArrayLoader()), + ]; + } $names = new Node([new AssignNameExpression('foo', 1)], [], 1); $values = new TextNode('foo', 1);