summaryrefslogtreecommitdiff
path: root/inc/lib/Twig/Extension/Core.php
diff options
context:
space:
mode:
authorBenjamin Southall <applemansigloo@gmail.com>2019-02-26 10:11:12 +1000
committerBenjamin Southall <applemansigloo@gmail.com>2019-02-26 10:11:12 +1000
commitf79395851b044689567e0e6c9504acbcaa742782 (patch)
treeee539aee9ffa90e8accd6f130b30a8ebed7a232e /inc/lib/Twig/Extension/Core.php
parent28f32baf0125ca5085266e73dc5071e5814d8e3b (diff)
Twig update to latest Twig 1.x legacy as per vichan
Diffstat (limited to 'inc/lib/Twig/Extension/Core.php')
-rw-r--r--inc/lib/Twig/Extension/Core.php809
1 files changed, 547 insertions, 262 deletions
diff --git a/inc/lib/Twig/Extension/Core.php b/inc/lib/Twig/Extension/Core.php
index 60fe1936..6fa78e13 100644
--- a/inc/lib/Twig/Extension/Core.php
+++ b/inc/lib/Twig/Extension/Core.php
@@ -7,16 +7,42 @@ if (!defined('ENT_SUBSTITUTE')) {
/*
* This file is part of Twig.
*
- * (c) 2009 Fabien Potencier
+ * (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
+
+/**
+ * @final
+ */
class Twig_Extension_Core extends Twig_Extension
{
protected $dateFormats = array('F j, Y H:i', '%d days');
protected $numberFormat = array(0, '.', ',');
protected $timezone = null;
+ protected $escapers = array();
+
+ /**
+ * Defines a new escaper to be used via the escape filter.
+ *
+ * @param string $strategy The strategy name that should be used as a strategy in the escape call
+ * @param callable $callable A valid PHP callable
+ */
+ public function setEscaper($strategy, $callable)
+ {
+ $this->escapers[$strategy] = $callable;
+ }
+
+ /**
+ * Gets all defined escapers.
+ *
+ * @return array An array of escapers
+ */
+ public function getEscapers()
+ {
+ return $this->escapers;
+ }
/**
* Sets the default format to be used by the date filter.
@@ -72,9 +98,9 @@ class Twig_Extension_Core extends Twig_Extension
/**
* Sets the default format to be used by the number_format filter.
*
- * @param integer $decimal The number of decimal places to use.
- * @param string $decimalPoint The character(s) to use for the decimal point.
- * @param string $thousandSep The character(s) to use for the thousands separator.
+ * @param int $decimal the number of decimal places to use
+ * @param string $decimalPoint the character(s) to use for the decimal point
+ * @param string $thousandSep the character(s) to use for the thousands separator
*/
public function setNumberFormat($decimal, $decimalPoint, $thousandSep)
{
@@ -91,11 +117,6 @@ class Twig_Extension_Core extends Twig_Extension
return $this->numberFormat;
}
- /**
- * Returns the token parser instance to add to the existing list.
- *
- * @return array An array of Twig_TokenParser instances
- */
public function getTokenParsers()
{
return array(
@@ -114,14 +135,10 @@ class Twig_Extension_Core extends Twig_Extension
new Twig_TokenParser_Flush(),
new Twig_TokenParser_Do(),
new Twig_TokenParser_Embed(),
+ new Twig_TokenParser_With(),
);
}
- /**
- * Returns a list of filters to add to the existing list.
- *
- * @return array An array of filters
- */
public function getFilters()
{
$filters = array(
@@ -129,9 +146,10 @@ class Twig_Extension_Core extends Twig_Extension
new Twig_SimpleFilter('date', 'twig_date_format_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('date_modify', 'twig_date_modify_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('format', 'sprintf'),
- new Twig_SimpleFilter('replace', 'strtr'),
+ new Twig_SimpleFilter('replace', 'twig_replace_filter'),
new Twig_SimpleFilter('number_format', 'twig_number_format_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('abs', 'abs'),
+ new Twig_SimpleFilter('round', 'twig_round'),
// encoding
new Twig_SimpleFilter('url_encode', 'twig_urlencode_filter'),
@@ -144,12 +162,12 @@ class Twig_Extension_Core extends Twig_Extension
new Twig_SimpleFilter('upper', 'strtoupper'),
new Twig_SimpleFilter('lower', 'strtolower'),
new Twig_SimpleFilter('striptags', 'strip_tags'),
- new Twig_SimpleFilter('trim', 'trim'),
+ new Twig_SimpleFilter('trim', 'twig_trim_filter'),
new Twig_SimpleFilter('nl2br', 'nl2br', array('pre_escape' => 'html', 'is_safe' => array('html'))),
// array helpers
new Twig_SimpleFilter('join', 'twig_join_filter'),
- new Twig_SimpleFilter('split', 'twig_split_filter'),
+ new Twig_SimpleFilter('split', 'twig_split_filter', array('needs_environment' => true)),
new Twig_SimpleFilter('sort', 'twig_sort_filter'),
new Twig_SimpleFilter('merge', 'twig_array_merge'),
new Twig_SimpleFilter('batch', 'twig_array_batch'),
@@ -178,130 +196,80 @@ class Twig_Extension_Core extends Twig_Extension
return $filters;
}
- /**
- * Returns a list of global functions to add to the existing list.
- *
- * @return array An array of global functions
- */
public function getFunctions()
{
return array(
+ new Twig_SimpleFunction('max', 'max'),
+ new Twig_SimpleFunction('min', 'min'),
new Twig_SimpleFunction('range', 'range'),
new Twig_SimpleFunction('constant', 'twig_constant'),
new Twig_SimpleFunction('cycle', 'twig_cycle'),
new Twig_SimpleFunction('random', 'twig_random', array('needs_environment' => true)),
new Twig_SimpleFunction('date', 'twig_date_converter', array('needs_environment' => true)),
new Twig_SimpleFunction('include', 'twig_include', array('needs_environment' => true, 'needs_context' => true, 'is_safe' => array('all'))),
+ new Twig_SimpleFunction('source', 'twig_source', array('needs_environment' => true, 'is_safe' => array('all'))),
);
}
- /**
- * Returns a list of tests to add to the existing list.
- *
- * @return array An array of tests
- */
public function getTests()
{
return array(
new Twig_SimpleTest('even', null, array('node_class' => 'Twig_Node_Expression_Test_Even')),
new Twig_SimpleTest('odd', null, array('node_class' => 'Twig_Node_Expression_Test_Odd')),
new Twig_SimpleTest('defined', null, array('node_class' => 'Twig_Node_Expression_Test_Defined')),
- new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
+ new Twig_SimpleTest('sameas', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas', 'deprecated' => '1.21', 'alternative' => 'same as')),
+ new Twig_SimpleTest('same as', null, array('node_class' => 'Twig_Node_Expression_Test_Sameas')),
new Twig_SimpleTest('none', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
new Twig_SimpleTest('null', null, array('node_class' => 'Twig_Node_Expression_Test_Null')),
- new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
+ new Twig_SimpleTest('divisibleby', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby', 'deprecated' => '1.21', 'alternative' => 'divisible by')),
+ new Twig_SimpleTest('divisible by', null, array('node_class' => 'Twig_Node_Expression_Test_Divisibleby')),
new Twig_SimpleTest('constant', null, array('node_class' => 'Twig_Node_Expression_Test_Constant')),
new Twig_SimpleTest('empty', 'twig_test_empty'),
new Twig_SimpleTest('iterable', 'twig_test_iterable'),
);
}
- /**
- * Returns a list of operators to add to the existing list.
- *
- * @return array An array of operators
- */
public function getOperators()
{
return array(
array(
'not' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
- '-' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Neg'),
- '+' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Pos'),
+ '-' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Neg'),
+ '+' => array('precedence' => 500, 'class' => 'Twig_Node_Expression_Unary_Pos'),
),
array(
- 'or' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'and' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'b-or' => array('precedence' => 16, 'class' => 'Twig_Node_Expression_Binary_BitwiseOr', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'b-xor' => array('precedence' => 17, 'class' => 'Twig_Node_Expression_Binary_BitwiseXor', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'b-and' => array('precedence' => 18, 'class' => 'Twig_Node_Expression_Binary_BitwiseAnd', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '==' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '!=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '<' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '>=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '<=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'or' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'and' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'b-or' => array('precedence' => 16, 'class' => 'Twig_Node_Expression_Binary_BitwiseOr', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'b-xor' => array('precedence' => 17, 'class' => 'Twig_Node_Expression_Binary_BitwiseXor', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'b-and' => array('precedence' => 18, 'class' => 'Twig_Node_Expression_Binary_BitwiseAnd', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '==' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '!=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '<' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '>=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '<=' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
'not in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotIn', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_In', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '..' => array('precedence' => 25, 'class' => 'Twig_Node_Expression_Binary_Range', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '+' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Add', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '-' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Sub', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '*' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mul', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '/' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Div', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '//' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_FloorDiv', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '%' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'is' => array('precedence' => 100, 'callable' => array($this, 'parseTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- 'is not' => array('precedence' => 100, 'callable' => array($this, 'parseNotTestExpression'), 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
- '**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
+ 'in' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_In', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'matches' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Matches', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'starts with' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_StartsWith', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'ends with' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_EndsWith', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '..' => array('precedence' => 25, 'class' => 'Twig_Node_Expression_Binary_Range', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '+' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Add', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '-' => array('precedence' => 30, 'class' => 'Twig_Node_Expression_Binary_Sub', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '~' => array('precedence' => 40, 'class' => 'Twig_Node_Expression_Binary_Concat', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '*' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mul', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '/' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Div', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '//' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_FloorDiv', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '%' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'is' => array('precedence' => 100, 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ 'is not' => array('precedence' => 100, 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+ '**' => array('precedence' => 200, 'class' => 'Twig_Node_Expression_Binary_Power', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
+ '??' => array('precedence' => 300, 'class' => 'Twig_Node_Expression_NullCoalesce', 'associativity' => Twig_ExpressionParser::OPERATOR_RIGHT),
),
);
}
- public function parseNotTestExpression(Twig_Parser $parser, $node)
- {
- return new Twig_Node_Expression_Unary_Not($this->parseTestExpression($parser, $node), $parser->getCurrentToken()->getLine());
- }
-
- public function parseTestExpression(Twig_Parser $parser, $node)
- {
- $stream = $parser->getStream();
- $name = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
- $arguments = null;
- if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
- $arguments = $parser->getExpressionParser()->parseArguments(true);
- }
-
- $class = $this->getTestNodeClass($parser, $name, $node->getLine());
-
- return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
- }
-
- protected function getTestNodeClass(Twig_Parser $parser, $name, $line)
- {
- $env = $parser->getEnvironment();
- $testMap = $env->getTests();
- if (!isset($testMap[$name])) {
- $message = sprintf('The test "%s" does not exist', $name);
- if ($alternatives = $env->computeAlternatives($name, array_keys($env->getTests()))) {
- $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
- }
-
- throw new Twig_Error_Syntax($message, $line, $parser->getFilename());
- }
-
- if ($testMap[$name] instanceof Twig_SimpleTest) {
- return $testMap[$name]->getNodeClass();
- }
-
- return $testMap[$name] instanceof Twig_Test_Node ? $testMap[$name]->getClass() : 'Twig_Node_Expression_Test';
- }
-
- /**
- * Returns the name of the extension.
- *
- * @return string The extension name
- */
public function getName()
{
return 'core';
@@ -311,8 +279,8 @@ class Twig_Extension_Core extends Twig_Extension
/**
* Cycles over a value.
*
- * @param ArrayAccess|array $values An array or an ArrayAccess instance
- * @param integer $position The cycle position
+ * @param ArrayAccess|array $values
+ * @param int $position The cycle position
*
* @return string The next value in the cycle
*/
@@ -329,12 +297,12 @@ function twig_cycle($values, $position)
* Returns a random value depending on the supplied parameter type:
* - a random item from a Traversable or array
* - a random character from a string
- * - a random integer between 0 and the integer parameter
+ * - a random integer between 0 and the integer parameter.
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param Traversable|array|integer|string $values The values to pick a random item from
+ * @param Twig_Environment $env
+ * @param Traversable|array|int|float|string $values The values to pick a random item from
*
- * @throws Twig_Error_Runtime When $values is an empty array (does not apply to an empty string which is returned as is).
+ * @throws Twig_Error_Runtime when $values is an empty array (does not apply to an empty string which is returned as is)
*
* @return mixed A random value from the given sequence
*/
@@ -348,14 +316,14 @@ function twig_random(Twig_Environment $env, $values = null)
return $values < 0 ? mt_rand($values, 0) : mt_rand(0, $values);
}
- if (is_object($values) && $values instanceof Traversable) {
+ if ($values instanceof Traversable) {
$values = iterator_to_array($values);
} elseif (is_string($values)) {
if ('' === $values) {
return '';
}
if (null !== $charset = $env->getCharset()) {
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$values = twig_convert_encoding($values, 'UTF-8', $charset);
}
@@ -363,7 +331,7 @@ function twig_random(Twig_Environment $env, $values = null)
// split at all positions, but not after the start and not before the end
$values = preg_split('/(?<!^)(?!$)/u', $values);
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
foreach ($values as $i => $value) {
$values[$i] = twig_convert_encoding($value, $charset, 'UTF-8');
}
@@ -391,17 +359,17 @@ function twig_random(Twig_Environment $env, $values = null)
* {{ post.published_at|date("m/d/Y") }}
* </pre>
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param DateTime|DateInterval|string $date A date
- * @param string $format A format
- * @param DateTimeZone|string $timezone A timezone
+ * @param Twig_Environment $env
+ * @param DateTime|DateTimeInterface|DateInterval|string $date A date
+ * @param string|null $format The target format, null to use the default
+ * @param DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged
*
* @return string The formatted date
*/
function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $timezone = null)
{
if (null === $format) {
- $formats = $env->getExtension('core')->getDateFormat();
+ $formats = $env->getExtension('Twig_Extension_Core')->getDateFormat();
$format = $date instanceof DateInterval ? $formats[1] : $formats[0];
}
@@ -413,24 +381,27 @@ function twig_date_format_filter(Twig_Environment $env, $date, $format = null, $
}
/**
- * Returns a new date object modified
+ * Returns a new date object modified.
*
* <pre>
* {{ post.published_at|date_modify("-1day")|date("m/d/Y") }}
* </pre>
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param DateTime|string $date A date
- * @param string $modifier A modifier string
+ * @param Twig_Environment $env
+ * @param DateTime|string $date A date
+ * @param string $modifier A modifier string
*
* @return DateTime A new date object
*/
function twig_date_modify_filter(Twig_Environment $env, $date, $modifier)
{
$date = twig_date_converter($env, $date, false);
- $date->modify($modifier);
+ $resultDate = $date->modify($modifier);
- return $date;
+ // This is a hack to ensure PHP 5.2 support and support for DateTimeImmutable
+ // DateTime::modify does not return the modified DateTime object < 5.3.0
+ // and DateTimeImmutable does not modify $date.
+ return null === $resultDate ? $date : $resultDate;
}
/**
@@ -442,63 +413,119 @@ function twig_date_modify_filter(Twig_Environment $env, $date, $modifier)
* {% endif %}
* </pre>
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param DateTime|string $date A date
- * @param DateTimeZone|string $timezone A timezone
+ * @param Twig_Environment $env
+ * @param DateTime|DateTimeInterface|string|null $date A date
+ * @param DateTimeZone|string|null|false $timezone The target timezone, null to use the default, false to leave unchanged
*
* @return DateTime A DateTime instance
*/
function twig_date_converter(Twig_Environment $env, $date = null, $timezone = null)
{
// determine the timezone
- if (!$timezone) {
- $defaultTimezone = $env->getExtension('core')->getTimezone();
- } elseif (!$timezone instanceof DateTimeZone) {
- $defaultTimezone = new DateTimeZone($timezone);
- } else {
- $defaultTimezone = $timezone;
+ if (false !== $timezone) {
+ if (null === $timezone) {
+ $timezone = $env->getExtension('Twig_Extension_Core')->getTimezone();
+ } elseif (!$timezone instanceof DateTimeZone) {
+ $timezone = new DateTimeZone($timezone);
+ }
}
- if ($date instanceof DateTime) {
+ // immutable dates
+ if ($date instanceof DateTimeImmutable) {
+ return false !== $timezone ? $date->setTimezone($timezone) : $date;
+ }
+
+ if ($date instanceof DateTime || $date instanceof DateTimeInterface) {
$date = clone $date;
if (false !== $timezone) {
- $date->setTimezone($defaultTimezone);
+ $date->setTimezone($timezone);
}
return $date;
}
+ if (null === $date || 'now' === $date) {
+ return new DateTime($date, false !== $timezone ? $timezone : $env->getExtension('Twig_Extension_Core')->getTimezone());
+ }
+
$asString = (string) $date;
if (ctype_digit($asString) || (!empty($asString) && '-' === $asString[0] && ctype_digit(substr($asString, 1)))) {
- $date = '@'.$date;
+ $date = new DateTime('@'.$date);
+ } else {
+ $date = new DateTime($date, $env->getExtension('Twig_Extension_Core')->getTimezone());
}
- $date = new DateTime($date, $defaultTimezone);
if (false !== $timezone) {
- $date->setTimezone($defaultTimezone);
+ $date->setTimezone($timezone);
}
return $date;
}
/**
+ * Replaces strings within a string.
+ *
+ * @param string $str String to replace in
+ * @param array|Traversable $from Replace values
+ * @param string|null $to Replace to, deprecated (@see https://secure.php.net/manual/en/function.strtr.php)
+ *
+ * @return string
+ */
+function twig_replace_filter($str, $from, $to = null)
+{
+ if ($from instanceof Traversable) {
+ $from = iterator_to_array($from);
+ } elseif (is_string($from) && is_string($to)) {
+ @trigger_error('Using "replace" with character by character replacement is deprecated since version 1.22 and will be removed in Twig 2.0', E_USER_DEPRECATED);
+
+ return strtr($str, $from, $to);
+ } elseif (!is_array($from)) {
+ throw new Twig_Error_Runtime(sprintf('The "replace" filter expects an array or "Traversable" as replace values, got "%s".', is_object($from) ? get_class($from) : gettype($from)));
+ }
+
+ return strtr($str, $from);
+}
+
+/**
+ * Rounds a number.
+ *
+ * @param int|float $value The value to round
+ * @param int|float $precision The rounding precision
+ * @param string $method The method to use for rounding
+ *
+ * @return int|float The rounded number
+ */
+function twig_round($value, $precision = 0, $method = 'common')
+{
+ if ('common' == $method) {
+ return round($value, $precision);
+ }
+
+ if ('ceil' != $method && 'floor' != $method) {
+ throw new Twig_Error_Runtime('The round filter only supports the "common", "ceil", and "floor" methods.');
+ }
+
+ return $method($value * pow(10, $precision)) / pow(10, $precision);
+}
+
+/**
* Number format filter.
*
* All of the formatting options can be left null, in that case the defaults will
* be used. Supplying any of the parameters will override the defaults set in the
* environment object.
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param mixed $number A float/int/string of the number to format
- * @param integer $decimal The number of decimal points to display.
- * @param string $decimalPoint The character(s) to use for the decimal point.
- * @param string $thousandSep The character(s) to use for the thousands separator.
+ * @param Twig_Environment $env
+ * @param mixed $number A float/int/string of the number to format
+ * @param int $decimal the number of decimal points to display
+ * @param string $decimalPoint the character(s) to use for the decimal point
+ * @param string $thousandSep the character(s) to use for the thousands separator
*
* @return string The formatted number
*/
function twig_number_format_filter(Twig_Environment $env, $number, $decimal = null, $decimalPoint = null, $thousandSep = null)
{
- $defaults = $env->getExtension('core')->getNumberFormat();
+ $defaults = $env->getExtension('Twig_Extension_Core')->getNumberFormat();
if (null === $decimal) {
$decimal = $defaults[0];
}
@@ -515,32 +542,31 @@ function twig_number_format_filter(Twig_Environment $env, $number, $decimal = nu
}
/**
- * URL encodes a string as a path segment or an array as a query string.
+ * URL encodes (RFC 3986) a string as a path segment or an array as a query string.
*
* @param string|array $url A URL or an array of query parameters
- * @param bool $raw true to use rawurlencode() instead of urlencode
*
* @return string The URL encoded value
*/
-function twig_urlencode_filter($url, $raw = false)
+function twig_urlencode_filter($url)
{
if (is_array($url)) {
- return http_build_query($url, '', '&');
- }
+ if (defined('PHP_QUERY_RFC3986')) {
+ return http_build_query($url, '', '&', PHP_QUERY_RFC3986);
+ }
- if ($raw) {
- return rawurlencode($url);
+ return http_build_query($url, '', '&');
}
- return urlencode($url);
+ return rawurlencode($url);
}
-if (version_compare(PHP_VERSION, '5.3.0', '<')) {
+if (PHP_VERSION_ID < 50300) {
/**
* JSON encodes a variable.
*
- * @param mixed $value The value to encode.
- * @param integer $options Not used on PHP 5.2.x
+ * @param mixed $value the value to encode
+ * @param int $options Not used on PHP 5.2.x
*
* @return mixed The JSON encoded value
*/
@@ -558,8 +584,8 @@ if (version_compare(PHP_VERSION, '5.3.0', '<')) {
/**
* JSON encodes a variable.
*
- * @param mixed $value The value to encode.
- * @param integer $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
+ * @param mixed $value the value to encode
+ * @param int $options Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
*
* @return mixed The JSON encoded value
*/
@@ -593,15 +619,23 @@ function _twig_markup2string(&$value)
* {# items now contains { 'apple': 'fruit', 'orange': 'fruit', 'peugeot': 'car' } #}
* </pre>
*
- * @param array $arr1 An array
- * @param array $arr2 An array
+ * @param array|Traversable $arr1 An array
+ * @param array|Traversable $arr2 An array
*
* @return array The merged array
*/
function twig_array_merge($arr1, $arr2)
{
- if (!is_array($arr1) || !is_array($arr2)) {
- throw new Twig_Error_Runtime('The merge filter only works with arrays or hashes.');
+ if ($arr1 instanceof Traversable) {
+ $arr1 = iterator_to_array($arr1);
+ } elseif (!is_array($arr1)) {
+ throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as first argument.', gettype($arr1)));
+ }
+
+ if ($arr2 instanceof Traversable) {
+ $arr2 = iterator_to_array($arr2);
+ } elseif (!is_array($arr2)) {
+ throw new Twig_Error_Runtime(sprintf('The merge filter only works with arrays or "Traversable", got "%s" as second argument.', gettype($arr2)));
}
return array_merge($arr1, $arr2);
@@ -610,18 +644,30 @@ function twig_array_merge($arr1, $arr2)
/**
* Slices a variable.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param mixed $item A variable
- * @param integer $start Start of the slice
- * @param integer $length Size of the slice
- * @param Boolean $preserveKeys Whether to preserve key or not (when the input is an array)
+ * @param int $start Start of the slice
+ * @param int $length Size of the slice
+ * @param bool $preserveKeys Whether to preserve key or not (when the input is an array)
*
* @return mixed The sliced variable
*/
function twig_slice(Twig_Environment $env, $item, $start, $length = null, $preserveKeys = false)
{
- if (is_object($item) && $item instanceof Traversable) {
- $item = iterator_to_array($item, false);
+ if ($item instanceof Traversable) {
+ while ($item instanceof IteratorAggregate) {
+ $item = $item->getIterator();
+ }
+
+ if ($start >= 0 && $length >= 0 && $item instanceof Iterator) {
+ try {
+ return iterator_to_array(new LimitIterator($item, $start, null === $length ? -1 : $length), $preserveKeys);
+ } catch (OutOfBoundsException $exception) {
+ return array();
+ }
+ }
+
+ $item = iterator_to_array($item, $preserveKeys);
}
if (is_array($item)) {
@@ -631,16 +677,16 @@ function twig_slice(Twig_Environment $env, $item, $start, $length = null, $prese
$item = (string) $item;
if (function_exists('mb_get_info') && null !== $charset = $env->getCharset()) {
- return mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
+ return (string) mb_substr($item, $start, null === $length ? mb_strlen($item, $charset) - $start : $length, $charset);
}
- return null === $length ? substr($item, $start) : substr($item, $start, $length);
+ return (string) (null === $length ? substr($item, $start) : substr($item, $start, $length));
}
/**
* Returns the first element of the item.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param mixed $item A variable
*
* @return mixed The first element of the item
@@ -649,13 +695,13 @@ function twig_first(Twig_Environment $env, $item)
{
$elements = twig_slice($env, $item, 0, 1, false);
- return is_string($elements) ? $elements[0] : current($elements);
+ return is_string($elements) ? $elements : current($elements);
}
/**
* Returns the last element of the item.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param mixed $item A variable
*
* @return mixed The last element of the item
@@ -664,7 +710,7 @@ function twig_last(Twig_Environment $env, $item)
{
$elements = twig_slice($env, $item, -1, 1, false);
- return is_string($elements) ? $elements[0] : current($elements);
+ return is_string($elements) ? $elements : current($elements);
}
/**
@@ -687,7 +733,7 @@ function twig_last(Twig_Environment $env, $item)
*/
function twig_join_filter($value, $glue = '')
{
- if (is_object($value) && $value instanceof Traversable) {
+ if ($value instanceof Traversable) {
$value = iterator_to_array($value, false);
}
@@ -711,24 +757,46 @@ function twig_join_filter($value, $glue = '')
* {# returns [aa, bb, cc] #}
* </pre>
*
- * @param string $value A string
- * @param string $delimiter The delimiter
- * @param integer $limit The limit
+ * @param Twig_Environment $env
+ * @param string $value A string
+ * @param string $delimiter The delimiter
+ * @param int $limit The limit
*
* @return array The split string as an array
*/
-function twig_split_filter($value, $delimiter, $limit = null)
+function twig_split_filter(Twig_Environment $env, $value, $delimiter, $limit = null)
{
- if (empty($delimiter)) {
+ if (!empty($delimiter)) {
+ return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
+ }
+
+ if (!function_exists('mb_get_info') || null === $charset = $env->getCharset()) {
return str_split($value, null === $limit ? 1 : $limit);
}
- return null === $limit ? explode($delimiter, $value) : explode($delimiter, $value, $limit);
+ if ($limit <= 1) {
+ return preg_split('/(?<!^)(?!$)/u', $value);
+ }
+
+ $length = mb_strlen($value, $charset);
+ if ($length < $limit) {
+ return array($value);
+ }
+
+ $r = array();
+ for ($i = 0; $i < $length; $i += $limit) {
+ $r[] = mb_substr($value, $i, $limit, $charset);
+ }
+
+ return $r;
}
// The '_default' filter is used internally to avoid using the ternary operator
// which costs a lot for big contexts (before PHP 5.4). So, on average,
// a function call is cheaper.
+/**
+ * @internal
+ */
function _twig_default_filter($value, $default = '')
{
if (twig_test_empty($value)) {
@@ -755,8 +823,28 @@ function _twig_default_filter($value, $default = '')
*/
function twig_get_array_keys_filter($array)
{
- if (is_object($array) && $array instanceof Traversable) {
- return array_keys(iterator_to_array($array));
+ if ($array instanceof Traversable) {
+ while ($array instanceof IteratorAggregate) {
+ $array = $array->getIterator();
+ }
+
+ if ($array instanceof Iterator) {
+ $keys = array();
+ $array->rewind();
+ while ($array->valid()) {
+ $keys[] = $array->key();
+ $array->next();
+ }
+
+ return $keys;
+ }
+
+ $keys = array();
+ foreach ($array as $key => $item) {
+ $keys[] = $key;
+ }
+
+ return $keys;
}
if (!is_array($array)) {
@@ -769,15 +857,15 @@ function twig_get_array_keys_filter($array)
/**
* Reverses a variable.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param array|Traversable|string $item An array, a Traversable instance, or a string
- * @param Boolean $preserveKeys Whether to preserve key or not
+ * @param bool $preserveKeys Whether to preserve key or not
*
* @return mixed The reversed input
*/
function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false)
{
- if (is_object($item) && $item instanceof Traversable) {
+ if ($item instanceof Traversable) {
return array_reverse(iterator_to_array($item), $preserveKeys);
}
@@ -788,7 +876,7 @@ function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false
if (null !== $charset = $env->getCharset()) {
$string = (string) $item;
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$item = twig_convert_encoding($string, 'UTF-8', $charset);
}
@@ -796,7 +884,7 @@ function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false
$string = implode('', array_reverse($matches[0]));
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, $charset, 'UTF-8');
}
@@ -809,41 +897,88 @@ function twig_reverse_filter(Twig_Environment $env, $item, $preserveKeys = false
/**
* Sorts an array.
*
- * @param array $array An array
+ * @param array|Traversable $array
+ *
+ * @return array
*/
function twig_sort_filter($array)
{
+ if ($array instanceof Traversable) {
+ $array = iterator_to_array($array);
+ } elseif (!is_array($array)) {
+ throw new Twig_Error_Runtime(sprintf('The sort filter only works with arrays or "Traversable", got "%s".', gettype($array)));
+ }
+
asort($array);
return $array;
}
-/* used internally */
+/**
+ * @internal
+ */
function twig_in_filter($value, $compare)
{
if (is_array($compare)) {
- return in_array($value, $compare, is_object($value));
- } elseif (is_string($compare)) {
- if (!strlen($value)) {
- return empty($compare);
+ return in_array($value, $compare, is_object($value) || is_resource($value));
+ } elseif (is_string($compare) && (is_string($value) || is_int($value) || is_float($value))) {
+ return '' === $value || false !== strpos($compare, (string) $value);
+ } elseif ($compare instanceof Traversable) {
+ if (is_object($value) || is_resource($value)) {
+ foreach ($compare as $item) {
+ if ($item === $value) {
+ return true;
+ }
+ }
+ } else {
+ foreach ($compare as $item) {
+ if ($item == $value) {
+ return true;
+ }
+ }
}
- return false !== strpos($compare, (string) $value);
- } elseif (is_object($compare) && $compare instanceof Traversable) {
- return in_array($value, iterator_to_array($compare, false), is_object($value));
+ return false;
}
return false;
}
/**
+ * Returns a trimmed string.
+ *
+ * @return string
+ *
+ * @throws Twig_Error_Runtime When an invalid trimming side is used (not a string or not 'left', 'right', or 'both')
+ */
+function twig_trim_filter($string, $characterMask = null, $side = 'both')
+{
+ if (null === $characterMask) {
+ $characterMask = " \t\n\r\0\x0B";
+ }
+
+ switch ($side) {
+ case 'both':
+ return trim($string, $characterMask);
+ case 'left':
+ return ltrim($string, $characterMask);
+ case 'right':
+ return rtrim($string, $characterMask);
+ default:
+ throw new Twig_Error_Runtime('Trimming side must be "left", "right" or "both".');
+ }
+}
+
+/**
* Escapes a string.
*
- * @param Twig_Environment $env A Twig_Environment instance
- * @param string $string The value to be escaped
+ * @param Twig_Environment $env
+ * @param mixed $string The value to be escaped
* @param string $strategy The escaping strategy
* @param string $charset The charset
- * @param Boolean $autoescape Whether the function is called by the auto-escaping feature (true) or by the developer (false)
+ * @param bool $autoescape Whether the function is called by the auto-escaping feature (true) or by the developer (false)
+ *
+ * @return string
*/
function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html', $charset = null, $autoescape = false)
{
@@ -854,7 +989,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
if (!is_string($string)) {
if (is_object($string) && method_exists($string, '__toString')) {
$string = (string) $string;
- } else {
+ } elseif (in_array($strategy, array('html', 'js', 'css', 'html_attr', 'url'))) {
return $string;
}
}
@@ -865,7 +1000,7 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
switch ($strategy) {
case 'html':
- // see http://php.net/htmlspecialchars
+ // see https://secure.php.net/htmlspecialchars
// Using a static variable to avoid initializing the array
// each time the function is called. Moving the declaration on the
@@ -905,72 +1040,84 @@ function twig_escape_filter(Twig_Environment $env, $string, $strategy = 'html',
case 'js':
// escape all non-alphanumeric characters
- // into their \xHH or \uHHHH representations
- if ('UTF-8' != $charset) {
+ // into their \x or \uHHHH representations
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, 'UTF-8', $charset);
}
- if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
+ if (0 == strlen($string) ? false : 1 !== preg_match('/^./su', $string)) {
throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
}
$string = preg_replace_callback('#[^a-zA-Z0-9,\._]#Su', '_twig_escape_js_callback', $string);
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, $charset, 'UTF-8');
}
return $string;
case 'css':
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, 'UTF-8', $charset);
}
- if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
+ if (0 == strlen($string) ? false : 1 !== preg_match('/^./su', $string)) {
throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
}
$string = preg_replace_callback('#[^a-zA-Z0-9]#Su', '_twig_escape_css_callback', $string);
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, $charset, 'UTF-8');
}
return $string;
case 'html_attr':
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, 'UTF-8', $charset);
}
- if (0 == strlen($string) ? false : (1 == preg_match('/^./su', $string) ? false : true)) {
+ if (0 == strlen($string) ? false : 1 !== preg_match('/^./su', $string)) {
throw new Twig_Error_Runtime('The string to escape is not a valid UTF-8 string.');
}
$string = preg_replace_callback('#[^a-zA-Z0-9,\.\-_]#Su', '_twig_escape_html_attr_callback', $string);
- if ('UTF-8' != $charset) {
+ if ('UTF-8' !== $charset) {
$string = twig_convert_encoding($string, $charset, 'UTF-8');
}
return $string;
case 'url':
- // hackish test to avoid version_compare that is much slower, this works unless PHP releases a 5.10.*
- // at that point however PHP 5.2.* support can be removed
- if (PHP_VERSION < '5.3.0') {
+ if (PHP_VERSION_ID < 50300) {
return str_replace('%7E', '~', rawurlencode($string));
}
return rawurlencode($string);
default:
- throw new Twig_Error_Runtime(sprintf('Invalid escaping strategy "%s" (valid ones: html, js, url, css, and html_attr).', $strategy));
+ static $escapers;
+
+ if (null === $escapers) {
+ $escapers = $env->getExtension('Twig_Extension_Core')->getEscapers();
+ }
+
+ if (isset($escapers[$strategy])) {
+ return call_user_func($escapers[$strategy], $env, $string, $charset);
+ }
+
+ $validStrategies = implode(', ', array_merge(array('html', 'js', 'url', 'css', 'html_attr'), array_keys($escapers)));
+
+ throw new Twig_Error_Runtime(sprintf('Invalid escaping strategy "%s" (valid ones: %s).', $strategy, $validStrategies));
}
}
-/* used internally */
+/**
+ * @internal
+ */
function twig_escape_filter_is_safe(Twig_Node $filterArgs)
{
foreach ($filterArgs as $arg) {
@@ -1005,15 +1152,34 @@ function _twig_escape_js_callback($matches)
{
$char = $matches[0];
- // \xHH
- if (!isset($char[1])) {
- return '\\x'.strtoupper(substr('00'.bin2hex($char), -2));
+ /*
+ * A few characters have short escape sequences in JSON and JavaScript.
+ * Escape sequences supported only by JavaScript, not JSON, are ommitted.
+ * \" is also supported but omitted, because the resulting string is not HTML safe.
+ */
+ static $shortMap = array(
+ '\\' => '\\\\',
+ '/' => '\\/',
+ "\x08" => '\b',
+ "\x0C" => '\f',
+ "\x0A" => '\n',
+ "\x0D" => '\r',
+ "\x09" => '\t',
+ );
+
+ if (isset($shortMap[$char])) {
+ return $shortMap[$char];
}
// \uHHHH
$char = twig_convert_encoding($char, 'UTF-16BE', 'UTF-8');
+ $char = strtoupper(bin2hex($char));
- return '\\u'.strtoupper(substr('0000'.bin2hex($char), -4));
+ if (4 >= strlen($char)) {
+ return sprintf('\u%04s', $char);
+ }
+
+ return sprintf('\u%04s\u%04s', substr($char, 0, -4), substr($char, -4));
}
function _twig_escape_css_callback($matches)
@@ -1039,8 +1205,8 @@ function _twig_escape_css_callback($matches)
/**
* This function is adapted from code coming from Zend Framework.
*
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (https://www.zend.com)
+ * @license https://framework.zend.com/license/new-bsd New BSD License
*/
function _twig_escape_html_attr_callback($matches)
{
@@ -1060,19 +1226,19 @@ function _twig_escape_html_attr_callback($matches)
$chr = $matches[0];
$ord = ord($chr);
- /**
+ /*
* The following replaces characters undefined in HTML with the
* hex entity for the Unicode replacement character.
*/
- if (($ord <= 0x1f && $chr != "\t" && $chr != "\n" && $chr != "\r") || ($ord >= 0x7f && $ord <= 0x9f)) {
+ if (($ord <= 0x1f && "\t" != $chr && "\n" != $chr && "\r" != $chr) || ($ord >= 0x7f && $ord <= 0x9f)) {
return '&#xFFFD;';
}
- /**
+ /*
* Check if the current character to escape has a name entity we should
* replace it with while grabbing the hex value of the character.
*/
- if (strlen($chr) == 1) {
+ if (1 == strlen($chr)) {
$hex = strtoupper(substr('00'.bin2hex($chr), -2));
} else {
$chr = twig_convert_encoding($chr, 'UTF-16BE', 'UTF-8');
@@ -1084,11 +1250,10 @@ function _twig_escape_html_attr_callback($matches)
return sprintf('&%s;', $entityMap[$int]);
}
- /**
+ /*
* Per OWASP recommendations, we'll use hex entities for any other
* characters where a named entity does not exist.
*/
-
return sprintf('&#x%s;', $hex);
}
@@ -1097,27 +1262,51 @@ if (function_exists('mb_get_info')) {
/**
* Returns the length of a variable.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param mixed $thing A variable
*
- * @return integer The length of the value
+ * @return int The length of the value
*/
function twig_length_filter(Twig_Environment $env, $thing)
{
- return is_scalar($thing) ? mb_strlen($thing, $env->getCharset()) : count($thing);
+ if (null === $thing) {
+ return 0;
+ }
+
+ if (is_scalar($thing)) {
+ return mb_strlen($thing, $env->getCharset());
+ }
+
+ if ($thing instanceof \SimpleXMLElement) {
+ return count($thing);
+ }
+
+ if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
+ return mb_strlen((string) $thing, $env->getCharset());
+ }
+
+ if ($thing instanceof \Countable || is_array($thing)) {
+ return count($thing);
+ }
+
+ if ($thing instanceof \IteratorAggregate) {
+ return iterator_count($thing);
+ }
+
+ return 1;
}
/**
* Converts a string to uppercase.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The uppercased string
*/
function twig_upper_filter(Twig_Environment $env, $string)
{
- if (null !== ($charset = $env->getCharset())) {
+ if (null !== $charset = $env->getCharset()) {
return mb_strtoupper($string, $charset);
}
@@ -1127,14 +1316,14 @@ if (function_exists('mb_get_info')) {
/**
* Converts a string to lowercase.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The lowercased string
*/
function twig_lower_filter(Twig_Environment $env, $string)
{
- if (null !== ($charset = $env->getCharset())) {
+ if (null !== $charset = $env->getCharset()) {
return mb_strtolower($string, $charset);
}
@@ -1144,14 +1333,14 @@ if (function_exists('mb_get_info')) {
/**
* Returns a titlecased string.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The titlecased 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);
}
@@ -1161,16 +1350,15 @@ if (function_exists('mb_get_info')) {
/**
* Returns a capitalized string.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The capitalized string
*/
function twig_capitalize_string_filter(Twig_Environment $env, $string)
{
- if (null !== ($charset = $env->getCharset())) {
- return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).
- mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
+ if (null !== $charset = $env->getCharset()) {
+ return mb_strtoupper(mb_substr($string, 0, 1, $charset), $charset).mb_strtolower(mb_substr($string, 1, mb_strlen($string, $charset), $charset), $charset);
}
return ucfirst(strtolower($string));
@@ -1181,20 +1369,44 @@ else {
/**
* Returns the length of a variable.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param mixed $thing A variable
*
- * @return integer The length of the value
+ * @return int The length of the value
*/
function twig_length_filter(Twig_Environment $env, $thing)
{
- return is_scalar($thing) ? strlen($thing) : count($thing);
+ if (null === $thing) {
+ return 0;
+ }
+
+ if (is_scalar($thing)) {
+ return strlen($thing);
+ }
+
+ if ($thing instanceof \SimpleXMLElement) {
+ return count($thing);
+ }
+
+ if (is_object($thing) && method_exists($thing, '__toString') && !$thing instanceof \Countable) {
+ return strlen((string) $thing);
+ }
+
+ if ($thing instanceof \Countable || is_array($thing)) {
+ return count($thing);
+ }
+
+ if ($thing instanceof \IteratorAggregate) {
+ return iterator_count($thing);
+ }
+
+ return 1;
}
/**
* Returns a titlecased string.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The titlecased string
@@ -1207,7 +1419,7 @@ else {
/**
* Returns a capitalized string.
*
- * @param Twig_Environment $env A Twig_Environment instance
+ * @param Twig_Environment $env
* @param string $string A string
*
* @return string The capitalized string
@@ -1218,7 +1430,9 @@ else {
}
}
-/* used internally */
+/**
+ * @internal
+ */
function twig_ensure_traversable($seq)
{
if ($seq instanceof Traversable || is_array($seq)) {
@@ -1240,7 +1454,7 @@ function twig_ensure_traversable($seq)
*
* @param mixed $value A variable
*
- * @return Boolean true if the value is empty, false otherwise
+ * @return bool true if the value is empty, false otherwise
*/
function twig_test_empty($value)
{
@@ -1248,6 +1462,10 @@ function twig_test_empty($value)
return 0 == count($value);
}
+ if (is_object($value) && method_exists($value, '__toString')) {
+ return '' === (string) $value;
+ }
+
return '' === $value || false === $value || null === $value || array() === $value;
}
@@ -1256,14 +1474,14 @@ function twig_test_empty($value)
*
* <pre>
* {# evaluates to true if the foo variable is an array or a traversable object #}
- * {% if foo is traversable %}
+ * {% if foo is iterable %}
* {# ... #}
* {% endif %}
* </pre>
*
* @param mixed $value A variable
*
- * @return Boolean true if the value is traversable
+ * @return bool true if the value is traversable
*/
function twig_test_iterable($value)
{
@@ -1273,38 +1491,86 @@ function twig_test_iterable($value)
/**
* Renders a template.
*
- * @param string $template The template to render
- * @param array $variables The variables to pass to the template
- * @param Boolean $with_context Whether to pass the current context variables or not
- * @param Boolean $ignore_missing Whether to ignore missing templates or not
- * @param Boolean $sandboxed Whether to sandbox the template or not
+ * @param Twig_Environment $env
+ * @param array $context
+ * @param string|array $template The template to render or an array of templates to try consecutively
+ * @param array $variables The variables to pass to the template
+ * @param bool $withContext
+ * @param bool $ignoreMissing Whether to ignore missing templates or not
+ * @param bool $sandboxed Whether to sandbox the template or not
*
* @return string The rendered template
*/
function twig_include(Twig_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false)
{
+ $alreadySandboxed = false;
+ $sandbox = null;
if ($withContext) {
$variables = array_merge($context, $variables);
}
- if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) {
- $sandbox = $env->getExtension('sandbox');
+ if ($isSandboxed = $sandboxed && $env->hasExtension('Twig_Extension_Sandbox')) {
+ $sandbox = $env->getExtension('Twig_Extension_Sandbox');
if (!$alreadySandboxed = $sandbox->isSandboxed()) {
$sandbox->enableSandbox();
}
}
+ $result = null;
try {
- return $env->resolveTemplate($template)->render($variables);
+ $result = $env->resolveTemplate($template)->render($variables);
} catch (Twig_Error_Loader $e) {
if (!$ignoreMissing) {
+ if ($isSandboxed && !$alreadySandboxed) {
+ $sandbox->disableSandbox();
+ }
+
throw $e;
}
+ } catch (Throwable $e) {
+ if ($isSandboxed && !$alreadySandboxed) {
+ $sandbox->disableSandbox();
+ }
+
+ throw $e;
+ } catch (Exception $e) {
+ if ($isSandboxed && !$alreadySandboxed) {
+ $sandbox->disableSandbox();
+ }
+
+ throw $e;
}
if ($isSandboxed && !$alreadySandboxed) {
$sandbox->disableSandbox();
}
+
+ return $result;
+}
+
+/**
+ * Returns a template content without rendering it.
+ *
+ * @param Twig_Environment $env
+ * @param string $name The template name
+ * @param bool $ignoreMissing Whether to ignore missing templates or not
+ *
+ * @return string The template source
+ */
+function twig_source(Twig_Environment $env, $name, $ignoreMissing = false)
+{
+ $loader = $env->getLoader();
+ try {
+ if (!$loader instanceof Twig_SourceContextLoaderInterface) {
+ return $loader->getSource($name);
+ } else {
+ return $loader->getSourceContext($name)->getCode();
+ }
+ } catch (Twig_Error_Loader $e) {
+ if (!$ignoreMissing) {
+ throw $e;
+ }
+ }
}
/**
@@ -1325,17 +1591,34 @@ function twig_constant($constant, $object = null)
}
/**
+ * Checks if a constant exists.
+ *
+ * @param string $constant The name of the constant
+ * @param null|object $object The object to get the constant from
+ *
+ * @return bool
+ */
+function twig_constant_is_defined($constant, $object = null)
+{
+ if (null !== $object) {
+ $constant = get_class($object).'::'.$constant;
+ }
+
+ return defined($constant);
+}
+
+/**
* Batches item.
*
- * @param array $items An array of items
- * @param integer $size The size of the batch
- * @param mixed $fill A value used to fill missing items
+ * @param array $items An array of items
+ * @param int $size The size of the batch
+ * @param mixed $fill A value used to fill missing items
*
* @return array
*/
function twig_array_batch($items, $size, $fill = null)
{
- if (is_object($items) && $items instanceof Traversable) {
+ if ($items instanceof Traversable) {
$items = iterator_to_array($items, false);
}
@@ -1343,7 +1626,7 @@ function twig_array_batch($items, $size, $fill = null)
$result = array_chunk($items, $size, true);
- if (null !== $fill) {
+ if (null !== $fill && !empty($result)) {
$last = count($result) - 1;
if ($fillCount = $size - count($result[$last])) {
$result[$last] = array_merge(
@@ -1355,3 +1638,5 @@ function twig_array_batch($items, $size, $fill = null)
return $result;
}
+
+class_alias('Twig_Extension_Core', 'Twig\Extension\CoreExtension', false);