diff --git a/CHANGELOG b/CHANGELOG index eb75f3a26..a45196470 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -163,6 +163,9 @@ * 1.38.0 (2019-XX-XX) + * fixed batch filter clobbers array keys when fill parameter is used + * added preserveKeys support for the batch filter + * fixed "embed" support when used from "template_from_string" * added the possibility to pass a TemplateWrapper to Twig\Environment::load() * improved the performance of the sandbox * added a spaceless filter diff --git a/src/Environment.php b/src/Environment.php index 82afadc4a..b577c737b 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -299,7 +299,7 @@ class Environment { $key = $this->getLoader()->getCacheKey($name).$this->optionsHash; - return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '_'.$index); + return $this->templateClassPrefix.hash('sha256', $key).(null === $index ? '' : '___'.$index); } /** @@ -379,9 +379,17 @@ class Environment */ public function loadTemplate($name, $index = null) { - $cls = $mainCls = $this->getTemplateClass($name); + return $this->loadClass($this->getTemplateClass($name), $name, $index); + } + + /** + * @internal + */ + public function loadClass($cls, $name, $index = null) + { + $mainCls = $cls; if (null !== $index) { - $cls .= '_'.$index; + $cls .= '___'.$index; } if (isset($this->loadedTemplates[$cls])) { @@ -411,7 +419,7 @@ class Environment } if (!class_exists($cls, false)) { - throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source); + throw new RuntimeError(sprintf('Failed to load Twig template "%s", index "%s": cache might be corrupted.', $name, $index), -1, $source); } } } diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 5d5ad3e4b..04d367dc7 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1469,23 +1469,22 @@ function twig_constant_is_defined($constant, $object = null) * * @return array */ -function twig_array_batch($items, $size, $fill = null) +function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) { if ($items instanceof \Traversable) { - $items = iterator_to_array($items, false); + $items = iterator_to_array($items, $preserveKeys); } $size = ceil($size); - $result = array_chunk($items, $size, true); + $result = array_chunk($items, $size, $preserveKeys); - if (null !== $fill && !empty($result)) { + if (null !== $fill && $result) { $last = \count($result) - 1; if ($fillCount = $size - \count($result[$last])) { - $result[$last] = array_merge( - $result[$last], - array_fill(0, $fillCount, $fill) - ); + for ($i = 0; $i < $fillCount; $i++) { + $result[$last][] = $fill; + } } } diff --git a/src/NodeTraverser.php b/src/NodeTraverser.php index fc483a07f..e4146fc0b 100644 --- a/src/NodeTraverser.php +++ b/src/NodeTraverser.php @@ -39,10 +39,6 @@ final class NodeTraverser public function addVisitor(NodeVisitorInterface $visitor) { - if (!isset($this->visitors[$visitor->getPriority()])) { - $this->visitors[$visitor->getPriority()] = []; - } - $this->visitors[$visitor->getPriority()][] = $visitor; } diff --git a/src/Template.php b/src/Template.php index 5919b74e5..c28f2b5a0 100644 --- a/src/Template.php +++ b/src/Template.php @@ -309,6 +309,15 @@ abstract class Template return $template; } + if ($template === $this->getTemplateName()) { + $class = get_class($this); + if (false !== $pos = strrpos($class, '___', -1)) { + $class = substr($class, 0, $pos); + } + + return $this->env->loadClass($class, $template, $index); + } + return $this->env->loadTemplate($template, $index); } catch (Error $e) { if (!$e->getSourceContext()) { diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index b76e8ecf8..ed36473a2 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -364,7 +364,7 @@ class Twig_Tests_EnvironmentTest extends \PHPUnit\Framework\TestCase /** * @expectedException \Twig\Error\RuntimeError - * @expectedExceptionMessage Failed to load Twig template "testFailLoadTemplate.twig", index "abc": cache is corrupted in "testFailLoadTemplate.twig". + * @expectedExceptionMessage Failed to load Twig template "testFailLoadTemplate.twig", index "abc": cache might be corrupted in "testFailLoadTemplate.twig". */ public function testFailLoadTemplate() { diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_keys.test b/test/Twig/Tests/Fixtures/filters/batch_with_keys.test index f3279a636..e56cd79bb 100644 --- a/test/Twig/Tests/Fixtures/filters/batch_with_keys.test +++ b/test/Twig/Tests/Fixtures/filters/batch_with_keys.test @@ -1,8 +1,8 @@ --TEST-- "batch" filter preserves array keys --TEMPLATE-- -{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }} -{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }} +{{ {'foo': 'bar', 'key': 'value'}|batch(4)|first|keys|join(',') }} +{{ {'foo': 'bar', 'key': 'value'}|batch(4, 'fill')|first|keys|join(',') }} --DATA-- return [] --EXPECT-- diff --git a/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test b/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test new file mode 100644 index 000000000..90f4de685 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test @@ -0,0 +1,23 @@ +--TEST-- +"batch" filter +--TEMPLATE-- +{% for row in items|batch(3, 'fill') %} +
+ {% for key, column in row %} +
{{ column }}
+ {% endfor %} +
+{% endfor %} +--DATA-- +return ['items' => ['a' => 'a', 'b' => 'b', 'c' => 'c', 'd' => 'd', '123' => 'e']] +--EXPECT-- +
+
a
+
b
+
c
+
+
+
d
+
e
+
fill
+
diff --git a/test/Twig/Tests/Fixtures/functions/include_template_from_string.test b/test/Twig/Tests/Fixtures/functions/include_template_from_string.test new file mode 100644 index 000000000..8d9ba60ce --- /dev/null +++ b/test/Twig/Tests/Fixtures/functions/include_template_from_string.test @@ -0,0 +1,11 @@ +--TEST-- +"template_from_string" function works in an "include" +--TEMPLATE-- +{% set embed = '{% embed "embed.twig" %}{% endembed %}' %} +{{ include(template_from_string(embed)) }} +--TEMPLATE(embed.twig)-- +Cool +--DATA-- +return [] +--EXPECT-- +Cool