[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
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\JavaScript; 9 10 /** 11 * This class creates local variables to deduplicate complex configValues 12 */ 13 class ConfigOptimizer 14 { 15 /** 16 * @var array Associative array of ConfigValue instances 17 */ 18 protected $configValues; 19 20 /** 21 * @var Encoder 22 */ 23 protected $encoder; 24 25 /** 26 * @var array Associative array with the length of the JavaScript representation of each value 27 */ 28 protected $jsLengths; 29 30 /** 31 * Constructor 32 * 33 * @param Encoder $encoder 34 */ 35 public function __construct(Encoder $encoder) 36 { 37 $this->encoder = $encoder; 38 $this->reset(); 39 } 40 41 /** 42 * Return the var declarations for all deduplicated config values 43 * 44 * @return string JavaScript code 45 */ 46 public function getVarDeclarations() 47 { 48 asort($this->jsLengths); 49 50 $src = ''; 51 foreach (array_keys($this->jsLengths) as $varName) 52 { 53 $configValue = $this->configValues[$varName]; 54 if ($configValue->isDeduplicated()) 55 { 56 $src .= '/** @const */ var ' . $varName . '=' . $this->encoder->encode($configValue->getValue()) . ";\n"; 57 } 58 } 59 60 return $src; 61 } 62 63 /** 64 * Optimize given config object 65 * 66 * @param array|Dictionary $object Original config object 67 * @return array|Dictionary Modified config object 68 */ 69 public function optimize($object) 70 { 71 return current($this->optimizeObjectContent([$object]))->getValue(); 72 } 73 74 /** 75 * Clear the deduplicated config values stored in this instance 76 * 77 * @return void 78 */ 79 public function reset() 80 { 81 $this->configValues = []; 82 $this->jsLengths = []; 83 } 84 85 /** 86 * Test whether given value can be deduplicated 87 * 88 * @param mixed $value 89 * @return bool 90 */ 91 protected function canDeduplicate($value) 92 { 93 if (is_array($value) || $value instanceof Dictionary) 94 { 95 // Do not deduplicate empty arrays and dictionaries 96 return (bool) count($value); 97 } 98 99 return ($value instanceof Code); 100 } 101 102 /** 103 * Mark ConfigValue instances that have been used multiple times 104 * 105 * @return void 106 */ 107 protected function deduplicateConfigValues() 108 { 109 arsort($this->jsLengths); 110 foreach (array_keys($this->jsLengths) as $varName) 111 { 112 $configValue = $this->configValues[$varName]; 113 if ($configValue->getUseCount() > 1) 114 { 115 $configValue->deduplicate(); 116 } 117 } 118 } 119 120 /** 121 * Return the name of the variable that will a given value 122 * 123 * @param string $js JavaScript representation of the value 124 * @return string 125 */ 126 protected function getVarName($js) 127 { 128 return sprintf('o%08X', crc32($js)); 129 } 130 131 /** 132 * Test whether given value is iterable 133 * 134 * @param mixed $value 135 * @return bool 136 */ 137 protected function isIterable($value) 138 { 139 return (is_array($value) || $value instanceof Dictionary); 140 } 141 142 /** 143 * Optimize given object's content 144 * 145 * @param array|Dictionary $object Original object 146 * @return array|Dictionary Modified object 147 */ 148 protected function optimizeObjectContent($object) 149 { 150 $object = $this->recordObject($object); 151 $this->deduplicateConfigValues(); 152 153 return $object->getValue(); 154 } 155 156 /** 157 * Record a given config object as a ConfigValue instance 158 * 159 * @param array|Code|Dictionary $object Original object 160 * @return ConfigValue Stored ConfigValue instance 161 */ 162 protected function recordObject($object) 163 { 164 $js = $this->encoder->encode($object); 165 $varName = $this->getVarName($js); 166 167 if ($this->isIterable($object)) 168 { 169 $object = $this->recordObjectContent($object); 170 } 171 172 if (!isset($this->configValues[$varName])) 173 { 174 $this->configValues[$varName] = new ConfigValue($object, $varName); 175 $this->jsLengths[$varName] = strlen($js); 176 } 177 $this->configValues[$varName]->incrementUseCount(); 178 179 return $this->configValues[$varName]; 180 } 181 182 /** 183 * Record the content of given config object 184 * 185 * @param array|Dictionary $object Original object 186 * @return array|Dictionary Modified object containing ConfigValue instances 187 */ 188 protected function recordObjectContent($object) 189 { 190 foreach ($object as $k => $v) 191 { 192 if ($this->canDeduplicate($v) && !$this->shouldPreserve($v)) 193 { 194 $object[$k] = $this->recordObject($v); 195 } 196 } 197 198 return $object; 199 } 200 201 /** 202 * Test whether given value should be preserved and not deduplicated 203 * 204 * @param mixed $value 205 * @return bool 206 */ 207 protected function shouldPreserve($value) 208 { 209 // Simple variables should be kept as-is 210 return ($value instanceof Code && preg_match('(^\\w+$)', $value)); 211 } 212 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |