diff --git a/CHANGELOG b/CHANGELOG index 9e79a4d65..b27d29ae2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -160,6 +160,7 @@ * 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 2e0b55ec3..188ef05af 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -304,8 +304,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 * @@ -315,14 +315,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 @@ -330,7 +330,7 @@ class Environment */ public function display($name, array $context = []) { - $this->loadTemplate($name)->display($context); + $this->load($name)->display($context); } /** @@ -480,12 +480,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|TemplateWrapper|array $names A template or an array of templates to try consecutively * - * @return Template|TemplateWrapper + * @return TemplateWrapper * * @throws LoaderError When none of the templates can be found * @throws SyntaxError When an error occurred during compilation @@ -493,24 +493,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; - } } } diff --git a/src/Test/IntegrationTestCase.php b/src/Test/IntegrationTestCase.php index db471176e..1370fe2ac 100644 --- a/src/Test/IntegrationTestCase.php +++ b/src/Test/IntegrationTestCase.php @@ -200,7 +200,7 @@ abstract class IntegrationTestCase extends TestCase return $prevHandler ? $prevHandler($type, $msg, $file, $line, $context) : false; }); - $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 f413122fe..5cae0dcca 100644 --- a/test/Twig/Tests/ErrorTest.php +++ b/test/Twig/Tests/ErrorTest.php @@ -51,7 +51,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([]); @@ -79,7 +79,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()]); @@ -96,7 +96,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([]); @@ -115,7 +115,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()]); @@ -137,7 +137,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 4e624f24e..4992e0634 100644 --- a/test/Twig/Tests/Extension/SandboxTest.php +++ b/test/Twig/Tests/Extension/SandboxTest.php @@ -58,20 +58,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(SecurityNotAllowedMethodError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -84,7 +84,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(SecurityNotAllowedFilterError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFilterError'); @@ -96,7 +96,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(SecurityNotAllowedTagError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedTagError'); @@ -108,7 +108,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(SecurityNotAllowedPropertyError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedPropertyError'); @@ -121,7 +121,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(SecurityNotAllowedMethodError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -134,7 +134,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(SecurityNotAllowedMethodError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedMethodError'); @@ -147,7 +147,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(SecurityNotAllowedFunctionError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFunctionError'); @@ -159,7 +159,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(SecurityNotAllowedFunctionError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedFunctionError'); @@ -171,7 +171,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'); } @@ -179,7 +179,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'); } @@ -187,38 +187,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() @@ -226,10 +226,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)'); } } @@ -241,7 +241,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 }}', @@ -250,7 +250,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(SecurityNotAllowedTagError::class, $e, 'Exception should be an instance of Twig_Sandbox_SecurityNotAllowedTagError'); @@ -269,7 +269,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() @@ -278,7 +278,7 @@ EOF $e = null; try { - $twig->loadTemplate('1_include')->render(self::$params); + $twig->load('1_include')->render(self::$params); } catch (\Throwable $e) { } if (null === $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 be2f669de..92fea82b4 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 94d4b234e..ea714f2c7 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -36,7 +36,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']);