Index: trunk/extensions/Maps/includes/Maps_Settings.php |
— | — | @@ -1,23 +1,226 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | /** |
5 | | - * Static class for hooks handled by the Live Translate extension. |
| 5 | + * Static class for interaction with the settings of the Maps extension. |
6 | 6 | * |
7 | | - * @since 0.1 |
| 7 | + * @since 1.1 |
8 | 8 | * |
9 | | - * @file LiveTranslate.hooks.php |
10 | | - * @ingroup LiveTranslate |
| 9 | + * @file Maps_Settings.php |
| 10 | + * @ingroup Maps |
11 | 11 | * |
12 | | - * @author Jeroen De Dauw |
| 12 | + * @licence GNU GPL v3 |
| 13 | + * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
13 | 14 | */ |
14 | 15 | final class MapsSettings extends ExtensionSettings { |
15 | 16 | |
| 17 | + protected static function initIfNeeded() { |
| 18 | + parent::initIfNeeded(); |
| 19 | + |
| 20 | + self::$settings['php-bc'] = self::getPhpCompatSettings(); |
| 21 | + } |
16 | 22 | |
| 23 | + /** |
| 24 | + * Returns a name => value array with the default settings. |
| 25 | + * |
| 26 | + * @since 1.1 |
| 27 | + * |
| 28 | + * @return array |
| 29 | + */ |
| 30 | + protected static function getDefaultSettings() { |
| 31 | + static $defaultSettings = false; |
| 32 | + |
| 33 | + if ( $defaultSettings === false ) { |
| 34 | + $defaultSettings = include 'Maps.settings.php'; |
| 35 | + } |
| 36 | + |
| 37 | + return $defaultSettings; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Returns a name => value array with the default settings |
| 42 | + * specified using global PHP variables. |
| 43 | + * |
| 44 | + * @since 1.1 |
| 45 | + * |
| 46 | + * @return array |
| 47 | + */ |
| 48 | + protected static function getPhpSettings() { |
| 49 | + return $GLOBALS['egMapsSettings']; |
| 50 | + } |
17 | 51 | |
| 52 | + /** |
| 53 | + * Returns a name => value array with the default settings |
| 54 | + * specified using global PHP variables that have been deprecated. |
| 55 | + * |
| 56 | + * @since 1.1 |
| 57 | + * |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + protected static function getPhpCompatSettings() { |
| 61 | + $mappings = array( |
| 62 | + 'egMapsFoobar' => 'foobar' // TODO |
| 63 | + ); |
| 64 | + |
| 65 | + $settings = array(); |
| 66 | + |
| 67 | + foreach ( $mappings as $oldName => $newName ) { |
| 68 | + if ( array_key_exists( $oldName, $GLOBALS ) ) { |
| 69 | + $settings[$newName] = $GLOBALS[$oldName]; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return $settings; |
| 74 | + } |
| 75 | + |
18 | 76 | } |
19 | 77 | |
20 | 78 | abstract class ExtensionSettings { |
21 | 79 | |
| 80 | + protected static $settings = false; |
22 | 81 | |
| 82 | + protected static $mergedCaches = array(); |
23 | 83 | |
| 84 | + /** |
| 85 | + * Returns a name => value array with the default settings. |
| 86 | + * |
| 87 | + * @since 1.1 |
| 88 | + * |
| 89 | + * @return array |
| 90 | + */ |
| 91 | + protected abstract static function getDefaultSettings(); |
| 92 | + |
| 93 | + protected static function initIfNeeded() { |
| 94 | + if ( self::$settings === false ) { |
| 95 | + self::$settings = array( |
| 96 | + 'default' => self::getDefaultSettings(), |
| 97 | + 'php' => self::getPhpSettings() |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + protected static function getMergedSettings( array $groups, $cache = true ) { |
| 103 | + $names = implode( '|', $groups ); |
| 104 | + |
| 105 | + if ( array_key_exists( $names, self::$mergedCaches ) ) { |
| 106 | + return self::$mergedCaches[$names]; |
| 107 | + } |
| 108 | + else { |
| 109 | + $settings = array(); |
| 110 | + |
| 111 | + foreach ( $groups as $group ) { |
| 112 | + if ( array_key_exists( $group, self::$settings ) ) { |
| 113 | + $settings = array_merge_recursive( $settings, self::$settings[$group] ); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + if ( $cache ) { |
| 118 | + self::$mergedCaches[$names] = $settings; |
| 119 | + } |
| 120 | + |
| 121 | + return $settings; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Returns a name => value array with the default settings |
| 127 | + * specified using global PHP variables. |
| 128 | + * |
| 129 | + * @since 1.1 |
| 130 | + * |
| 131 | + * @return array |
| 132 | + */ |
| 133 | + protected static function getPhpSettings() { |
| 134 | + return array(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Returns all settings for a group. |
| 139 | + * |
| 140 | + * @since 1.1 |
| 141 | + * |
| 142 | + * @param array|boolean $groups True to use all overrides, false for none, array for custom set or order. |
| 143 | + * @param boolean $cache Cache the merging of groups or not? |
| 144 | + * |
| 145 | + * @return array |
| 146 | + */ |
| 147 | + public static function getSettings( $groups = true, $cache = true ) { |
| 148 | + self::initIfNeeded(); |
| 149 | + |
| 150 | + if ( $groups === false ) { |
| 151 | + return self::getDefaultSettings(); |
| 152 | + } |
| 153 | + else { |
| 154 | + if ( $groups === true ) { |
| 155 | + $groups = array_keys( self::$settings ); |
| 156 | + } |
| 157 | + return self::getMergedSettings( $groups, $cache ); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns the value of a single setting. |
| 163 | + * |
| 164 | + * @since 1.1 |
| 165 | + * |
| 166 | + * @param string $settingName |
| 167 | + * @param array|boolean $groups |
| 168 | + * @param boolean $cache Cache the merging of groups or not? |
| 169 | + * |
| 170 | + * @return mixed |
| 171 | + */ |
| 172 | + public static function get( $settingName, $groups = true, $cache = true ) { |
| 173 | + $settings = self::getSettings( $groups, $cache ); |
| 174 | + |
| 175 | + if ( !array_key_exists( $settingName, $settings ) ) { |
| 176 | + throw new MWException(); // TODO |
| 177 | + } |
| 178 | + |
| 179 | + return $settings[$settingName]; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Returns if a single setting exists or not. |
| 184 | + * |
| 185 | + * @since 1.1 |
| 186 | + * |
| 187 | + * @param string $settingName |
| 188 | + * @param array|boolean $groups |
| 189 | + * @param boolean $cache Cache the merging of groups or not? |
| 190 | + * |
| 191 | + * @return boolean |
| 192 | + */ |
| 193 | + public static function has( $settingName, $groups = true, $cache = true ) { |
| 194 | + $settings = self::getSettings( $groups, $cache ); |
| 195 | + return array_key_exists( $settingName, $settings ); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Set a sigle setting in the specified group. |
| 200 | + * |
| 201 | + * @since 1.1 |
| 202 | + * |
| 203 | + * @param string $settingName |
| 204 | + * @param mixed $settingValue |
| 205 | + * @param string $groupName |
| 206 | + * @param boolean $invalidateCache |
| 207 | + * |
| 208 | + * @return boolean |
| 209 | + */ |
| 210 | + public static function set( $settingName, $settingValue, $groupName, $invalidateCache = true ) { |
| 211 | + if ( !array_key_exists( $groupName, self::$settings ) ) { |
| 212 | + self::$settings[$groupName] = array(); |
| 213 | + } |
| 214 | + elseif ( $invalidateCache |
| 215 | + && ( !array_key_exists( $settingName, self::$settings[$groupName] ) |
| 216 | + || self::$settings[$groupName][$settingName] !== $settingValue ) ) { |
| 217 | + foreach ( array_keys( self::$mergedCaches ) as $cacheName ) { |
| 218 | + if ( in_array( $groupName, explode( '|', $cacheName ) ) ) { |
| 219 | + unset( self::$mergedCaches[$cacheName] ); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + self::$settings[$groupName][$settingName] = $settingValue; |
| 225 | + } |
| 226 | + |
24 | 227 | } |