From c82d7dcaab40edb7e0ba39894a76c352283e4781 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Mar 2019 17:54:51 +0100 Subject: [PATCH 1/3] added the possibility to pass a TemplateWrapper to Twig\Environment::load() --- CHANGELOG | 1 + src/Environment.php | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7efc6003a..510a84bab 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.38.0 (2019-XX-XX) + * added the possibility to pass a TemplateWrapper to Twig\Environment::load() * improved the performance of the sandbox * added a spaceless filter * added max value to the "random" function diff --git a/src/Environment.php b/src/Environment.php index 2f6be7eb6..7b8b31a99 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -371,8 +371,8 @@ class Environment /** * Renders a template. * - * @param string $name The template name - * @param array $context An array of parameters to pass to the template + * @param string|TemplateWrapper $name The template name + * @param array $context An array of parameters to pass to the template * * @return string The rendered template * @@ -382,14 +382,14 @@ class Environment */ public function render($name, array $context = []) { - return $this->loadTemplate($name)->render($context); + return $this->load($name)->render($context); } /** * Displays a template. * - * @param string $name The template name - * @param array $context An array of parameters to pass to the template + * @param string|TemplateWrapper $name The template name + * @param array $context An array of parameters to pass to the template * * @throws LoaderError When the template cannot be found * @throws SyntaxError When an error occurred during compilation @@ -397,7 +397,7 @@ class Environment */ public function display($name, array $context = []) { - $this->loadTemplate($name)->display($context); + $this->load($name)->display($context); } /** From e2103c87ae05bceee9152dc4d303a3a860c6701c Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Mar 2019 18:39:46 +0100 Subject: [PATCH 2/3] use load() instead of loadTemplate() in tests --- src/TemplateWrapper.php | 2 +- src/Test/IntegrationTestCase.php | 2 +- test/Twig/Tests/ErrorTest.php | 10 ++-- test/Twig/Tests/Extension/SandboxTest.php | 48 +++++++++---------- .../functions/block_with_template.test | 2 +- .../functions/include/template_instance.test | 2 +- .../tags/include/template_instance.test | 4 +- .../tags/inheritance/template_instance.test | 2 +- .../defined_for_blocks_with_template.test | 2 +- test/Twig/Tests/Loader/FilesystemTest.php | 6 +-- test/Twig/Tests/TemplateTest.php | 2 +- test/Twig/Tests/TemplateWrapperTest.php | 10 ++-- 12 files changed, 46 insertions(+), 46 deletions(-) diff --git a/src/TemplateWrapper.php b/src/TemplateWrapper.php index 6dbce0a28..1bd332590 100644 --- a/src/TemplateWrapper.php +++ b/src/TemplateWrapper.php @@ -56,7 +56,7 @@ final class TemplateWrapper { // using func_get_args() allows to not expose the blocks argument // as it should only be used by internal code - $this->template->display($context, \func_num_args() >= 1 ? func_get_arg(1) : []); + $this->template->display($context, \func_num_args() > 1 ? func_get_arg(1) : []); } /** diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index 1ee81c69f..d1b633ab9 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -188,7 +188,7 @@ abstract class IntegrationTestCase extends TestCase $p->setValue($twig, '__TwigTemplate_'.hash('sha256', uniqid(mt_rand(), true), false).'_'); try { - $template = $twig->loadTemplate('index.twig'); + $template = $twig->load('index.twig'); } catch (\Exception $e) { if (false !== $exception) { $message = $e->getMessage(); diff --git a/test/Twig/Tests/ErrorTest.php b/test/Twig/Tests/ErrorTest.php index c20a3c638..52010611a 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -50,7 +50,7 @@ EOHTML ]); $twig = new Environment($loader, ['strict_variables' => true, 'debug' => true, 'cache' => false]); - $template = $twig->loadTemplate('index.html'); + $template = $twig->load('index.html'); try { $template->render([]); @@ -78,7 +78,7 @@ EOHTML ]); $twig = new Environment($loader, ['strict_variables' => true, 'debug' => true, 'cache' => false]); - $template = $twig->loadTemplate('index.html'); + $template = $twig->load('index.html'); try { $template->render(['foo' => new Twig_Tests_ErrorTest_Foo()]); @@ -95,7 +95,7 @@ EOHTML $loader = new FilesystemLoader(__DIR__.'/Fixtures/errors'); $twig = new Environment($loader, ['strict_variables' => true, 'debug' => true, 'cache' => false]); - $template = $twig->loadTemplate('index.html'); + $template = $twig->load('index.html'); try { $template->render([]); @@ -114,7 +114,7 @@ EOHTML $loader = new FilesystemLoader(__DIR__.'/Fixtures/errors'); $twig = new Environment($loader, ['strict_variables' => true, 'debug' => true, 'cache' => false]); - $template = $twig->loadTemplate('index.html'); + $template = $twig->load('index.html'); try { $template->render(['foo' => new Twig_Tests_ErrorTest_Foo()]); @@ -136,7 +136,7 @@ EOHTML $loader = new ArrayLoader($templates); $twig = new Environment($loader, ['strict_variables' => true, 'debug' => true, 'cache' => false]); - $template = $twig->loadTemplate('index'); + $template = $twig->load('index'); try { $template->render([]); diff --git a/test/Twig/Tests/Extension/SandboxTest.php b/test/Twig/Tests/Extension/SandboxTest.php index b686ad72d..e554bfbc3 100644 --- a/test/Twig/Tests/Extension/SandboxTest.php +++ b/test/Twig/Tests/Extension/SandboxTest.php @@ -53,20 +53,20 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase public function testSandboxWithInheritance() { $twig = $this->getEnvironment(true, [], self::$templates, ['block']); - $twig->loadTemplate('1_child')->render([]); + $twig->load('1_child')->render([]); } public function testSandboxGloballySet() { $twig = $this->getEnvironment(false, [], self::$templates); - $this->assertEquals('FOO', $twig->loadTemplate('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally'); + $this->assertEquals('FOO', $twig->load('1_basic')->render(self::$params), 'Sandbox does nothing if it is disabled globally'); } public function testSandboxUnallowedMethodAccessor() { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic1')->render(self::$params); + $twig->load('1_basic1')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed method is called'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedMethodError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -79,7 +79,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic2')->render(self::$params); + $twig->load('1_basic2')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed filter is called'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFilterError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); @@ -91,7 +91,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic3')->render(self::$params); + $twig->load('1_basic3')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedTagError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedTagError'); @@ -103,7 +103,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic4')->render(self::$params); + $twig->load('1_basic4')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed property is called in the template'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedPropertyError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedPropertyError'); @@ -116,7 +116,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic5')->render(self::$params); + $twig->load('1_basic5')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) is called in the template'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedMethodError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -129,7 +129,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic6')->render(self::$params); + $twig->load('1_basic6')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed method (__toString()) is called in the template'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedMethodError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -142,7 +142,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_basic7')->render(self::$params); + $twig->load('1_basic7')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if an unallowed function is called in the template'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFunctionError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFunctionError'); @@ -154,7 +154,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('1_range_operator')->render(self::$params); + $twig->load('1_range_operator')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception if the unallowed range operator is called'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedFunctionError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFunctionError'); @@ -166,7 +166,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates, [], [], ['FooObject' => 'foo']); FooObject::reset(); - $this->assertEquals('foo', $twig->loadTemplate('1_basic1')->render(self::$params), 'Sandbox allow some methods'); + $this->assertEquals('foo', $twig->load('1_basic1')->render(self::$params), 'Sandbox allow some methods'); $this->assertEquals(1, FooObject::$called['foo'], 'Sandbox only calls method once'); } @@ -174,7 +174,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(true, [], self::$templates, [], [], ['FooObject' => '__toString']); FooObject::reset(); - $this->assertEquals('foo', $twig->loadTemplate('1_basic5')->render(self::$params), 'Sandbox allow some methods'); + $this->assertEquals('foo', $twig->load('1_basic5')->render(self::$params), 'Sandbox allow some methods'); $this->assertEquals(1, FooObject::$called['__toString'], 'Sandbox only calls method once'); } @@ -182,38 +182,38 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase { $twig = $this->getEnvironment(false, [], self::$templates); FooObject::reset(); - $this->assertEquals('foo', $twig->loadTemplate('1_basic5')->render(self::$params), 'Sandbox allows __toString when sandbox disabled'); + $this->assertEquals('foo', $twig->load('1_basic5')->render(self::$params), 'Sandbox allows __toString when sandbox disabled'); $this->assertEquals(1, FooObject::$called['__toString'], 'Sandbox only calls method once'); } public function testSandboxAllowFilter() { $twig = $this->getEnvironment(true, [], self::$templates, [], ['upper']); - $this->assertEquals('FABIEN', $twig->loadTemplate('1_basic2')->render(self::$params), 'Sandbox allow some filters'); + $this->assertEquals('FABIEN', $twig->load('1_basic2')->render(self::$params), 'Sandbox allow some filters'); } public function testSandboxAllowTag() { $twig = $this->getEnvironment(true, [], self::$templates, ['if']); - $this->assertEquals('foo', $twig->loadTemplate('1_basic3')->render(self::$params), 'Sandbox allow some tags'); + $this->assertEquals('foo', $twig->load('1_basic3')->render(self::$params), 'Sandbox allow some tags'); } public function testSandboxAllowProperty() { $twig = $this->getEnvironment(true, [], self::$templates, [], [], [], ['FooObject' => 'bar']); - $this->assertEquals('bar', $twig->loadTemplate('1_basic4')->render(self::$params), 'Sandbox allow some properties'); + $this->assertEquals('bar', $twig->load('1_basic4')->render(self::$params), 'Sandbox allow some properties'); } public function testSandboxAllowFunction() { $twig = $this->getEnvironment(true, [], self::$templates, [], [], [], [], ['cycle']); - $this->assertEquals('bar', $twig->loadTemplate('1_basic7')->render(self::$params), 'Sandbox allow some functions'); + $this->assertEquals('bar', $twig->load('1_basic7')->render(self::$params), 'Sandbox allow some functions'); } public function testSandboxAllowRangeOperator() { $twig = $this->getEnvironment(true, [], self::$templates, [], [], [], [], ['range']); - $this->assertEquals('1', $twig->loadTemplate('1_range_operator')->render(self::$params), 'Sandbox allow the range operator'); + $this->assertEquals('1', $twig->load('1_range_operator')->render(self::$params), 'Sandbox allow the range operator'); } public function testSandboxAllowFunctionsCaseInsensitive() @@ -221,10 +221,10 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase foreach (['getfoobar', 'getFoobar', 'getFooBar'] as $name) { $twig = $this->getEnvironment(true, [], self::$templates, [], [], ['FooObject' => $name]); FooObject::reset(); - $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic8')->render(self::$params), 'Sandbox allow methods in a case-insensitive way'); + $this->assertEquals('foobarfoobar', $twig->load('1_basic8')->render(self::$params), 'Sandbox allow methods in a case-insensitive way'); $this->assertEquals(2, FooObject::$called['getFooBar'], 'Sandbox only calls method once'); - $this->assertEquals('foobarfoobar', $twig->loadTemplate('1_basic9')->render(self::$params), 'Sandbox allow methods via shortcut names (ie. without get/set)'); + $this->assertEquals('foobarfoobar', $twig->load('1_basic9')->render(self::$params), 'Sandbox allow methods via shortcut names (ie. without get/set)'); } } @@ -236,7 +236,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase ]; $twig = $this->getEnvironment(false, [], self::$templates); - $this->assertEquals('fooFOOfoo', $twig->loadTemplate('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include'); + $this->assertEquals('fooFOOfoo', $twig->load('2_basic')->render(self::$params), 'Sandbox does nothing if disabled globally and sandboxed not used for the include'); self::$templates = [ '3_basic' => '{{ obj.foo }}{% sandbox %}{% include "3_included" %}{% endsandbox %}{{ obj.foo }}', @@ -245,7 +245,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase $twig = $this->getEnvironment(true, [], self::$templates); try { - $twig->loadTemplate('3_basic')->render(self::$params); + $twig->load('3_basic')->render(self::$params); $this->fail('Sandbox throws a SecurityError exception when the included file is sandboxed'); } catch (SecurityError $e) { $this->assertInstanceOf('\Twig\Sandbox\SecurityNotAllowedTagError', $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedTagError'); @@ -264,7 +264,7 @@ class Twig_Tests_Extension_SandboxTest extends \PHPUnit\Framework\TestCase EOF ], ['macro', 'import'], ['escape']); - $this->assertEquals('

username

', $twig->loadTemplate('index')->render([])); + $this->assertEquals('

username

', $twig->load('index')->render([])); } public function testSandboxDisabledAfterIncludeFunctionError() @@ -273,7 +273,7 @@ EOF $e = null; try { - $twig->loadTemplate('1_include')->render(self::$params); + $twig->load('1_include')->render(self::$params); } catch (\Throwable $e) { } catch (\Exception $e) { } diff --git a/test/Twig/Tests/Fixtures/functions/block_with_template.test b/test/Twig/Tests/Fixtures/functions/block_with_template.test index 3d403d5b7..37cb7a481 100644 --- a/test/Twig/Tests/Fixtures/functions/block_with_template.test +++ b/test/Twig/Tests/Fixtures/functions/block_with_template.test @@ -12,7 +12,7 @@ --DATA-- return [ 'included_loaded' => $twig->load('included.twig'), - 'included_loaded_internal' => $twig->loadTemplate('included.twig'), + 'included_loaded_internal' => $twig->load('included.twig'), ] --EXPECT-- FOO diff --git a/test/Twig/Tests/Fixtures/functions/include/template_instance.test b/test/Twig/Tests/Fixtures/functions/include/template_instance.test index 8ba956b89..4c8b45083 100644 --- a/test/Twig/Tests/Fixtures/functions/include/template_instance.test +++ b/test/Twig/Tests/Fixtures/functions/include/template_instance.test @@ -5,6 +5,6 @@ --TEMPLATE(foo.twig)-- BAR --DATA-- -return ['foo' => $twig->loadTemplate('foo.twig')] +return ['foo' => $twig->load('foo.twig')] --EXPECT-- BAR FOO diff --git a/test/Twig/Tests/Fixtures/tags/include/template_instance.test b/test/Twig/Tests/Fixtures/tags/include/template_instance.test index 4b2285e50..4fb862a17 100644 --- a/test/Twig/Tests/Fixtures/tags/include/template_instance.test +++ b/test/Twig/Tests/Fixtures/tags/include/template_instance.test @@ -1,10 +1,10 @@ --TEST-- -"include" tag accepts Twig_Template instance +"include" tag accepts \Twig\TemplateWrapper instance --TEMPLATE-- {% include foo %} FOO --TEMPLATE(foo.twig)-- BAR --DATA-- -return ['foo' => $twig->loadTemplate('foo.twig')] +return ['foo' => $twig->load('foo.twig')] --EXPECT-- BAR FOO diff --git a/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test b/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test index 9d62cdba7..a5a223886 100644 --- a/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test +++ b/test/Twig/Tests/Fixtures/tags/inheritance/template_instance.test @@ -9,6 +9,6 @@ --TEMPLATE(foo.twig)-- {% block content %}BAR{% endblock %} --DATA-- -return ['foo' => $twig->loadTemplate('foo.twig')] +return ['foo' => $twig->load('foo.twig')] --EXPECT-- BARFOO diff --git a/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test b/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test index f713cd236..68540de7a 100644 --- a/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test +++ b/test/Twig/Tests/Fixtures/tests/defined_for_blocks_with_template.test @@ -9,7 +9,7 @@ --DATA-- return [ 'included_loaded' => $twig->load('included.twig'), - 'included_loaded_internal' => $twig->loadTemplate('included.twig'), + 'included_loaded_internal' => $twig->load('included.twig'), ] --EXPECT-- ok diff --git a/test/Twig/Tests/Loader/FilesystemTest.php b/test/Twig/Tests/Loader/FilesystemTest.php index f03fead99..86e58e6da 100644 --- a/test/Twig/Tests/Loader/FilesystemTest.php +++ b/test/Twig/Tests/Loader/FilesystemTest.php @@ -182,10 +182,10 @@ class Twig_Tests_Loader_FilesystemTest extends \PHPUnit\Framework\TestCase $twig = new Environment($loader); - $template = $twig->loadTemplate('blocks.html.twig'); + $template = $twig->load('blocks.html.twig'); $this->assertSame('block from theme 1', $template->renderBlock('b1', [])); - $template = $twig->loadTemplate('blocks.html.twig'); + $template = $twig->load('blocks.html.twig'); $this->assertSame('block from theme 2', $template->renderBlock('b2', [])); } @@ -211,7 +211,7 @@ class Twig_Tests_Loader_FilesystemTest extends \PHPUnit\Framework\TestCase $twig = new Environment($loader); - $template = $twig->loadTemplate($templateName); + $template = $twig->load($templateName); $this->assertSame('VALID Child', $template->renderBlock('body', [])); } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 3d3ffb4b4..1b42b93e1 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -39,7 +39,7 @@ class Twig_Tests_TemplateTest extends \PHPUnit\Framework\TestCase { $templates = ['index' => $template]; $env = new Environment(new ArrayLoader($templates), ['strict_variables' => true]); - $template = $env->loadTemplate('index'); + $template = $env->load('index'); $context = [ 'string' => 'foo', diff --git a/test/Twig/Tests/TemplateWrapperTest.php b/test/Twig/Tests/TemplateWrapperTest.php index 0522895ce..96ca9b01c 100644 --- a/test/Twig/Tests/TemplateWrapperTest.php +++ b/test/Twig/Tests/TemplateWrapperTest.php @@ -25,17 +25,17 @@ class Twig_Tests_TemplateWrapperTest extends \PHPUnit\Framework\TestCase 'extended' => '{% block extended %}{% endblock %}', ])); - $wrapper = new TemplateWrapper($twig, $twig->loadTemplate('index')); + $wrapper = $twig->load('index'); $this->assertTrue($wrapper->hasBlock('foo')); $this->assertFalse($wrapper->hasBlock('bar')); $this->assertEquals(['foo'], $wrapper->getBlockNames()); - $wrapper = new TemplateWrapper($twig, $twig->loadTemplate('index_with_use')); + $wrapper = $twig->load('index_with_use'); $this->assertTrue($wrapper->hasBlock('foo')); $this->assertTrue($wrapper->hasBlock('imported')); $this->assertEquals(['imported', 'foo'], $wrapper->getBlockNames()); - $wrapper = new TemplateWrapper($twig, $twig->loadTemplate('index_with_extends')); + $wrapper = $twig->load('index_with_extends'); $this->assertTrue($wrapper->hasBlock('foo')); $this->assertTrue($wrapper->hasBlock('extended')); $this->assertEquals(['foo', 'extended'], $wrapper->getBlockNames()); @@ -48,7 +48,7 @@ class Twig_Tests_TemplateWrapperTest extends \PHPUnit\Framework\TestCase ])); $twig->addGlobal('bar', 'BAR'); - $wrapper = new TemplateWrapper($twig, $twig->loadTemplate('index')); + $wrapper = $twig->load('index'); $this->assertEquals('FOOBAR', $wrapper->renderBlock('foo', ['foo' => 'FOO'])); } @@ -59,7 +59,7 @@ class Twig_Tests_TemplateWrapperTest extends \PHPUnit\Framework\TestCase ])); $twig->addGlobal('bar', 'BAR'); - $wrapper = new TemplateWrapper($twig, $twig->loadTemplate('index')); + $wrapper = $twig->load('index'); ob_start(); $wrapper->displayBlock('foo', ['foo' => 'FOO']); From 8ab1079842a3924ebc1c744c41f17f7da36731c1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Mar 2019 18:56:53 +0100 Subject: [PATCH 3/3] simplified code --- src/Environment.php | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/Environment.php b/src/Environment.php index 7b8b31a99..4a953fa86 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -584,12 +584,12 @@ class Environment /** * Tries to load a template consecutively from an array. * - * Similar to loadTemplate() but it also accepts instances of \Twig\Template and + * Similar to load() but it also accepts instances of \Twig\Template and * \Twig\TemplateWrapper, and an array of templates where each is tried to be loaded. * * @param string|Template|\Twig\TemplateWrapper|array $names A template or an array of templates to try consecutively * - * @return Template|Twig_TemplateWrapper + * @return TemplateWrapper * * @throws LoaderError When none of the templates can be found * @throws SyntaxError When an error occurred during compilation @@ -597,24 +597,13 @@ class Environment public function resolveTemplate($names) { if (!\is_array($names)) { - $names = [$names]; + return $this->load($names); } foreach ($names as $name) { - if ($name instanceof Template) { - return $name; - } - - if ($name instanceof TemplateWrapper) { - return $name; - } - try { - return $this->loadTemplate($name); + return $this->load($name); } catch (LoaderError $e) { - if (1 === \count($names)) { - throw $e; - } } }