1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/freshrss_ynh.git synced 2024-09-03 18:36:33 +02:00
freshrss_ynh/sources/lib/Minz/Configuration.php

222 lines
6 KiB
PHP
Raw Normal View History

2014-07-23 15:52:50 +02:00
<?php
/**
2015-02-08 18:55:48 +01:00
* Manage configuration for the application.
2014-07-23 15:52:50 +02:00
*/
class Minz_Configuration {
/**
2015-02-08 18:55:48 +01:00
* The list of configurations.
2014-07-23 15:52:50 +02:00
*/
2015-02-08 18:55:48 +01:00
private static $config_list = array();
2014-07-23 15:52:50 +02:00
/**
2015-02-08 18:55:48 +01:00
* Add a new configuration to the list of 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
2014-07-23 15:52:50 +02:00
*/
2015-02-08 18:55:48 +01:00
public static function register($namespace, $config_filename, $default_filename = null,
$configuration_setter = null) {
self::$config_list[$namespace] = new Minz_Configuration(
$namespace, $config_filename, $default_filename, $configuration_setter
);
}
2014-07-23 15:52:50 +02:00
/**
2015-02-08 18:55:48 +01:00
* Parse a file and return its data.
*
* If the file does not contain a valid PHP code returning an array, an
* empty array is returned anyway.
*
* @param $filename the name of the file to parse.
* @return an array of values
* @throws Minz_FileNotExistException if the file does not exist.
2014-07-23 15:52:50 +02:00
*/
2015-02-08 18:55:48 +01:00
public static function load($filename) {
if (!file_exists($filename)) {
throw new Minz_FileNotExistException($filename);
}
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
$data = @include($filename);
if (is_array($data)) {
return $data;
} else {
return array();
}
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
/**
* Return the configuration related to a given namespace.
*
* @param $namespace the name of the configuration to get.
* @return a Minz_Configuration object
* @throws Minz_ConfigurationNamespaceException if the namespace does not exist.
*/
public static function get($namespace) {
if (!isset(self::$config_list[$namespace])) {
throw new Minz_ConfigurationNamespaceException(
$namespace . ' namespace does not exist'
);
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
return self::$config_list[$namespace];
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
/**
* The namespace of the current configuration.
*/
private $namespace = '';
/**
* The filename for the current configuration.
*/
private $config_filename = '';
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
/**
* The filename for the current default values, null by default.
*/
private $default_filename = null;
/**
* The configuration values, an empty array by default.
*/
private $data = array();
/**
* The default values, an empty array by default.
*/
private $data_default = array();
/**
* An object which help to set good values in configuration.
*/
private $configuration_setter = null;
public function removeExtension($ext_name) {
self::$extensions_enabled = array_diff(
self::$extensions_enabled,
array($ext_name)
);
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
public function addExtension($ext_name) {
$found = array_search($ext_name, self::$extensions_enabled) !== false;
if (!$found) {
self::$extensions_enabled[] = $ext_name;
}
2014-07-23 15:52:50 +02:00
}
/**
2015-02-08 18:55:48 +01:00
* 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
2014-07-23 15:52:50 +02:00
*/
2015-02-08 18:55:48 +01:00
private function __construct($namespace, $config_filename, $default_filename = null,
$configuration_setter = null) {
$this->namespace = $namespace;
$this->config_filename = $config_filename;
2014-07-23 15:52:50 +02:00
try {
2015-02-08 18:55:48 +01:00
$this->data = self::load($this->config_filename);
2014-07-23 15:52:50 +02:00
} catch (Minz_FileNotExistException $e) {
2015-02-08 18:55:48 +01:00
if (is_null($default_filename)) {
throw $e;
}
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
$this->default_filename = $default_filename;
if (!is_null($this->default_filename)) {
$this->data_default = self::load($this->default_filename);
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
$this->_configurationSetter($configuration_setter);
2014-07-23 15:52:50 +02:00
}
/**
2015-02-08 18:55:48 +01:00
* Set a configuration setter for the current configuration.
* @param $configuration_setter the setter to call when modifying data. It
* must implement an handle($key, $value) method.
2014-07-23 15:52:50 +02:00
*/
2015-02-08 18:55:48 +01:00
public function _configurationSetter($configuration_setter) {
if (is_callable(array($configuration_setter, 'handle'))) {
$this->configuration_setter = $configuration_setter;
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
}
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
/**
* Return the value of the given param.
*
* @param $key the name of the param.
* @param $default default value to return if key does not exist.
* @return the value corresponding to the key.
* @throws Minz_ConfigurationParamException if the param does not exist
*/
public function param($key, $default = null) {
if (isset($this->data[$key])) {
return $this->data[$key];
} elseif (!is_null($default)) {
return $default;
} elseif (isset($this->data_default[$key])) {
return $this->data_default[$key];
} else {
Minz_Log::warning($key . ' does not exist in configuration');
return null;
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
}
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
/**
* A wrapper for param().
*/
public function __get($key) {
return $this->param($key);
}
/**
* Set or remove a param.
*
* @param $key the param name to set.
* @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) && $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;
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
}
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
/**
* A wrapper for _param().
*/
public function __set($key, $value) {
$this->_param($key, $value);
}
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
/**
* Save the current configuration in the configuration file.
*/
public function save() {
$back_filename = $this->config_filename . '.bak.php';
@rename($this->config_filename, $back_filename);
2014-07-23 15:52:50 +02:00
2015-02-08 18:55:48 +01:00
if (file_put_contents($this->config_filename,
"<?php\nreturn " . var_export($this->data, true) . ';',
LOCK_EX) === false) {
return false;
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
// Clear PHP 5.5+ cache for include
if (function_exists('opcache_invalidate')) {
opcache_invalidate($this->config_filename);
2014-07-23 15:52:50 +02:00
}
2015-02-08 18:55:48 +01:00
return true;
2014-07-23 15:52:50 +02:00
}
}