You are here

class Twig_Extension_Core in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/Extension/Core.php \Twig_Extension_Core

Hierarchy

Expanded class hierarchy of Twig_Extension_Core

File

vendor/Twig/Extension/Core.php, line 16

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;
  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.
   *
   * @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 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) {
    $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 Twig_TokenParser[] 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'),
      new Twig_SimpleFilter('round', 'twig_round'),
      // 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', 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'),
      // 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('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('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('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(
        '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,
        ),
        '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,
          '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, Twig_NodeInterface $node) {
    return new Twig_Node_Expression_Unary_Not($this
      ->parseTestExpression($parser, $node), $parser
      ->getCurrentToken()
      ->getLine());
  }
  public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node) {
    $stream = $parser
      ->getStream();
    $name = $this
      ->getTestName($parser, $node
      ->getLine());
    $class = $this
      ->getTestNodeClass($parser, $name);
    $arguments = null;
    if ($stream
      ->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
      $arguments = $parser
        ->getExpressionParser()
        ->parseArguments(true);
    }
    return new $class($node, $name, $arguments, $parser
      ->getCurrentToken()
      ->getLine());
  }
  protected function getTestName(Twig_Parser $parser, $line) {
    $stream = $parser
      ->getStream();
    $name = $stream
      ->expect(Twig_Token::NAME_TYPE)
      ->getValue();
    $env = $parser
      ->getEnvironment();
    $testMap = $env
      ->getTests();
    if (isset($testMap[$name])) {
      return $name;
    }
    if ($stream
      ->test(Twig_Token::NAME_TYPE)) {

      // try 2-words tests
      $name = $name . ' ' . $parser
        ->getCurrentToken()
        ->getValue();
      if (isset($testMap[$name])) {
        $parser
          ->getStream()
          ->next();
        return $name;
      }
    }
    $message = sprintf('The test "%s" does not exist', $name);
    if ($alternatives = $env
      ->computeAlternatives($name, array_keys($testMap))) {
      $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
    }
    throw new Twig_Error_Syntax($message, $line, $parser
      ->getFilename());
  }
  protected function getTestNodeClass(Twig_Parser $parser, $name) {
    $env = $parser
      ->getEnvironment();
    $testMap = $env
      ->getTests();
    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';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Twig_Extension::getGlobals public function Returns a list of global variables to add to the existing list. Overrides Twig_ExtensionInterface::getGlobals 1
Twig_Extension::getNodeVisitors public function Returns the node visitor instances to add to the existing list. Overrides Twig_ExtensionInterface::getNodeVisitors 4
Twig_Extension::initRuntime public function Initializes the runtime environment. Overrides Twig_ExtensionInterface::initRuntime
Twig_Extension_Core::$dateFormats protected property
Twig_Extension_Core::$escapers 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::getEscapers public function Gets all defined escapers.
Twig_Extension_Core::getFilters public function Returns a list of filters to add to the existing list. Overrides Twig_Extension::getFilters
Twig_Extension_Core::getFunctions public function Returns a list of global functions to add to the existing list. Overrides Twig_Extension::getFunctions
Twig_Extension_Core::getName public function Returns the name of the extension. Overrides Twig_ExtensionInterface::getName
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. Overrides Twig_Extension::getOperators
Twig_Extension_Core::getTestName protected function
Twig_Extension_Core::getTestNodeClass protected function
Twig_Extension_Core::getTests public function Returns a list of tests to add to the existing list. Overrides Twig_Extension::getTests
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. Overrides Twig_Extension::getTokenParsers
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::setEscaper public function Defines a new escaper to be used via the escape 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.