[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/phpbb/template/twig/ -> extension.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\template\twig;
  15  
  16  class extension extends \Twig_Extension
  17  {
  18      /** @var \phpbb\template\context */
  19      protected $context;
  20  
  21      /** @var \phpbb\template\twig\environment */
  22      protected $environment;
  23  
  24      /** @var \phpbb\language\language */
  25      protected $language;
  26  
  27      /**
  28      * Constructor
  29      *
  30      * @param \phpbb\template\context $context
  31      * @param \phpbb\template\twig\environment $environment
  32      * @param \phpbb\language\language $language
  33      */
  34  	public function __construct(\phpbb\template\context $context, \phpbb\template\twig\environment $environment, $language)
  35      {
  36          $this->context = $context;
  37          $this->environment = $environment;
  38          $this->language = $language;
  39      }
  40  
  41      /**
  42      * Get the name of this extension
  43      *
  44      * @return string
  45      */
  46  	public function getName()
  47      {
  48          return 'phpbb';
  49      }
  50  
  51      /**
  52      * Returns the token parser instance to add to the existing list.
  53      *
  54      * @return array An array of Twig_TokenParser instances
  55      */
  56  	public function getTokenParsers()
  57      {
  58          return array(
  59              new \phpbb\template\twig\tokenparser\defineparser,
  60              new \phpbb\template\twig\tokenparser\includeparser,
  61              new \phpbb\template\twig\tokenparser\includejs,
  62              new \phpbb\template\twig\tokenparser\includecss,
  63              new \phpbb\template\twig\tokenparser\event($this->environment),
  64              new \phpbb\template\twig\tokenparser\includephp($this->environment),
  65              new \phpbb\template\twig\tokenparser\php($this->environment),
  66          );
  67      }
  68  
  69      /**
  70      * Returns a list of filters to add to the existing list.
  71      *
  72      * @return array An array of filters
  73      */
  74  	public function getFilters()
  75      {
  76          return array(
  77              new \Twig_SimpleFilter('subset', array($this, 'loop_subset'), array('needs_environment' => true)),
  78              // @deprecated 3.2.0 Uses twig's JS escape method instead of addslashes
  79              new \Twig_SimpleFilter('addslashes', 'addslashes'),
  80          );
  81      }
  82  
  83      /**
  84      * Returns a list of global functions to add to the existing list.
  85      *
  86      * @return array An array of global functions
  87      */
  88  	public function getFunctions()
  89      {
  90          return array(
  91              new \Twig_SimpleFunction('lang', array($this, 'lang')),
  92              new \Twig_SimpleFunction('lang_defined', array($this, 'lang_defined')),
  93              new \Twig_SimpleFunction('get_class', 'get_class'),
  94          );
  95      }
  96  
  97      /**
  98      * Returns a list of operators to add to the existing list.
  99      *
 100      * @return array An array of operators
 101      */
 102  	public function getOperators()
 103      {
 104          return array(
 105              array(
 106                  '!' => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Not'),
 107              ),
 108              array(
 109                  // precedence settings are copied from similar operators in Twig core extension
 110                  '||' => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 111                  '&&' => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 112  
 113                  'eq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 114  
 115                  'ne' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 116                  'neq' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 117                  '<>' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_NotEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 118  
 119                  '===' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\equalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 120                  '!==' => array('precedence' => 20, 'class' => '\phpbb\template\twig\node\expression\binary\notequalequal', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 121  
 122                  'gt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Greater', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 123                  'gte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 124                  'ge' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_GreaterEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 125                  'lt' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Less', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 126                  'lte' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 127                  'le' => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_LessEqual', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 128  
 129                  'mod' => array('precedence' => 60, 'class' => 'Twig_Node_Expression_Binary_Mod', 'associativity' => \Twig_ExpressionParser::OPERATOR_LEFT),
 130              ),
 131          );
 132      }
 133  
 134      /**
 135      * Grabs a subset of a loop
 136      *
 137      * @param \Twig_Environment $env          A Twig_Environment instance
 138      * @param mixed            $item         A variable
 139      * @param integer          $start        Start of the subset
 140      * @param integer          $end            End of the subset
 141      * @param Boolean          $preserveKeys Whether to preserve key or not (when the input is an array)
 142      *
 143      * @return mixed The sliced variable
 144      */
 145  	public function loop_subset(\Twig_Environment $env, $item, $start, $end = null, $preserveKeys = false)
 146      {
 147          // We do almost the same thing as Twig's slice (array_slice), except when $end is positive
 148          if ($end >= 1)
 149          {
 150              // When end is > 1, subset will end on the last item in an array with the specified $end
 151              // This is different from slice in that it is the number we end on rather than the number
 152              //  of items to grab (length)
 153  
 154              // Start must always be the actual starting number for this calculation (not negative)
 155              $start = ($start < 0) ? count($item) + $start : $start;
 156              $end = $end - $start;
 157          }
 158  
 159          // We always include the last element (this was the past design)
 160          $end = ($end == -1 || $end === null) ? null : $end + 1;
 161  
 162          return twig_slice($env, $item, $start, $end, $preserveKeys);
 163      }
 164  
 165      /**
 166      * Get output for a language variable (L_FOO, LA_FOO)
 167      *
 168      * This function checks to see if the language var was outputted to $context
 169      * (e.g. in the ACP, L_TITLE)
 170      * If not, we return the result of $user->lang()
 171      *
 172      * @return string
 173      */
 174  	public function lang()
 175      {
 176          $args = func_get_args();
 177          $key = $args[0];
 178  
 179          $context_vars = $this->context->get_root_ref();
 180  
 181          if (is_string($key) && isset($context_vars['L_' . $key]))
 182          {
 183              return $context_vars['L_' . $key];
 184          }
 185  
 186          // LA_ is transformed into lang(\'$1\')|escape('js'), so we should not
 187          // need to check for it
 188  
 189          return call_user_func_array(array($this->language, 'lang'), $args);
 190      }
 191  
 192      /**
 193       * Check if a language variable exists
 194       *
 195       * @return bool
 196       */
 197  	public function lang_defined($key)
 198      {
 199          return call_user_func_array([$this->language, 'is_set'], [$key]);
 200      }
 201  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1