Ver código fonte

Add support of configuration_setter

A configuration setter must implement only one method: `handle($key, $value)`.
Before setting a value in configuration, the setter will be called with this
method to check its validity.
If a setter has been assigned to a configuration object, it will be called for
each of its data so be careful to always return a value (or null if you want
to delete the key).

See https://github.com/FreshRSS/FreshRSS/issues/730
Marien Fressinaud 11 anos atrás
pai
commit
91e2d4936d
1 arquivos alterados com 19 adições e 4 exclusões
  1. 19 4
      lib/Minz/Configuration.php

+ 19 - 4
lib/Minz/Configuration.php

@@ -15,9 +15,11 @@ class Minz_Configuration {
 	 * @param $namespace the name of the current configuration
 	 * @param $config_filename the filename of the configuration
 	 * @param $default_filename a filename containing default values for the configuration
+	 * @param $configuration_setter an optional helper to set values in configuration
 	 * @throws Minz_ConfigurationNamespaceException if the namespace already exists.
 	 */
-	public static function register($namespace, $config_filename, $default_filename = null) {
+	public static function register($namespace, $config_filename, $default_filename = null,
+	                                $configuration_setter = null) {
 		if (isset(self::$config_list[$namespace])) {
 			throw new Minz_ConfigurationNamespaceException(
 				$namespace . ' namespace already exists'
@@ -25,7 +27,7 @@ class Minz_Configuration {
 		}
 
 		self::$config_list[$namespace] = new Minz_Configuration(
-			$namespace, $config_filename, $default_filename
+			$namespace, $config_filename, $default_filename, $configuration_setter
 		);
 	}
 
@@ -94,14 +96,21 @@ class Minz_Configuration {
 	 */
 	private $data_default = array();
 
+	/**
+	 * An object which help to set good values in configuration.
+	 */
+	private $configuration_setter = null;
+
 	/**
 	 * Create a new Minz_Configuration object.
 	 * 
 	 * @param $namespace the name of the current configuration.
 	 * @param $config_filename the file containing configuration values.
 	 * @param $default_filename the file containing default values, null by default.
+	 * @param $configuration_setter an optional helper to set values in configuration
 	 */
-	private function __construct($namespace, $config_filename, $default_filename = null) {
+	private function __construct($namespace, $config_filename, $default_filename = null,
+	                             $configuration_setter = null) {
 		$this->namespace = $namespace;
 		$this->config_filename = $config_filename;
 
@@ -117,6 +126,8 @@ class Minz_Configuration {
 		if (!is_null($this->default_filename)) {
 			$this->data_default = self::load($this->default_filename);
 		}
+
+		$this->configuration_setter = $configuration_setter;
 	}
 
 	/**
@@ -154,9 +165,13 @@ 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)) {
 			unset($this->data[$key]);
-		} else {
+		} elseif (!is_null($value)) {
 			$this->data[$key] = $value;
 		}
 	}