moved Configuraiton class to separate library
parent
43d3136208
commit
76026b747c
@ -1,73 +0,0 @@
|
||||
<?php
|
||||
namespace BSR\Webservice;
|
||||
|
||||
class Configuration {
|
||||
private static $instance = null;
|
||||
|
||||
/**
|
||||
* ! WARNING !
|
||||
*
|
||||
* Those are default values, if you need to change them for a particular host,
|
||||
* please just create a file named 'configuration.local.php' in the same directory
|
||||
* and redefine the values there !
|
||||
*
|
||||
* This file must contain an array named $configuration containing the keys you
|
||||
* want to redefine. Then, those values will be used to replace those in the
|
||||
* array defined just below.
|
||||
*
|
||||
* @var array configuration values
|
||||
*/
|
||||
private $values = array();
|
||||
|
||||
private $custom_config_filename = 'configuration.local.php';
|
||||
|
||||
private function __construct() {
|
||||
// by default, set the session save path to the default value;
|
||||
$this->values['session']['save_path'] = session_save_path();
|
||||
|
||||
if(!file_exists($this->custom_config_filename)) {
|
||||
throw new \RuntimeException('No configuration.local.php file was found. Create it with the proper config.');
|
||||
}
|
||||
|
||||
$configuration = include_once realpath(dirname(__FILE__) . '/../config/') . $this->custom_config_filename;
|
||||
|
||||
if(!is_array($configuration)) {
|
||||
throw new \RuntimeException("You custom configuration in '{$this->custom_config_filename}' must be in a variable named '\$configuration' and be an array.");
|
||||
}
|
||||
|
||||
$this->values = array_replace_recursive($this->values, $configuration);
|
||||
}
|
||||
|
||||
private function dotNotationAccess($data, $key, $default=null)
|
||||
{
|
||||
$keys = explode('.', $key);
|
||||
foreach ($keys as $k) {
|
||||
if (!is_array($data)) {
|
||||
throw new \Exception("Try to access non-array as array, key '$key''");
|
||||
}
|
||||
if (!isset($data[$k])) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$data = $data[$k];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function value($name, $default) {
|
||||
return $this->dotNotationAccess($this->values, $name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param mixed $default the default value for your configuration option
|
||||
* @return mixed return the configuration value if the key is find, the default otherwise
|
||||
*/
|
||||
public static function get($name, $default = null) {
|
||||
if(is_null(self::$instance)) {
|
||||
self::$instance = new Configuration();
|
||||
}
|
||||
|
||||
return self::$instance->value($name, $default);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue