Tweak code

This commit is contained in:
Fabien Potencier
2024-01-05 19:16:08 +01:00
parent 15879406c8
commit a9c7307b5c
6 changed files with 50 additions and 25 deletions
+8 -2
View File
@@ -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 @@ class Environment
], $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');
@@ -849,6 +854,7 @@ class Environment
self::VERSION,
(int) $this->debug,
(int) $this->strictVariables,
$this->useYield ? '1' : '0',
]);
}
}
+4 -1
View File
@@ -19,7 +19,10 @@ use Twig\Node\Node;
abstract class NodeTestCase extends TestCase
{
private Environment $currentEnv;
/**
* @var Environment
*/
private $currentEnv;
abstract public function getTests();
+8
View File
@@ -21,6 +21,9 @@ use Twig\Error\RuntimeError;
*/
abstract class YieldingTemplate extends Template
{
/**
* @return iterable<string>
*/
public function yield(array $context, array $blocks = []): iterable
{
$context = $this->env->mergeGlobals($context);
@@ -65,6 +68,9 @@ abstract class YieldingTemplate extends Template
}
}
/**
* @return iterable<string>
*/
public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, Template $templateContext = null)
{
if ($useBlocks && isset($blocks[$name])) {
@@ -133,6 +139,8 @@ abstract class YieldingTemplate extends Template
* @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<string>
*/
public function yieldParentBlock($name, array $context, array $blocks = [])
{
+9 -7
View File
@@ -44,7 +44,8 @@ EOF
, new Environment(new ArrayLoader(), ['use_yield' => true])
];
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
if (!$this->getEnvironment()->useYield()) {
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
@@ -52,10 +53,10 @@ public function block_foo(\$context, array \$blocks = [])
echo "foo";
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => false])
];
$tests[] = [new BlockNode('foo', new Node(), 1), <<<EOF
, new Environment(new ArrayLoader())
];
} else {
$tests[] = [new BlockNode('foo', new Node(), 1), <<<EOF
// line 1
public function block_foo(\$context, array \$blocks = [])
{
@@ -63,8 +64,9 @@ public function block_foo(\$context, array \$blocks = [])
yield;
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => true])
];
, new Environment(new ArrayLoader())
];
}
return $tests;
}
+11 -9
View File
@@ -45,7 +45,8 @@ class MacroTest extends NodeTestCase
$body = new TextNode('foo', 1);
$node = new MacroNode('foo', $body, $arguments, 1);
$text[] = [$node, <<<EOF
if ($this->getEnvironment()->useYield()) {
$text[] = [$node, <<<EOF
// line 1
public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
{
@@ -63,13 +64,13 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
})() ?? new \EmptyIterator())), \$this->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, <<<EOF
$tests[] = [$node, <<<EOF
// line 1
public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
{
@@ -94,8 +95,9 @@ public function macro_foo(\$__foo__ = null, \$__bar__ = "Foo", ...\$__varargs__)
})();
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => false]),
];
, new Environment(new ArrayLoader()),
];
}
return $tests;
}
+10 -6
View File
@@ -48,19 +48,22 @@ class SetTest extends NodeTestCase
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, <<<EOF
if ($this->getEnvironment()->useYield()) {
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = ('' === \$tmp = implode('', iterator_to_array((function () use (&\$context, \$macros, \$blocks) {
yield "foo";
})() ?? new \EmptyIterator()))) ? '' : new Markup(\$tmp, \$this->env->getCharset());
EOF
, new Environment(new ArrayLoader(), ['use_yield' => true]),
];
$tests[] = [$node, <<<EOF
, new Environment(new ArrayLoader()),
];
} else {
$tests[] = [$node, <<<EOF
// line 1
\$context["foo"] = (function () use (&\$context, \$macros, \$blocks) {
ob_start(function () { return ''; });
@@ -73,8 +76,9 @@ EOF
}
})();
EOF
, new Environment(new ArrayLoader(), ['use_yield' => false]),
];
, new Environment(new ArrayLoader()),
];
}
$names = new Node([new AssignNameExpression('foo', 1)], [], 1);
$values = new TextNode('foo', 1);