From e7976359a966bd7b82e96a787cd9b40e1c3e90be Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 11 Mar 2019 21:19:43 +0100 Subject: [PATCH 1/5] fixed CS --- test/Twig/Tests/TemplateWrapperTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/test/Twig/Tests/TemplateWrapperTest.php b/test/Twig/Tests/TemplateWrapperTest.php index 96ca9b01c..74618406c 100644 --- a/test/Twig/Tests/TemplateWrapperTest.php +++ b/test/Twig/Tests/TemplateWrapperTest.php @@ -11,7 +11,6 @@ use Twig\Environment; use Twig\Loader\ArrayLoader; -use Twig\TemplateWrapper; class Twig_Tests_TemplateWrapperTest extends \PHPUnit\Framework\TestCase { From 92a63e0c77af85d16a979d0b6a29397c1fffe295 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 12 Mar 2019 09:27:17 +0100 Subject: [PATCH 2/5] fixed "embed" support when used from "template_from_string" --- CHANGELOG | 1 + src/Environment.php | 16 ++++++++++++---- src/Template.php | 9 +++++++++ .../functions/include_template_from_string.test | 11 +++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/functions/include_template_from_string.test diff --git a/CHANGELOG b/CHANGELOG index 510a84bab..7e9ad2614 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.38.0 (2019-XX-XX) + * 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 4a953fa86..968309946 100644 --- a/src/Environment.php +++ b/src/Environment.php @@ -351,7 +351,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); } /** @@ -443,9 +443,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])) { @@ -491,7 +499,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/Template.php b/src/Template.php index 013e92612..8889c024d 100644 --- a/src/Template.php +++ b/src/Template.php @@ -351,6 +351,15 @@ abstract class Template implements \Twig_TemplateInterface 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/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 From 9f23f7c8e217e113725f6e84dd5781897c87e1de Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 12 Mar 2019 10:36:40 +0100 Subject: [PATCH 3/5] removed dead code --- src/NodeTraverser.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/NodeTraverser.php b/src/NodeTraverser.php index db28be1a1..8b0f85c52 100644 --- a/src/NodeTraverser.php +++ b/src/NodeTraverser.php @@ -40,10 +40,6 @@ class NodeTraverser public function addVisitor(NodeVisitorInterface $visitor) { - if (!isset($this->visitors[$visitor->getPriority()])) { - $this->visitors[$visitor->getPriority()] = []; - } - $this->visitors[$visitor->getPriority()][] = $visitor; } From ede9a604eb43533b322c932d08f986999300b3f0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 12 Mar 2019 10:19:08 +0100 Subject: [PATCH 4/5] added preserveKeys support for the batch filter --- CHANGELOG | 1 + src/Extension/CoreExtension.php | 13 +++++-------- .../Tests/Fixtures/filters/batch_with_keys.test | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7e9ad2614..205c88e53 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.38.0 (2019-XX-XX) + * 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 diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 21178b6ef..7918e1f8e 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1638,23 +1638,20 @@ 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) - ); + $result[$last] = array_merge($result[$last], array_fill(0, $fillCount, $fill)); } } 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-- From 750cb237421a2210b677c8ae1f23096ce407714b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 12 Mar 2019 10:48:58 +0100 Subject: [PATCH 5/5] fixed batch filter clobbers array keys when fill parameter is used --- CHANGELOG | 1 + src/Extension/CoreExtension.php | 4 +++- .../filters/batch_with_more_elements.test | 23 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/Twig/Tests/Fixtures/filters/batch_with_more_elements.test diff --git a/CHANGELOG b/CHANGELOG index 205c88e53..b5e117f4b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 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() diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 7918e1f8e..63265d619 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1651,7 +1651,9 @@ function twig_array_batch($items, $size, $fill = null, $preserveKeys = true) 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/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
+