Feature #8464 ยป 8464-write-settings.diff

Karsten Dambekalns, 2010-10-14 16:03

View differences:

a/Classes/Configuration/ConfigurationManager.php
173 173
	 *
174 174
	 * @param string $configurationType The kind of configuration to fetch - must be one of the CONFIGURATION_TYPE_* constants
175 175
	 * @param array $configuration The new configuration
176
	 * @param string $packageKey The package key the configuration relates to, needed for CONFIGURATION_TYPE_SETTINGS
176 177
	 * @return void
177 178
	 * @throws \F3\FLOW3\Configuration\Exception\InvalidConfigurationTypeException on invalid configuration types
178 179
	 * @author Robert Lemke <robert@typo3.org>
180
	 * @author Karsten Dambekalns <karsten@dambekalns.de>
179 181
	 */
180
	public function setConfiguration($configurationType, array $configuration) {
182
	public function setConfiguration($configurationType, array $configuration, $packageKey = NULL) {
181 183
		switch ($configurationType) {
182 184
			case self::CONFIGURATION_TYPE_PACKAGESTATES :
183 185
				$this->configurations[$configurationType] = $configuration;
184 186
				$this->cacheNeedsUpdate = TRUE;
185 187
			break;
188
			case self::CONFIGURATION_TYPE_SETTINGS :
189
				$this->configurations[$configurationType][$packageKey] = $configuration;
190
				$this->cacheNeedsUpdate = TRUE;
191
			break;
186 192
			default :
187 193
				throw new \F3\FLOW3\Configuration\Exception\InvalidConfigurationTypeException('Invalid configuration type "' . $configurationType . '"', 1251127738);
188 194
		}
......
195 201
	 * @param string $configurationType The kind of configuration to save - must be one of the supported CONFIGURATION_TYPE_* constants
196 202
	 * @return void
197 203
	 * @author Robert Lemke <robert@typo3.org>
204
	 * @author Karsten Dambekalns <karsten@dambekalns.de>
198 205
	 */
199 206
	public function saveConfiguration($configurationType) {
200 207
		switch ($configurationType) {
201 208
			case self::CONFIGURATION_TYPE_PACKAGESTATES :
202 209
				$this->configurationSource->save(FLOW3_PATH_CONFIGURATION . $configurationType, $this->configurations[$configurationType]);
203 210
			break;
211
			case self::CONFIGURATION_TYPE_SETTINGS :
212
				$this->configurationSource->save(FLOW3_PATH_CONFIGURATION . $configurationType,
213
						$this->pickChangedSettings($this->configurations[$configurationType])
214
					);
215
			break;
204 216
			default :
205 217
				throw new \F3\FLOW3\Configuration\Exception\InvalidConfigurationTypeException('Configuration type "' . $configurationType . '" does not support saving.', 1251127425);
206 218
		}
207 219
	}
208 220

  
209 221
	/**
222
	 *
223
	 * @param array $settings
224
	 * @return array
225
	 * @author Karsten Dambekalns <karsten@dambekalns.de>
226
	 */
227
	protected function pickChangedSettings(array $settings) {
228
		$changedSettings = array();
229
		$cleanSettings = $this->configurationSource->load(FLOW3_PATH_FLOW3 . 'Configuration/' . self::CONFIGURATION_TYPE_SETTINGS);
230
		$cleanSettings = \F3\FLOW3\Utility\Arrays::arrayMergeRecursiveOverrule($cleanSettings, $this->configurationSource->load(FLOW3_PATH_FLOW3 . 'Configuration/' . $this->context . '/' . self::CONFIGURATION_TYPE_SETTINGS));
231
		foreach ($this->packages as $package) {
232
			$cleanSettings = \F3\FLOW3\Utility\Arrays::arrayMergeRecursiveOverrule($cleanSettings, $this->configurationSource->load($package->getConfigurationPath() . self::CONFIGURATION_TYPE_SETTINGS));
233
		}
234

  
235
		return \F3\FLOW3\Utility\Arrays::recursiveDiff($settings, $cleanSettings);
236
	}
237

  
238
	/**
210 239
	 * Shuts down the configuration manager.
211 240
	 * This method writes the current configuration into a cache file if FLOW3 was configured to do so.
212 241
	 *
a/Classes/Utility/Arrays.php
210 210
		return ksort($array, $sortFlags);
211 211
	}
212 212

  
213

  
214
	static public function recursiveDiff(array $newArray, array $oldArray) {
215
		$difference = array();
216

  
217
		foreach ($newArray as $newKey => $newValue) {
218
			if (array_key_exists($newKey, $oldArray)) {
219
				if (is_array($newValue)) {
220
					$recursiveDifference = self::recursiveDiff($newValue, $oldArray[$newKey]);
221
					if (count($recursiveDifference)) {
222
						$difference[$newKey] = $recursiveDifference;
223
					}
224
				} else {
225
					if ($newValue !== $oldArray[$newKey]) {
226
						$difference[$newKey] = $newValue;
227
					}
228
				}
229
			} elseif (is_array($newValue) && count($newValue)) {
230
				$difference[$newKey] = $newValue;
231
			} else {
232
				$difference[$newKey] = $newValue;
233
			}
234
		}
235

  
236
	    return $difference;
237
	}
238

  
213 239
	/**
214 240
	 * Recursively convert an object to an associative array.
215 241
	 *
a/Tests/Unit/Configuration/ConfigurationManagerTest.php
182 182
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_PACKAGE,
183 183
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES,
184 184
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_POLICY,
185
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
186 185
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SIGNALSSLOTS
187 186
		);
188 187

  
......
225 224
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_PACKAGE,
226 225
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES,
227 226
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_POLICY,
228
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SETTINGS,
229 227
			\F3\FLOW3\Configuration\ConfigurationManager::CONFIGURATION_TYPE_SIGNALSSLOTS
230 228
		);
231 229

  
    (1-1/1)