Skip to content

Commit

Permalink
Remove $templateName from Template::loadTemplate()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Feb 21, 2025
1 parent 1c6ac16 commit d770284
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 42 deletions.
4 changes: 1 addition & 3 deletions src/Node/EmbedNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ public function __construct(string $name, int $index, ?AbstractExpression $varia
protected function addGetTemplate(Compiler $compiler, string $template = ''): void
{
$compiler
->raw('$this->loadTemplate(')
->raw('$this->load(')
->string($this->getAttribute('name'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(', ')
->string($this->getAttribute('index'))
Expand Down
4 changes: 1 addition & 3 deletions src/Node/Expression/BlockReferenceExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ private function compileTemplateCall(Compiler $compiler, string $method): Compil
$compiler->write('$this');
} else {
$compiler
->write('$this->loadTemplate(')
->write('$this->load(')
->subcompile($this->getNode('template'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
Expand Down
4 changes: 1 addition & 3 deletions src/Node/ImportNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ public function compile(Compiler $compiler): void
$compiler->raw('$this');
} else {
$compiler
->raw('$this->loadTemplate(')
->raw('$this->load(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')->unwrap()')
;
Expand Down
4 changes: 1 addition & 3 deletions src/Node/IncludeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,9 @@ public function compile(Compiler $compiler): void
protected function addGetTemplate(Compiler $compiler/* , string $template = '' */)
{
$compiler
->raw('$this->loadTemplate(')
->raw('$this->load(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')')
;
Expand Down
12 changes: 3 additions & 9 deletions src/Node/ModuleNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,9 @@ protected function compileGetParent(Compiler $compiler)
$compiler->subcompile($parent);
} else {
$compiler
->raw('$this->loadTemplate(')
->raw('$this->load(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(')')
;
Expand Down Expand Up @@ -218,11 +216,9 @@ protected function compileConstructor(Compiler $compiler)

$compiler
->addDebugInfo($node)
->write(\sprintf('$_trait_%s = $this->loadTemplate(', $i))
->write(\sprintf('$_trait_%s = $this->load(', $i))
->subcompile($node)
->raw(', ')
->repr($node->getTemplateName())
->raw(', ')
->repr($node->getTemplateLine())
->raw(");\n")
->write(\sprintf("if (!\$_trait_%s->unwrap()->isTraitable()) {\n", $i))
Expand Down Expand Up @@ -353,11 +349,9 @@ protected function compileDisplay(Compiler $compiler)
$compiler->addDebugInfo($parent);
if ($parent instanceof ConstantExpression) {
$compiler
->write('$this->parent = $this->loadTemplate(')
->write('$this->parent = $this->load(')
->subcompile($parent)
->raw(', ')
->repr($this->getSourceContext()->getName())
->raw(', ')
->repr($parent->getTemplateLine())
->raw(");\n")
;
Expand Down
40 changes: 28 additions & 12 deletions src/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function getParent(array $context): self|TemplateWrapper|false
}

if (!isset($this->parents[$parent])) {
$this->parents[$parent] = $this->loadTemplate($parent);
$this->parents[$parent] = $this->load($parent, -1);
}

return $this->parents[$parent];
Expand Down Expand Up @@ -270,21 +270,15 @@ public function getBlockNames(array $context, array $blocks = []): array
/**
* @param string|TemplateWrapper|array<string|TemplateWrapper> $template
*/
protected function loadTemplate($template, $templateName = null, $line = null, $index = null): self|TemplateWrapper
protected function load(string|TemplateWrapper|array $template, int $line, int|null $index = null): self
{
try {
if (\is_array($template)) {
return $this->env->resolveTemplate($template);
return $this->env->resolveTemplate($template)->unwrap();
}

if ($template instanceof TemplateWrapper) {
return $template;
}

if ($template instanceof self) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', self::class, __METHOD__);

return $template;
return $template->unwrap();
}

if ($template === $this->getTemplateName()) {
Expand All @@ -299,14 +293,14 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
return $this->env->loadTemplate($class, $template, $index);
} catch (Error $e) {
if (!$e->getSourceContext()) {
$e->setSourceContext($templateName ? new Source('', $templateName) : $this->getSourceContext());
$e->setSourceContext($this->getSourceContext());
}

if ($e->getTemplateLine() > 0) {
throw $e;
}

if (!$line) {
if (-1 === $line) {
$e->guess();
} else {
$e->setTemplateLine($line);
Expand All @@ -316,6 +310,28 @@ protected function loadTemplate($template, $templateName = null, $line = null, $
}
}

/**
* @param string|TemplateWrapper|array<string|TemplateWrapper> $template
*/
protected function loadTemplate($template, $templateName = null, int|null $line = null, int|null $index = null): self|TemplateWrapper
{
trigger_deprecation('twig/twig', '3.21', 'The "%s" method is deprecated.', __METHOD__);

if (null === $line) {
trigger_deprecation('twig/twig', '3.21', 'Passing a "null" line number to "%s" is deprecated.', __METHOD__);

$line = -1;
}

if ($template instanceof self) {
trigger_deprecation('twig/twig', '3.9', 'Passing a "%s" instance to "%s" is deprecated.', self::class, __METHOD__);

return $template;
}

return $this->load($template, $line, $index);
}

/**
* @internal
*
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/templates/include.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@



{% extends 'invalid.twig' %}
1 change: 1 addition & 0 deletions tests/Fixtures/templates/index.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% include "include.twig" %}
65 changes: 65 additions & 0 deletions tests/Node/ExtendsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Twig\Tests\Node;

/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ArrayLoader;
use Twig\Loader\FilesystemLoader;
use Twig\Test\NodeTestCase;

class ExtendsTest extends NodeTestCase
{
public function testErrorFromArrayLoader()
{
$twig = new Environment(new ArrayLoader([
'index.twig' => '{% include "include.twig" %}',
'include.twig' => $include = <<<EOF
{% extends 'invalid.twig' %}
EOF,
]), ['debug' => true]);
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertSame('Template "invalid.twig" is not defined.', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}

public function testErrorFromFilesystemLoader()
{
$twig = new Environment(new FilesystemLoader([
$dir = dirname(__DIR__).'/Fixtures/templates',
]), ['debug' => true]);
$include = file_get_contents($dir.'/include.twig');
try {
$twig->render('index.twig');
$this->fail('Expected LoaderError to be thrown');
} catch (LoaderError $e) {
$this->assertStringContainsString('Unable to find template "invalid.twig"', $e->getRawMessage());
$this->assertSame(4, $e->getTemplateLine());
$this->assertSame('include.twig', $e->getSourceContext()->getName());
$this->assertSame($include, $e->getSourceContext()->getCode());
}
}

public static function provideTests(): iterable
{
return [];
}
}
2 changes: 1 addition & 1 deletion tests/Node/ImportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static function provideTests(): iterable

$tests[] = [$node, <<<EOF
// line 1
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", null, 1)->unwrap();
\$macros["macro"] = \$this->macros["macro"] = \$this->load("foo.twig", 1)->unwrap();
EOF
];

Expand Down
10 changes: 5 additions & 5 deletions tests/Node/IncludeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, null, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield($context);
yield from $this->load("foo.twig", 1)->unwrap()->yield($context);
EOF
];

Expand All @@ -55,7 +55,7 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, null, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate(((true) ? ("foo") : ("foo")), null, 1)->unwrap()->yield($context);
yield from $this->load(((true) ? ("foo") : ("foo")), 1)->unwrap()->yield($context);
EOF
];

Expand All @@ -64,22 +64,22 @@ public static function provideTests(): iterable
$node = new IncludeNode($expr, $vars, false, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield(CoreExtension::merge($context, ["foo" => true]));
yield from $this->load("foo.twig", 1)->unwrap()->yield(CoreExtension::merge($context, ["foo" => true]));
EOF
];

$node = new IncludeNode($expr, $vars, true, false, 1);
$tests[] = [$node, <<<'EOF'
// line 1
yield from $this->loadTemplate("foo.twig", null, 1)->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
yield from $this->load("foo.twig", 1)->unwrap()->yield(CoreExtension::toArray(["foo" => true]));
EOF
];

$node = new IncludeNode($expr, $vars, true, true, 1);
$tests[] = [$node, <<<EOF
// line 1
try {
\$_v%s = \$this->loadTemplate("foo.twig", null, 1);
\$_v%s = \$this->load("foo.twig", 1);
} catch (LoaderError \$e) {
// ignore missing template
\$_v%s = null;
Expand Down
6 changes: 3 additions & 3 deletions tests/Node/ModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,9 @@ protected function doDisplay(array \$context, array \$blocks = []): iterable
{
\$macros = \$this->macros;
// line 2
\$macros["macro"] = \$this->macros["macro"] = \$this->loadTemplate("foo.twig", "foo.twig", 2)->unwrap();
\$macros["macro"] = \$this->macros["macro"] = \$this->load("foo.twig", 2)->unwrap();
// line 1
\$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1);
\$this->parent = \$this->load("layout.twig", 1);
yield from \$this->parent->unwrap()->yield(\$context, array_merge(\$this->blocks, \$blocks));
}
Expand Down Expand Up @@ -271,7 +271,7 @@ public function __construct(Environment \$env)
protected function doGetParent(array \$context): bool|string|Template|TemplateWrapper
{
// line 2
return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2);
return \$this->load(((true) ? ("foo") : ("foo")), 2);
}
protected function doDisplay(array \$context, array \$blocks = []): iterable
Expand Down

0 comments on commit d770284

Please sign in to comment.