Skip to content

Commit

Permalink
Tweak code
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jan 6, 2024
1 parent 89a8f59 commit 9d2e340
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 25 deletions.
10 changes: 8 additions & 2 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Environment
private $runtimeLoaders = [];
private $runtimes = [];
private $optionsHash;
/** @var bool */
private $useYield;

/**
Expand Down Expand Up @@ -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 = [])
{
Expand All @@ -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');
Expand Down Expand Up @@ -850,6 +855,7 @@ private function updateOptionsHash(): void
self::VERSION,
(int) $this->debug,
(int) $this->strictVariables,
$this->useYield ? '1' : '0',
]);
}
}
5 changes: 4 additions & 1 deletion src/Test/NodeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

abstract class NodeTestCase extends TestCase
{
private Environment $currentEnv;
/**
* @var Environment
*/
private $currentEnv;

abstract public function getTests();

Expand Down
8 changes: 8 additions & 0 deletions src/YieldingTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
abstract class YieldingTemplate extends Template
{
/**
* @return iterable<string>
*/
public function yield(array $context, array $blocks = []): iterable
{
$context = $this->env->mergeGlobals($context);
Expand Down Expand Up @@ -65,6 +68,9 @@ public function display(array $context, array $blocks = []): void
}
}

/**
* @return iterable<string>
*/
public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, Template $templateContext = null)
{
if ($useBlocks && isset($blocks[$name])) {
Expand Down Expand Up @@ -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<string>
*/
public function yieldParentBlock($name, array $context, array $blocks = [])
{
Expand Down
16 changes: 9 additions & 7 deletions tests/Node/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,29 @@ public function block_foo(\$context, array \$blocks = [])
, 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 = [])
{
\$macros = \$this->macros;
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 = [])
{
\$macros = \$this->macros;
yield;
}
EOF
, new Environment(new ArrayLoader(), ['use_yield' => true])
];
, new Environment(new ArrayLoader())
];
}

return $tests;
}
Expand Down
20 changes: 11 additions & 9 deletions tests/Node/MacroTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function getTests()
$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__)
{
Expand All @@ -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__)
{
Expand All @@ -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;
}
Expand Down
16 changes: 10 additions & 6 deletions tests/Node/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, <<<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 ''; });
Expand All @@ -73,8 +76,9 @@ public function getTests()
}
})();
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);
Expand Down

0 comments on commit 9d2e340

Please sign in to comment.