Răsfoiți Sursa

Change way to call configuration setter.

- Add a support($key) method which return if the given key is supported by
  the setter.
- Change handle signature by adding a $data param which must be passed by
  reference.

See https://github.com/FreshRSS/FreshRSS/issues/730
Marien Fressinaud 11 ani în urmă
părinte
comite
fb614ab80c
2 a modificat fișierele cu 30 adăugiri și 22 ștergeri
  1. 27 17
      app/Models/ConfigurationSetter.php
  2. 3 5
      lib/Minz/Configuration.php

+ 27 - 17
app/Models/ConfigurationSetter.php

@@ -1,37 +1,47 @@
 <?php
 
 class FreshRSS_ConfigurationSetter {
-	private $setters = array(
-		'language' => '_language',
-		'posts_per_page' => '_posts_per_page',
-		'view_mode' => '_view_mode',
-	);
+	/**
+	 * Return if the given key is supported by this setter.
+	 * @param $key the key to test.
+	 * @return true if the key is supported, false else.
+	 */
+	public function support($key) {
+		$name_setter = '_' . $key;
+		return is_callable(array($this, $name_setter));
+	}
 
-	public function handle($key, $value) {
-		if (isset($this->setters[$key])) {
-			$value = call_user_func(array($this, $this->setters[$key]), $value);
-		}
-		return $value;
+	/**
+	 * Set the given key in data with the current value.
+	 * @param $data an array containing the list of all configuration data.
+	 * @param $key the key to update.
+	 * @param $value the value to set.
+	 */
+	public function handle(&$data, $key, $value) {
+		$name_setter = '_' . $key;
+		call_user_func_array(array($this, $name_setter), array(&$data, $value));
 	}
 
-	private function _language($value) {
+	/**
+	 * The (long) list of setters.
+	 */
+	private function _language(&$data, $value) {
 		$languages = Minz_Translate::availableLanguages();
 		if (!isset($languages[$value])) {
 			$value = 'en';
 		}
-
-		return $value;
+		$data['language'] = $value;
 	}
 
-	private function _posts_per_page($value) {
+	private function _posts_per_page(&$data, $value) {
 		$value = intval($value);
-		return $value > 0 ? $value : 10;
+		$data['posts_per_page'] = $value > 0 ? $value : 10;
 	}
 
-	private function _view_mode($value) {
+	private function _view_mode(&$data, $value) {
 		if (!in_array($value, array('global', 'normal', 'reader'))) {
 			$value = 'normal';
 		}
-		return $value;
+		$data['view_mode'] =  $value;
 	}
 }

+ 3 - 5
lib/Minz/Configuration.php

@@ -176,11 +176,9 @@ class Minz_Configuration {
 	 * @param $value the value to set. If null, the key is removed from the configuration.
 	 */
 	public function _param($key, $value = null) {
-		if (!is_null($this->configuration_setter)) {
-			$value = $this->configuration_setter->handle($key, $value);
-		}
-
-		if (isset($this->data[$key]) && is_null($value)) {
+		if (!is_null($this->configuration_setter) && $this->configuration_setter->support($key)) {
+			$this->configuration_setter->handle($this->data, $key, $value);
+		} elseif (isset($this->data[$key]) && is_null($value)) {
 			unset($this->data[$key]);
 		} elseif (!is_null($value)) {
 			$this->data[$key] = $value;