From f5cf424d0eeb2406b962931f0f3645d7fcec6509 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 26 Oct 2015 17:55:08 +0100 Subject: [PATCH] added the Symfony mbstring polyfill --- composer.json | 3 ++- lib/Twig/Extension/Core.php | 19 ++++++------------- lib/Twig/Lexer.php | 2 +- lib/Twig/Markup.php | 2 +- .../Tests/Fixtures/filters/length_utf8.test | 2 -- .../Tests/Fixtures/filters/split_utf8.test | 2 -- .../Twig/Tests/Node/Expression/FilterTest.php | 6 +----- 7 files changed, 11 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 80f4240a3..ddf723d76 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "forum": "https://groups.google.com/forum/#!forum/twig-users" }, "require": { - "php": ">=5.5" + "php": ">=5.5", + "symfony/polyfill-mbstring": "~1.0" }, "require-dev": { "symfony/phpunit-bridge": "~2.7", diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index c99f9ce2e..432f68a2e 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -147,7 +147,7 @@ class Twig_Extension_Core extends Twig_Extension */ public function getFilters() { - $filters = array( + return array( // formatting filters new Twig_Filter('date', 'twig_date_format_filter', array('needs_environment' => true)), new Twig_Filter('date_modify', 'twig_date_modify_filter', array('needs_environment' => true)), @@ -165,8 +165,8 @@ class Twig_Extension_Core extends Twig_Extension // string filters new Twig_Filter('title', 'twig_title_string_filter', array('needs_environment' => true)), new Twig_Filter('capitalize', 'twig_capitalize_string_filter', array('needs_environment' => true)), - new Twig_Filter('upper', 'strtoupper'), - new Twig_Filter('lower', 'strtolower'), + new Twig_Filter('upper', 'twig_upper_filter', array('needs_environment' => true)), + new Twig_Filter('lower', 'twig_lower_filter', array('needs_environment' => true)), new Twig_Filter('striptags', 'strip_tags'), new Twig_Filter('trim', 'trim'), new Twig_Filter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))), @@ -193,13 +193,6 @@ class Twig_Extension_Core extends Twig_Extension new Twig_Filter('escape', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), new Twig_Filter('e', 'twig_escape_filter', array('needs_environment' => true, 'is_safe_callback' => 'twig_escape_filter_is_safe')), ); - - if (function_exists('mb_get_info')) { - $filters[] = new Twig_Filter('upper', 'twig_upper_filter', array('needs_environment' => true)); - $filters[] = new Twig_Filter('lower', 'twig_lower_filter', array('needs_environment' => true)); - } - - return $filters; } /** @@ -711,7 +704,7 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese $item = (string) $item; - if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) { + if (null !== $charset = $env->getCharset()) { return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset); } @@ -804,7 +797,7 @@ function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = n return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit); } - if (!function_exists('mb_get_info') || null === $charset = $env->getCharset()) { + if (null === $charset = $env->getCharset()) { return str_split($value, null === $limit ? 1 : $limit); } @@ -1253,7 +1246,7 @@ function twig_lower_filter(Twig_Environment $env, $string) */ function twig_title_string_filter(Twig_Environment $env, $string) { - if (null !== ($charset = $env->getCharset())) { + if (null !== $charset = $env->getCharset()) { return mb_convert_case($string, MB_CASE_TITLE, $charset); } diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index e2884214e..94f6a3718 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -77,7 +77,7 @@ class Twig_Lexer */ public function tokenize($code, $filename = null) { - if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2) { + if (((int) ini_get('mbstring.func_overload')) & 2) { $mbEncoding = mb_internal_encoding(); mb_internal_encoding('ASCII'); } else { diff --git a/lib/Twig/Markup.php b/lib/Twig/Markup.php index e8e796f70..1620a41b4 100644 --- a/lib/Twig/Markup.php +++ b/lib/Twig/Markup.php @@ -32,6 +32,6 @@ class Twig_Markup implements Countable public function count() { - return function_exists('mb_get_info') ? mb_strlen($this->content, $this->charset) : strlen($this->content); + return mb_strlen($this->content, $this->charset); } } diff --git a/test/Twig/Tests/Fixtures/filters/length_utf8.test b/test/Twig/Tests/Fixtures/filters/length_utf8.test index 5d5e24361..e5b1b101d 100644 --- a/test/Twig/Tests/Fixtures/filters/length_utf8.test +++ b/test/Twig/Tests/Fixtures/filters/length_utf8.test @@ -1,7 +1,5 @@ --TEST-- "length" filter ---CONDITION-- -function_exists('mb_get_info') --TEMPLATE-- {{ string|length }} {{ markup|length }} diff --git a/test/Twig/Tests/Fixtures/filters/split_utf8.test b/test/Twig/Tests/Fixtures/filters/split_utf8.test index 305e162fe..5757abaf0 100644 --- a/test/Twig/Tests/Fixtures/filters/split_utf8.test +++ b/test/Twig/Tests/Fixtures/filters/split_utf8.test @@ -1,7 +1,5 @@ --TEST-- "split" filter ---CONDITION-- -function_exists('mb_get_info') --TEMPLATE-- {{ "é"|split('', 10)|join('-') }} {{ foo|split(',')|join('-') }} diff --git a/test/Twig/Tests/Node/Expression/FilterTest.php b/test/Twig/Tests/Node/Expression/FilterTest.php index 0482daca3..0f4ff9c21 100644 --- a/test/Twig/Tests/Node/Expression/FilterTest.php +++ b/test/Twig/Tests/Node/Expression/FilterTest.php @@ -35,11 +35,7 @@ class Twig_Tests_Node_Expression_FilterTest extends Twig_Test_NodeTestCase $node = $this->createFilter($expr, 'upper'); $node = $this->createFilter($node, 'number_format', array(new Twig_Node_Expression_Constant(2, 1), new Twig_Node_Expression_Constant('.', 1), new Twig_Node_Expression_Constant(',', 1))); - if (function_exists('mb_get_info')) { - $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")'); - } else { - $tests[] = array($node, 'twig_number_format_filter($this->env, strtoupper("foo"), 2, ".", ",")'); - } + $tests[] = array($node, 'twig_number_format_filter($this->env, twig_upper_filter($this->env, "foo"), 2, ".", ",")'); // named arguments $date = new Twig_Node_Expression_Constant(0, 1);