first commit
commit
8183fba17c
@ -0,0 +1 @@
|
|||||||
|
vendor
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
{
|
||||||
|
"name" : "bsr/conf",
|
||||||
|
"description" : "Bsr configuration module",
|
||||||
|
"authors" : [
|
||||||
|
{
|
||||||
|
"name" : "Simon"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" : "Guillermo"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {"BSR\\Config\\" : "src/"}
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"pds/skeleton": "^1.0",
|
||||||
|
"phpunit/phpunit": "^6"
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,73 @@
|
|||||||
|
<?php
|
||||||
|
namespace BSR\Config;
|
||||||
|
|
||||||
|
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