[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/TemplateNormalizations/ -> AbstractConstantFolding.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
   9  
  10  use DOMAttr;
  11  use DOMElement;
  12  use s9e\TextFormatter\Configurator\Helpers\AVTHelper;
  13  
  14  abstract class AbstractConstantFolding extends AbstractNormalization
  15  {
  16      /**
  17      * {@inheritdoc}
  18      */
  19      protected $queries = [
  20          '//*[namespace-uri() != $XSL]/@*[contains(.,"{")]',
  21          '//xsl:if[@test]',
  22          '//xsl:value-of[@select]',
  23          '//xsl:when[@test]'
  24      ];
  25  
  26      /**
  27      * Return the optimization passes supported by the concrete implementation
  28      *
  29      * @return array Regexps as keys, method names as values
  30      */
  31      abstract protected function getOptimizationPasses();
  32  
  33      /**
  34      * Evaluate given expression and return the result
  35      *
  36      * @param  string $expr
  37      * @return string
  38      */
  39  	protected function evaluateExpression($expr)
  40      {
  41          $original = $expr;
  42          foreach ($this->getOptimizationPasses() as $regexp => $methodName)
  43          {
  44              $regexp = str_replace(' ', '\\s*', $regexp);
  45              $expr   = preg_replace_callback($regexp, [$this, $methodName], $expr);
  46          }
  47  
  48          return ($expr === $original) ? $expr : $this->evaluateExpression(trim($expr));
  49      }
  50  
  51      /**
  52      * Replace constant expressions in given AVT
  53      *
  54      * @param  DOMAttr $attribute
  55      * @return void
  56      */
  57  	protected function normalizeAttribute(DOMAttr $attribute)
  58      {
  59          AVTHelper::replace(
  60              $attribute,
  61              function ($token)
  62              {
  63                  if ($token[0] === 'expression')
  64                  {
  65                      $token[1] = $this->evaluateExpression($token[1]);
  66                  }
  67  
  68                  return $token;
  69              }
  70          );
  71      }
  72  
  73      /**
  74      * Replace constant expressions in given XSL element
  75      *
  76      * @param  DOMElement $element
  77      * @return void
  78      */
  79  	protected function normalizeElement(DOMElement $element)
  80      {
  81          $attrName = ($element->localName === 'value-of') ? 'select' : 'test';
  82          $oldExpr  = $element->getAttribute($attrName);
  83          $newExpr  = $this->evaluateExpression($oldExpr);
  84          if ($newExpr !== $oldExpr)
  85          {
  86              $element->setAttribute($attrName, $newExpr);
  87          }
  88      }
  89  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1