Expanded class hierarchy of Twig_Extension_Core
class Twig_Extension_Core extends Twig_Extension {
protected $dateFormats = array(
'F j, Y H:i',
'%d days',
);
protected $numberFormat = array(
0,
'.',
',',
);
protected $timezone = null;
/**
* Sets the default format to be used by the date filter.
*
* @param string $format The default date format string
* @param string $dateIntervalFormat The default date interval format string
*/
public function setDateFormat($format = null, $dateIntervalFormat = null) {
if (null !== $format) {
$this->dateFormats[0] = $format;
}
if (null !== $dateIntervalFormat) {
$this->dateFormats[1] = $dateIntervalFormat;
}
}
/**
* Gets the default format to be used by the date filter.
*
* @return array The default date format string and the default date interval format string
*/
public function getDateFormat() {
return $this->dateFormats;
}
/**
* Sets the default timezone to be used by the date filter.
*
* @param DateTimeZone|string $timezone The default timezone string or a DateTimeZone object
*/
public function setTimezone($timezone) {
$this->timezone = $timezone instanceof DateTimeZone ? $timezone : new DateTimeZone($timezone);
}
/**
* Gets the default timezone to be used by the date filter.
*
* @return DateTimeZone The default timezone currently in use
*/
public function getTimezone() {
if (null === $this->timezone) {
$this->timezone = new DateTimeZone(date_default_timezone_get());
}
return $this->timezone;
}
/**
* 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.
*/
public function setNumberFormat($decimal, $decimalPoint, $thousandSep) {
$this->numberFormat = array(
$decimal,
$decimalPoint,
$thousandSep,
);
}
/**
* Get the default format used by the number_format filter.
*
* @return array The arguments for number_format()
*/
public function getNumberFormat() {
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(
new Twig_TokenParser_For(),
new Twig_TokenParser_If(),
new Twig_TokenParser_Extends(),
new Twig_TokenParser_Include(),
new Twig_TokenParser_Block(),
new Twig_TokenParser_Use(),
new Twig_TokenParser_Filter(),
new Twig_TokenParser_Macro(),
new Twig_TokenParser_Import(),
new Twig_TokenParser_From(),
new Twig_TokenParser_Set(),
new Twig_TokenParser_Spaceless(),
new Twig_TokenParser_Flush(),
new Twig_TokenParser_Do(),
new Twig_TokenParser_Embed(),
);
}
/**
* Returns a list of filters to add to the existing list.
*
* @return array An array of filters
*/
public function getFilters() {
$filters = array(
// formatting filters
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('number_format', 'twig_number_format_filter', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('abs', 'abs'),
// encoding
new Twig_SimpleFilter('url_encode', 'twig_urlencode_filter'),
new Twig_SimpleFilter('json_encode', 'twig_jsonencode_filter'),
new Twig_SimpleFilter('convert_encoding', 'twig_convert_encoding'),
// string filters
new Twig_SimpleFilter('title', 'twig_title_string_filter', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('capitalize', 'twig_capitalize_string_filter', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('upper', 'strtoupper'),
new Twig_SimpleFilter('lower', 'strtolower'),
new Twig_SimpleFilter('striptags', 'strip_tags'),
new Twig_SimpleFilter('trim', 'trim'),
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('sort', 'twig_sort_filter'),
new Twig_SimpleFilter('merge', 'twig_array_merge'),
new Twig_SimpleFilter('batch', 'twig_array_batch'),
// string/array filters
new Twig_SimpleFilter('reverse', 'twig_reverse_filter', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('length', 'twig_length_filter', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('slice', 'twig_slice', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('first', 'twig_first', array(
'needs_environment' => true,
)),
new Twig_SimpleFilter('last', 'twig_last', array(
'needs_environment' => true,
)),
// iteration and runtime
new Twig_SimpleFilter('default', '_twig_default_filter', array(
'node_class' => 'Twig_Node_Expression_Filter_Default',
)),
new Twig_SimpleFilter('keys', 'twig_get_array_keys_filter'),
// escaping
new Twig_SimpleFilter('escape', 'twig_escape_filter', array(
'needs_environment' => true,
'is_safe_callback' => 'twig_escape_filter_is_safe',
)),
new Twig_SimpleFilter('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_SimpleFilter('upper', 'twig_upper_filter', array(
'needs_environment' => true,
));
$filters[] = new Twig_SimpleFilter('lower', 'twig_lower_filter', array(
'needs_environment' => true,
));
}
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('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,
)),
);
}
/**
* 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('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('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(
'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,
),
),
);
}
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';
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Twig_Extension_Core:: |
protected | property | ||
Twig_Extension_Core:: |
protected | property | ||
Twig_Extension_Core:: |
protected | property | ||
Twig_Extension_Core:: |
public | function | Gets the default format to be used by the date filter. | |
Twig_Extension_Core:: |
public | function | Returns a list of filters to add to the existing list. | |
Twig_Extension_Core:: |
public | function | Returns a list of global functions to add to the existing list. | |
Twig_Extension_Core:: |
public | function | Returns the name of the extension. | |
Twig_Extension_Core:: |
public | function | Get the default format used by the number_format filter. | |
Twig_Extension_Core:: |
public | function | Returns a list of operators to add to the existing list. | |
Twig_Extension_Core:: |
protected | function | ||
Twig_Extension_Core:: |
public | function | Returns a list of tests to add to the existing list. | |
Twig_Extension_Core:: |
public | function | Gets the default timezone to be used by the date filter. | |
Twig_Extension_Core:: |
public | function | Returns the token parser instance to add to the existing list. | |
Twig_Extension_Core:: |
public | function | ||
Twig_Extension_Core:: |
public | function | ||
Twig_Extension_Core:: |
public | function | Sets the default format to be used by the date filter. | |
Twig_Extension_Core:: |
public | function | Sets the default format to be used by the number_format filter. | |
Twig_Extension_Core:: |
public | function | Sets the default timezone to be used by the date filter. |