[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
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 class ConfigOptimizer 10 { 11 protected $configValues; 12 protected $encoder; 13 protected $jsLengths; 14 public function __construct(Encoder $encoder) 15 { 16 $this->encoder = $encoder; 17 $this->reset(); 18 } 19 public function getVarDeclarations() 20 { 21 \asort($this->jsLengths); 22 $src = ''; 23 foreach (\array_keys($this->jsLengths) as $varName) 24 { 25 $configValue = $this->configValues[$varName]; 26 if ($configValue->isDeduplicated()) 27 $src .= '/** @const */ var ' . $varName . '=' . $this->encoder->encode($configValue->getValue()) . ";\n"; 28 } 29 return $src; 30 } 31 public function optimize($object) 32 { 33 return \current($this->optimizeObjectContent([$object]))->getValue(); 34 } 35 public function reset() 36 { 37 $this->configValues = []; 38 $this->jsLengths = []; 39 } 40 protected function canDeduplicate($value) 41 { 42 if (\is_array($value) || $value instanceof Dictionary) 43 return (bool) \count($value); 44 return ($value instanceof Code); 45 } 46 protected function deduplicateConfigValues() 47 { 48 \arsort($this->jsLengths); 49 foreach (\array_keys($this->jsLengths) as $varName) 50 { 51 $configValue = $this->configValues[$varName]; 52 if ($configValue->getUseCount() > 1) 53 $configValue->deduplicate(); 54 } 55 } 56 protected function getVarName($js) 57 { 58 return \sprintf('o%08X', \crc32($js)); 59 } 60 protected function isIterable($value) 61 { 62 return (\is_array($value) || $value instanceof Dictionary); 63 } 64 protected function optimizeObjectContent($object) 65 { 66 $object = $this->recordObject($object); 67 $this->deduplicateConfigValues(); 68 return $object->getValue(); 69 } 70 protected function recordObject($object) 71 { 72 $js = $this->encoder->encode($object); 73 $varName = $this->getVarName($js); 74 if ($this->isIterable($object)) 75 $object = $this->recordObjectContent($object); 76 if (!isset($this->configValues[$varName])) 77 { 78 $this->configValues[$varName] = new ConfigValue($object, $varName); 79 $this->jsLengths[$varName] = \strlen($js); 80 } 81 $this->configValues[$varName]->incrementUseCount(); 82 return $this->configValues[$varName]; 83 } 84 protected function recordObjectContent($object) 85 { 86 foreach ($object as $k => $v) 87 if ($this->canDeduplicate($v) && !$this->shouldPreserve($v)) 88 $object[$k] = $this->recordObject($v); 89 return $object; 90 } 91 protected function shouldPreserve($value) 92 { 93 return ($value instanceof Code && \preg_match('(^\\w+$)', $value)); 94 } 95 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |