mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-17 08:50:50 +00:00
Add more types to Template class and compiled templates
This commit is contained in:
committed by
Fabien Potencier
parent
22abdfafae
commit
7e52a68097
+1
-1
@@ -124,7 +124,7 @@ using)::
|
||||
/* Hello {{ name }} */
|
||||
class __TwigTemplate_1121b6f109fe93ebe8c6e22e3712bceb extends Template
|
||||
{
|
||||
protected function doDisplay(array $context, array $blocks = [])
|
||||
protected function doDisplay(array $context, array $blocks = []): iterable
|
||||
{
|
||||
$macros = $this->macros;
|
||||
// line 1
|
||||
|
||||
@@ -32,7 +32,10 @@ class BlockNode extends Node
|
||||
{
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write(\sprintf("public function block_%s(\$context, array \$blocks = [])\n", $this->getAttribute('name')), "{\n")
|
||||
->write("/**\n")
|
||||
->write(" * @return iterable<string>\n")
|
||||
->write(" */\n")
|
||||
->write(\sprintf("public function block_%s(array \$context, array \$blocks = []): iterable\n", $this->getAttribute('name')), "{\n")
|
||||
->indent()
|
||||
->write("\$macros = \$this->macros;\n")
|
||||
;
|
||||
|
||||
@@ -318,7 +318,7 @@ final class ModuleNode extends Node
|
||||
protected function compileDisplay(Compiler $compiler)
|
||||
{
|
||||
$compiler
|
||||
->write("protected function doDisplay(array \$context, array \$blocks = [])\n", "{\n")
|
||||
->write("protected function doDisplay(array \$context, array \$blocks = []): iterable\n", "{\n")
|
||||
->indent()
|
||||
->write("\$macros = \$this->macros;\n")
|
||||
->subcompile($this->getNode('display_start'))
|
||||
|
||||
+16
-18
@@ -74,7 +74,7 @@ abstract class Template
|
||||
*
|
||||
* @return self|TemplateWrapper|false The parent template or false if there is no parent
|
||||
*/
|
||||
public function getParent(array $context)
|
||||
public function getParent(array $context): self|TemplateWrapper|false
|
||||
{
|
||||
if (null !== $this->parent) {
|
||||
return $this->parent;
|
||||
@@ -122,7 +122,7 @@ abstract class Template
|
||||
* @param array $context The context
|
||||
* @param array $blocks The current set of blocks
|
||||
*/
|
||||
public function displayParentBlock($name, array $context, array $blocks = [])
|
||||
public function displayParentBlock($name, array $context, array $blocks = []): void
|
||||
{
|
||||
foreach ($this->yieldParentBlock($name, $context, $blocks) as $data) {
|
||||
echo $data;
|
||||
@@ -140,7 +140,7 @@ abstract class Template
|
||||
* @param array $blocks The current set of blocks
|
||||
* @param bool $useBlocks Whether to use the current set of blocks
|
||||
*/
|
||||
public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null)
|
||||
public function displayBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null): void
|
||||
{
|
||||
foreach ($this->yieldBlock($name, $context, $blocks, $useBlocks, $templateContext) as $data) {
|
||||
echo $data;
|
||||
@@ -159,7 +159,7 @@ abstract class Template
|
||||
*
|
||||
* @return string The rendered block
|
||||
*/
|
||||
public function renderParentBlock($name, array $context, array $blocks = [])
|
||||
public function renderParentBlock($name, array $context, array $blocks = []): string
|
||||
{
|
||||
if (!$this->useYield) {
|
||||
if ($this->env->isDebug()) {
|
||||
@@ -193,7 +193,7 @@ abstract class Template
|
||||
*
|
||||
* @return string The rendered block
|
||||
*/
|
||||
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true)
|
||||
public function renderBlock($name, array $context, array $blocks = [], $useBlocks = true): string
|
||||
{
|
||||
if (!$this->useYield) {
|
||||
$level = ob_get_level();
|
||||
@@ -235,7 +235,7 @@ abstract class Template
|
||||
*
|
||||
* @return bool true if the block exists, false otherwise
|
||||
*/
|
||||
public function hasBlock($name, array $context, array $blocks = [])
|
||||
public function hasBlock($name, array $context, array $blocks = []): bool
|
||||
{
|
||||
if (isset($blocks[$name])) {
|
||||
return $blocks[$name][0] instanceof self;
|
||||
@@ -261,9 +261,9 @@ abstract class Template
|
||||
* @param array $context The context
|
||||
* @param array $blocks The current set of blocks
|
||||
*
|
||||
* @return array An array of block names
|
||||
* @return array<string> An array of block names
|
||||
*/
|
||||
public function getBlockNames(array $context, array $blocks = [])
|
||||
public function getBlockNames(array $context, array $blocks = []): array
|
||||
{
|
||||
$names = array_merge(array_keys($blocks), array_keys($this->blocks));
|
||||
|
||||
@@ -276,10 +276,8 @@ abstract class Template
|
||||
|
||||
/**
|
||||
* @param string|TemplateWrapper|array<string|TemplateWrapper> $template
|
||||
*
|
||||
* @return self|TemplateWrapper
|
||||
*/
|
||||
protected function loadTemplate($template, $templateName = null, $line = null, $index = null)
|
||||
protected function loadTemplate($template, $templateName = null, $line = null, $index = null): self|TemplateWrapper
|
||||
{
|
||||
try {
|
||||
if (\is_array($template)) {
|
||||
@@ -327,10 +325,8 @@ abstract class Template
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function unwrap()
|
||||
public function unwrap(): self
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
@@ -343,7 +339,7 @@ abstract class Template
|
||||
*
|
||||
* @return array An array of blocks
|
||||
*/
|
||||
public function getBlocks()
|
||||
public function getBlocks(): array
|
||||
{
|
||||
return $this->blocks;
|
||||
}
|
||||
@@ -418,7 +414,7 @@ abstract class Template
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null)
|
||||
public function yieldBlock($name, array $context, array $blocks = [], $useBlocks = true, ?self $templateContext = null): iterable
|
||||
{
|
||||
if ($useBlocks && isset($blocks[$name])) {
|
||||
$template = $blocks[$name][0];
|
||||
@@ -478,7 +474,7 @@ abstract class Template
|
||||
*
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function yieldParentBlock($name, array $context, array $blocks = [])
|
||||
public function yieldParentBlock($name, array $context, array $blocks = []): iterable
|
||||
{
|
||||
if (isset($this->traits[$name])) {
|
||||
yield from $this->traits[$name][0]->yieldBlock($name, $context, $blocks, false);
|
||||
@@ -494,6 +490,8 @@ abstract class Template
|
||||
*
|
||||
* @param array $context An array of parameters to pass to the template
|
||||
* @param array $blocks An array of blocks to pass to the template
|
||||
|
||||
* @return iterable<string>
|
||||
*/
|
||||
abstract protected function doDisplay(array $context, array $blocks = []);
|
||||
abstract protected function doDisplay(array $context, array $blocks = []): iterable;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,10 @@ class BlockTest extends NodeTestCase
|
||||
$tests = [];
|
||||
$tests[] = [new BlockNode('foo', new TextNode('foo', 1), 1), <<<EOF
|
||||
// line 1
|
||||
public function block_foo(\$context, array \$blocks = [])
|
||||
/**
|
||||
* @return iterable<string>
|
||||
*/
|
||||
public function block_foo(array \$context, array \$blocks = []): iterable
|
||||
{
|
||||
\$macros = \$this->macros;
|
||||
yield "foo";
|
||||
|
||||
@@ -98,7 +98,7 @@ class __TwigTemplate_%x extends Template
|
||||
];
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = [])
|
||||
protected function doDisplay(array \$context, array \$blocks = []): iterable
|
||||
{
|
||||
\$macros = \$this->macros;
|
||||
// line 1
|
||||
@@ -178,7 +178,7 @@ class __TwigTemplate_%x extends Template
|
||||
return "layout.twig";
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = [])
|
||||
protected function doDisplay(array \$context, array \$blocks = []): iterable
|
||||
{
|
||||
\$macros = \$this->macros;
|
||||
// line 2
|
||||
@@ -273,7 +273,7 @@ class __TwigTemplate_%x extends Template
|
||||
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
|
||||
}
|
||||
|
||||
protected function doDisplay(array \$context, array \$blocks = [])
|
||||
protected function doDisplay(array \$context, array \$blocks = []): iterable
|
||||
{
|
||||
\$macros = \$this->macros;
|
||||
// line 4
|
||||
|
||||
@@ -464,7 +464,7 @@ class TemplateForTest extends Template
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function doDisplay(array $context, array $blocks = [])
|
||||
protected function doDisplay(array $context, array $blocks = []): iterable
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user