From a264fc62e55e371960a738f40c1753974712b27e Mon Sep 17 00:00:00 2001 From: Artem Genvald Date: Fri, 5 May 2017 12:56:30 +0300 Subject: [PATCH 1/4] Fix typo in `advanced.rst` file --- doc/advanced.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/advanced.rst b/doc/advanced.rst index 5e69e5014..88a78fefa 100644 --- a/doc/advanced.rst +++ b/doc/advanced.rst @@ -464,7 +464,7 @@ from the token stream (``$this->parser->getStream()``): type/value a syntax error is thrown. Otherwise, if the type and value are correct, the token is returned and the stream moves to the next token. -* ``look()``: Looks a the next token without consuming it. +* ``look()``: Looks at the next token without consuming it. Parsing expressions is done by calling the ``parseExpression()`` like we did for the ``set`` tag. From d2b7a01a181efa9ff081df1e43226605e33bed8d Mon Sep 17 00:00:00 2001 From: Hidde Boomsma Date: Fri, 31 Mar 2017 12:05:13 +0200 Subject: [PATCH 2/4] Prepare for php 7.2 * allow `|length` of `null` * allow `|length` of objects not implementing `\Countable` * To get no errors when running with PHP 7.2 You need to use phpunit ^6.1, otherwise the `each` function, used by phpunit, triggers errors. You can do so by running phpunit like: `SYMFONY_PHPUNIT_VERSION=6.1 phpnightly vendor/bin/simple-phpunit` * --- lib/Twig/Extension/Core.php | 10 +++++++++- .../Tests/Fixtures/filters/length.legacy.test | 16 ++++++++++++++++ test/Twig/Tests/Fixtures/filters/length.test | 3 --- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/filters/length.legacy.test diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index 58f82c682..62ea64d91 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -1264,6 +1264,10 @@ if (function_exists('mb_get_info')) { */ function twig_length_filter(Twig_Environment $env, $thing) { + if (null === $thing) { + return 0; + } + if (is_scalar($thing)) { return mb_strlen($thing, $env->getCharset()); } @@ -1272,7 +1276,11 @@ if (function_exists('mb_get_info')) { return mb_strlen((string) $thing, $env->getCharset()); } - return count($thing); + if ($thing instanceof \Countable || is_array($thing)) { + return count($thing); + } + + return 1; } /** diff --git a/test/Twig/Tests/Fixtures/filters/length.legacy.test b/test/Twig/Tests/Fixtures/filters/length.legacy.test new file mode 100644 index 000000000..31fc1d915 --- /dev/null +++ b/test/Twig/Tests/Fixtures/filters/length.legacy.test @@ -0,0 +1,16 @@ +--TEST-- +"length" filter +--TEMPLATE-- +{{ null|length }} +{{ magic|length }} +{{ non_countable|length }} +--DATA-- +return array( + 'null' => null, /* triggers deprecation error */ + 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call, also triggers deprecation */ + 'non_countable' => new \StdClass(), /* triggers deprecation error */ +); +--EXPECT-- +0 +1 +1 diff --git a/test/Twig/Tests/Fixtures/filters/length.test b/test/Twig/Tests/Fixtures/filters/length.test index 599c3fd52..5a4c4d5da 100644 --- a/test/Twig/Tests/Fixtures/filters/length.test +++ b/test/Twig/Tests/Fixtures/filters/length.test @@ -6,7 +6,6 @@ {{ number|length }} {{ to_string_able|length }} {{ countable|length }} -{{ magic|length }} --DATA-- return array( 'array' => array(1, 4), @@ -14,7 +13,6 @@ return array( 'number' => 1000, 'to_string_able' => new ToStringStub('foobar'), 'countable' => new CountableStub(42), /* also asserts we do *not* call __toString() */ - 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call */ ); --EXPECT-- 2 @@ -22,4 +20,3 @@ return array( 4 6 42 -1 From a7c8b0827018437de2a93ea9fe8211c080dc1bd1 Mon Sep 17 00:00:00 2001 From: Andrej Hudec Date: Thu, 11 May 2017 12:36:01 +0200 Subject: [PATCH 3/4] Fix generating template name in createTemplate() method Before this change the name for the template was generated randomly based on uniqid(mt_rand()). It generated new file whenever the template was loaded as string using the `template_from_string` even the template was the same. The generated file wasn't used anymore. Now the cache file is generated by the content of template so using the `template_from_string` is faster and doesn't pollute the cache folder with unneeded files. --- lib/Twig/Environment.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index 51ffc53fe..d75a42568 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -489,7 +489,7 @@ class Twig_Environment */ public function createTemplate($template) { - $name = sprintf('__string_template__%s', hash('sha256', uniqid(mt_rand(), true), false)); + $name = sprintf('__string_template__%s', hash('sha256', $template, false)); $loader = new Twig_Loader_Chain(array( new Twig_Loader_Array(array($name => $template)), From f4adaef9f160f541b6f21bb4fe2f0c5af16196ab Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 11 May 2017 13:24:58 -0700 Subject: [PATCH 4/4] fixed tests --- .../Tests/Fixtures/filters/length.legacy.test | 16 ---------------- test/Twig/Tests/Fixtures/filters/length.test | 9 +++++++++ 2 files changed, 9 insertions(+), 16 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/filters/length.legacy.test diff --git a/test/Twig/Tests/Fixtures/filters/length.legacy.test b/test/Twig/Tests/Fixtures/filters/length.legacy.test deleted file mode 100644 index 31fc1d915..000000000 --- a/test/Twig/Tests/Fixtures/filters/length.legacy.test +++ /dev/null @@ -1,16 +0,0 @@ ---TEST-- -"length" filter ---TEMPLATE-- -{{ null|length }} -{{ magic|length }} -{{ non_countable|length }} ---DATA-- -return array( - 'null' => null, /* triggers deprecation error */ - 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call, also triggers deprecation */ - 'non_countable' => new \StdClass(), /* triggers deprecation error */ -); ---EXPECT-- -0 -1 -1 diff --git a/test/Twig/Tests/Fixtures/filters/length.test b/test/Twig/Tests/Fixtures/filters/length.test index 5a4c4d5da..a7f1e5031 100644 --- a/test/Twig/Tests/Fixtures/filters/length.test +++ b/test/Twig/Tests/Fixtures/filters/length.test @@ -6,6 +6,9 @@ {{ number|length }} {{ to_string_able|length }} {{ countable|length }} +{{ null|length }} +{{ magic|length }} +{{ non_countable|length }} --DATA-- return array( 'array' => array(1, 4), @@ -13,6 +16,9 @@ return array( 'number' => 1000, 'to_string_able' => new ToStringStub('foobar'), 'countable' => new CountableStub(42), /* also asserts we do *not* call __toString() */ + 'null' => null, + 'magic' => new MagicCallStub(), /* used to assert we do *not* call __call */ + 'non_countable' => new \StdClass(), ); --EXPECT-- 2 @@ -20,3 +26,6 @@ return array( 4 6 42 +0 +1 +1