Merge branch '1.x' into 2.x

* 1.x:
  simplified code
  use load() instead of loadTemplate() in tests
  added the possibility to pass a TemplateWrapper to Twig\Environment::load()
This commit is contained in:
Fabien Potencier
2019-03-11 19:58:03 +01:00
13 changed files with 56 additions and 66 deletions
+1
View File
@@ -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
+10 -21
View File
@@ -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;
}
}
}
+1 -1
View File
@@ -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();
+5 -5
View File
@@ -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([]);
+24 -24
View File
@@ -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('<p>username</p>', $twig->loadTemplate('index')->render([]));
$this->assertEquals('<p>username</p>', $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) {
@@ -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
@@ -5,6 +5,6 @@
--TEMPLATE(foo.twig)--
BAR
--DATA--
return ['foo' => $twig->loadTemplate('foo.twig')]
return ['foo' => $twig->load('foo.twig')]
--EXPECT--
BAR FOO
@@ -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
@@ -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
@@ -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
+3 -3
View File
@@ -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', []));
}
+1 -1
View File
@@ -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',
+5 -5
View File
@@ -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']);