[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/JavaScript/ -> StylesheetCompressor.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\JavaScript;
   9  use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
  10  use s9e\TextFormatter\Configurator\JavaScript\Code;
  11  class StylesheetCompressor
  12  {
  13      protected $deduplicateTargets = [
  14          '<xsl:template match="',
  15          '</xsl:template>',
  16          '<xsl:apply-templates/>',
  17          '<param name="allowfullscreen" value="true"/>',
  18          '<xsl:value-of select="',
  19          '<xsl:copy-of select="@',
  20          '<iframe allowfullscreen="" scrolling="no"',
  21          'display:block;overflow:hidden;position:relative;padding-bottom:',
  22          'display:inline-block;width:100%;max-width:',
  23          ' [-:\\w]++="',
  24          '\\{[^}]++\\}',
  25          '@[-\\w]{4,}+',
  26          '(?<=<)[-:\\w]{4,}+',
  27          '(?<==")[^"]{4,}+"'
  28      ];
  29      protected $dictionary;
  30      protected $keyPrefix = '$';
  31      public $minSaving = 10;
  32      protected $savings;
  33      protected $xsl;
  34  	public function encode($xsl)
  35      {
  36          $this->xsl = $xsl;
  37          $this->estimateSavings();
  38          $this->filterSavings();
  39          $this->buildDictionary();
  40          $js = \json_encode($this->getCompressedStylesheet());
  41          if (!empty($this->dictionary))
  42              $js .= '.replace(' . $this->getReplacementRegexp() . ',function(k){return' . \json_encode($this->dictionary) . '[k]})';
  43          return $js;
  44      }
  45  	protected function buildDictionary()
  46      {
  47          $keys = $this->getAvailableKeys();
  48          \rsort($keys);
  49          $this->dictionary = [];
  50          \arsort($this->savings);
  51          foreach (\array_keys($this->savings) as $str)
  52          {
  53              $key = \array_pop($keys);
  54              if (!$key)
  55                  break;
  56              $this->dictionary[$key] = $str;
  57          }
  58      }
  59  	protected function estimateSavings()
  60      {
  61          $this->savings = [];
  62          foreach ($this->getStringsFrequency() as $str => $cnt)
  63          {
  64              $len             = \strlen($str);
  65              $originalCost    = $cnt * $len;
  66              $replacementCost = $cnt * 2;
  67              $overhead        = $len + 6;
  68              $this->savings[$str] = $originalCost - ($replacementCost + $overhead);
  69          }
  70      }
  71  	protected function filterSavings()
  72      {
  73          $this->savings = \array_filter(
  74              $this->savings,
  75              function ($saving)
  76              {
  77                  return ($saving >= $this->minSaving);
  78              }
  79          );
  80      }
  81  	protected function getAvailableKeys()
  82      {
  83          return \array_diff($this->getPossibleKeys(), $this->getUnavailableKeys());
  84      }
  85  	protected function getCompressedStylesheet()
  86      {
  87          return \strtr($this->xsl, \array_flip($this->dictionary));
  88      }
  89  	protected function getPossibleKeys()
  90      {
  91          $keys = [];
  92          foreach (\range('a', 'z') as $char)
  93              $keys[] = $this->keyPrefix . $char;
  94          return $keys;
  95      }
  96  	protected function getReplacementRegexp()
  97      {
  98          return '/' . RegexpBuilder::fromList(\array_keys($this->dictionary)) . '/g';
  99      }
 100  	protected function getStringsFrequency()
 101      {
 102          $regexp = '(' . \implode('|', $this->deduplicateTargets) . ')S';
 103          \preg_match_all($regexp, $this->xsl, $matches);
 104          return \array_count_values($matches[0]);
 105      }
 106  	protected function getUnavailableKeys()
 107      {
 108          \preg_match_all('(' . \preg_quote($this->keyPrefix) . '.)', $this->xsl, $matches);
 109          return \array_unique($matches[0]);
 110      }
 111  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1