first blood
This commit is contained in:
98
configuration.php
Normal file
98
configuration.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
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(
|
||||
'db' => array(
|
||||
'driver' => 'SQL Server Native Client 11.0',
|
||||
'server' => 'BSR2012\SQLEXPRESS,1433',
|
||||
'username' => 'alcoda',
|
||||
'password' => 'alcodaonly',
|
||||
'name' => 'NetBiblio3',
|
||||
),
|
||||
'solr' => array(
|
||||
'server' => '192.168.1.250',
|
||||
'port' => '8983',
|
||||
'username' => '',
|
||||
'password' => '',
|
||||
'result_count' => 10,
|
||||
),
|
||||
'log' => array(
|
||||
'file' => 'c:\\logs\\ws_ipad.txt',
|
||||
'active' => false,
|
||||
),
|
||||
'session' => array(
|
||||
'save_path' => ''
|
||||
),
|
||||
|
||||
'checkfile_url' => 'http://fichiers.bibliothequesonore.ch/checkfile.php?',
|
||||
|
||||
'netbiblio_worker_id' => 45,
|
||||
'www_employee_id' => 45,
|
||||
'www_library_id' => 2,
|
||||
);
|
||||
|
||||
private $custom_config = '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)) {
|
||||
require_once($this->custom_config);
|
||||
|
||||
if(! isset($configuration) || ! is_array($configuration)) {
|
||||
throw new RuntimeException("You custom configuration in '{$this->custom_config}' 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user