diff options
| author | 2015-01-07 15:57:27 +0100 | |
|---|---|---|
| committer | 2015-01-07 15:57:27 +0100 | |
| commit | 91e2d4936d71a72d0aefe9736879099283519239 (patch) | |
| tree | 22ef286a7b8bcb6044fc769701c55d29d3c07fd3 | |
| parent | 9c8a80e57f1e2eb60916eae2e2cbed86d991c750 (diff) | |
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
| -rw-r--r-- | lib/Minz/Configuration.php | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php index 06a7b43b0..5dbd55876 100644 --- a/lib/Minz/Configuration.php +++ b/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 ); } @@ -95,13 +97,20 @@ 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; } } |
