class Twig_Extension_Core

Hierarchy

Expanded class hierarchy of Twig_Extension_Core

File

drupal/core/vendor/twig/twig/lib/Twig/Extension/Core.php, line 15

View source
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() {
    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
      'date' => new Twig_Filter_Function('twig_date_format_filter', array(
        'needs_environment' => true,
      )),
      'format' => new Twig_Filter_Function('sprintf'),
      'replace' => new Twig_Filter_Function('strtr'),
      'number_format' => new Twig_Filter_Function('twig_number_format_filter', array(
        'needs_environment' => true,
      )),
      'abs' => new Twig_Filter_Function('abs'),
      // encoding
      'url_encode' => new Twig_Filter_Function('twig_urlencode_filter'),
      'json_encode' => new Twig_Filter_Function('twig_jsonencode_filter'),
      'convert_encoding' => new Twig_Filter_Function('twig_convert_encoding'),
      // string filters
      'title' => new Twig_Filter_Function('twig_title_string_filter', array(
        'needs_environment' => true,
      )),
      'capitalize' => new Twig_Filter_Function('twig_capitalize_string_filter', array(
        'needs_environment' => true,
      )),
      'upper' => new Twig_Filter_Function('strtoupper'),
      'lower' => new Twig_Filter_Function('strtolower'),
      'striptags' => new Twig_Filter_Function('strip_tags'),
      'trim' => new Twig_Filter_Function('trim'),
      'nl2br' => new Twig_Filter_Function('nl2br', array(
        'pre_escape' => 'html',
        'is_safe' => array(
          'html',
        ),
      )),
      // array helpers
      'join' => new Twig_Filter_Function('twig_join_filter'),
      'sort' => new Twig_Filter_Function('twig_sort_filter'),
      'merge' => new Twig_Filter_Function('twig_array_merge'),
      // string/array filters
      'reverse' => new Twig_Filter_Function('twig_reverse_filter', array(
        'needs_environment' => true,
      )),
      'length' => new Twig_Filter_Function('twig_length_filter', array(
        'needs_environment' => true,
      )),
      'slice' => new Twig_Filter_Function('twig_slice', array(
        'needs_environment' => true,
      )),
      // iteration and runtime
      'default' => new Twig_Filter_Node('Twig_Node_Expression_Filter_Default'),
      '_default' => new Twig_Filter_Function('_twig_default_filter'),
      'keys' => new Twig_Filter_Function('twig_get_array_keys_filter'),
      // escaping
      'escape' => new Twig_Filter_Function('twig_escape_filter', array(
        'needs_environment' => true,
        'is_safe_callback' => 'twig_escape_filter_is_safe',
      )),
      'e' => new Twig_Filter_Function('twig_escape_filter', array(
        'needs_environment' => true,
        'is_safe_callback' => 'twig_escape_filter_is_safe',
      )),
    );
    if (function_exists('mb_get_info')) {
      $filters['upper'] = new Twig_Filter_Function('twig_upper_filter', array(
        'needs_environment' => true,
      ));
      $filters['lower'] = new Twig_Filter_Function('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(
      'range' => new Twig_Function_Function('range'),
      'constant' => new Twig_Function_Function('constant'),
      'cycle' => new Twig_Function_Function('twig_cycle'),
      'random' => new Twig_Function_Function('twig_random', array(
        'needs_environment' => true,
      )),
      'date' => new Twig_Function_Function('twig_date_converter', array(
        'needs_environment' => true,
      )),
    );
  }

  /**
   * Returns a list of tests to add to the existing list.
   *
   * @return array An array of tests
   */
  public function getTests() {
    return array(
      'even' => new Twig_Test_Node('Twig_Node_Expression_Test_Even'),
      'odd' => new Twig_Test_Node('Twig_Node_Expression_Test_Odd'),
      'defined' => new Twig_Test_Node('Twig_Node_Expression_Test_Defined'),
      'sameas' => new Twig_Test_Node('Twig_Node_Expression_Test_Sameas'),
      'none' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),
      'null' => new Twig_Test_Node('Twig_Node_Expression_Test_Null'),
      'divisibleby' => new Twig_Test_Node('Twig_Node_Expression_Test_Divisibleby'),
      'constant' => new Twig_Test_Node('Twig_Node_Expression_Test_Constant'),
      'empty' => new Twig_Test_Function('twig_test_empty'),
      'iterable' => new Twig_Test_Function('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(
        'b-and' => array(
          'precedence' => 5,
          'class' => 'Twig_Node_Expression_Binary_BitwiseAnd',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'b-xor' => array(
          'precedence' => 5,
          'class' => 'Twig_Node_Expression_Binary_BitwiseXor',
          'associativity' => Twig_ExpressionParser::OPERATOR_LEFT,
        ),
        'b-or' => array(
          'precedence' => 5,
          'class' => 'Twig_Node_Expression_Binary_BitwiseOr',
          '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,
        ),
        '==' => 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();
    }
    $class = $this
      ->getTestNodeClass($parser
      ->getEnvironment(), $name);
    return new $class($node, $name, $arguments, $parser
      ->getCurrentToken()
      ->getLine());
  }
  protected function getTestNodeClass(Twig_Environment $env, $name) {
    $testMap = $env
      ->getTests();
    if (isset($testMap[$name]) && $testMap[$name] instanceof Twig_Test_Node) {
      return $testMap[$name]
        ->getClass();
    }
    return 'Twig_Node_Expression_Test';
  }

  /**
   * Returns the name of the extension.
   *
   * @return string The extension name
   */
  public function getName() {
    return 'core';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Twig_Extension_Core::$dateFormats protected property
Twig_Extension_Core::$numberFormat protected property
Twig_Extension_Core::$timezone protected property
Twig_Extension_Core::getDateFormat public function Gets the default format to be used by the date filter.
Twig_Extension_Core::getFilters public function Returns a list of filters to add to the existing list.
Twig_Extension_Core::getFunctions public function Returns a list of global functions to add to the existing list.
Twig_Extension_Core::getName public function Returns the name of the extension.
Twig_Extension_Core::getNumberFormat public function Get the default format used by the number_format filter.
Twig_Extension_Core::getOperators public function Returns a list of operators to add to the existing list.
Twig_Extension_Core::getTestNodeClass protected function
Twig_Extension_Core::getTests public function Returns a list of tests to add to the existing list.
Twig_Extension_Core::getTimezone public function Gets the default timezone to be used by the date filter.
Twig_Extension_Core::getTokenParsers public function Returns the token parser instance to add to the existing list.
Twig_Extension_Core::parseNotTestExpression public function
Twig_Extension_Core::parseTestExpression public function
Twig_Extension_Core::setDateFormat public function Sets the default format to be used by the date filter.
Twig_Extension_Core::setNumberFormat public function Sets the default format to be used by the number_format filter.
Twig_Extension_Core::setTimezone public function Sets the default timezone to be used by the date filter.