Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ConfigHelper.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Configurator\Helpers;
009
010 use RuntimeException;
011 use s9e\TextFormatter\Configurator\ConfigProvider;
012 use s9e\TextFormatter\Configurator\FilterableConfigValue;
013 use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
014 use Traversable;
015
016 abstract class ConfigHelper
017 {
018 /**
019 * Recursively filter a config array to replace variants with the desired value
020 *
021 * @param array $config Config array
022 * @param string $target Target parser
023 * @return array Filtered config
024 */
025 public static function filterConfig(array $config, $target = 'PHP')
026 {
027 $filteredConfig = [];
028 foreach ($config as $name => $value)
029 {
030 if ($value instanceof FilterableConfigValue)
031 {
032 $value = $value->filterConfig($target);
033 if (!isset($value))
034 {
035 continue;
036 }
037 }
038 if (is_array($value))
039 {
040 $value = self::filterConfig($value, $target);
041 }
042 $filteredConfig[$name] = $value;
043 }
044
045 return $filteredConfig;
046 }
047
048 /**
049 * Generate a quickMatch string from a list of strings
050 *
051 * This is basically a LCS implementation, tuned for small strings and fast failure
052 *
053 * @param array $strings Array of strings
054 * @return mixed quickMatch string, or FALSE if none could be generated
055 */
056 public static function generateQuickMatchFromList(array $strings)
057 {
058 foreach ($strings as $string)
059 {
060 $stringLen = strlen($string);
061 $substrings = [];
062
063 for ($len = $stringLen; $len; --$len)
064 {
065 $pos = $stringLen - $len;
066
067 do
068 {
069 $substrings[substr($string, $pos, $len)] = 1;
070 }
071 while (--$pos >= 0);
072 }
073
074 if (isset($goodStrings))
075 {
076 $goodStrings = array_intersect_key($goodStrings, $substrings);
077
078 if (empty($goodStrings))
079 {
080 break;
081 }
082 }
083 else
084 {
085 $goodStrings = $substrings;
086 }
087 }
088
089 if (empty($goodStrings))
090 {
091 return false;
092 }
093
094 // The strings are stored by length descending, so we return the first in the list
095 return strval(key($goodStrings));
096 }
097
098 /**
099 * Optimize the size of a deep array by deduplicating identical structures
100 *
101 * This method is meant to be used on a config array which is only read and never modified
102 *
103 * @param array &$config
104 * @param array &$cache
105 * @return array
106 */
107 public static function optimizeArray(array &$config, array &$cache = [])
108 {
109 foreach ($config as $k => &$v)
110 {
111 if (!is_array($v))
112 {
113 continue;
114 }
115
116 // Dig deeper into this array
117 self::optimizeArray($v, $cache);
118
119 // Look for a matching structure
120 $cacheKey = serialize($v);
121 if (!isset($cache[$cacheKey]))
122 {
123 // Record this value in the cache
124 $cache[$cacheKey] = $v;
125 }
126
127 // Replace the entry in $config with a reference to the cached value
128 $config[$k] =& $cache[$cacheKey];
129 }
130 unset($v);
131 }
132
133 /**
134 * Convert a structure to a (possibly multidimensional) array
135 *
136 * @param mixed $value
137 * @param bool $keepEmpty Whether to keep empty arrays instead of removing them
138 * @param bool $keepNull Whether to keep NULL values instead of removing them
139 * @return array
140 */
141 public static function toArray($value, $keepEmpty = false, $keepNull = false)
142 {
143 $array = [];
144
145 foreach ($value as $k => $v)
146 {
147 $isDictionary = $v instanceof Dictionary;
148 if ($v instanceof ConfigProvider)
149 {
150 $v = $v->asConfig();
151 }
152 elseif ($v instanceof Traversable || is_array($v))
153 {
154 $v = self::toArray($v, $keepEmpty, $keepNull);
155 }
156 elseif (is_scalar($v) || is_null($v))
157 {
158 // Do nothing
159 }
160 else
161 {
162 $type = (is_object($v))
163 ? 'an instance of ' . get_class($v)
164 : 'a ' . gettype($v);
165
166 throw new RuntimeException('Cannot convert ' . $type . ' to array');
167 }
168
169 if (!isset($v) && !$keepNull)
170 {
171 // We don't record NULL values
172 continue;
173 }
174
175 if (!$keepEmpty && $v === [])
176 {
177 // We don't record empty structures
178 continue;
179 }
180
181 $array[$k] = ($isDictionary) ? new Dictionary($v) : $v;
182 }
183
184 return $array;
185 }
186 }