You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
5.4 KiB
PHP
220 lines
5.4 KiB
PHP
<?php
|
|
namespace Bsr\Utils\Configuration;
|
|
|
|
class Configuration {
|
|
|
|
/**
|
|
* @var Bsr\Config\Configuration
|
|
*/
|
|
private static $instance = null;
|
|
|
|
/**
|
|
* @var
|
|
*/
|
|
private static $customConfigFilePath;
|
|
|
|
/**
|
|
* ! 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 function __construct()
|
|
{
|
|
if (!isset($this->values['session'])) {
|
|
$this->values['session'] = array();
|
|
}
|
|
|
|
if (!isset($this->values['session']['save_path'])) {
|
|
$this->values['session']['save_path'] = session_save_path();
|
|
}
|
|
|
|
if (file_exists(self::getConfigFilePath())) {
|
|
$this->loadConfigFromFile();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The path to the file including file name
|
|
* @param string $filePath
|
|
* @return void
|
|
*/
|
|
public static function setConfigFilePath($filePath, $clearPrevious = false)
|
|
{
|
|
if (!file_exists($filePath)) {
|
|
throw new \RuntimeException("The file path $filePath, does not exist");
|
|
}
|
|
self::$customConfigFilePath = $filePath;
|
|
$instance = self::getInstance();
|
|
if ($clearPrevious) {
|
|
$instance->clear();
|
|
}
|
|
self::getInstance()->loadConfigFromFile();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return String
|
|
*/
|
|
public static function getConfigFilePath()
|
|
{
|
|
if (null === self::$customConfigFilePath) {
|
|
$envfile = getenv('APPLICATION_ENV') === 'development' ? 'local' : 'global';
|
|
self::$customConfigFilePath = realpath(dirname(__FILE__) . "/../../../../../../config/configuration.$envfile.php");
|
|
}
|
|
return self::$customConfigFilePath;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
* @throws \RuntimeException
|
|
*/
|
|
public function getConfigFromFile()
|
|
{
|
|
if (!file_exists(self::getConfigFilePath())) {
|
|
throw new \RuntimeException("The file : {self::getConfigFilePath()} does not exist");
|
|
}
|
|
return include self::getConfigFilePath();
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @return void
|
|
* @throws \RuntimeException
|
|
*/
|
|
private function loadConfigFromFile()
|
|
{
|
|
$this->setCustomConfig($this->getConfigFromFile());
|
|
}
|
|
|
|
/**
|
|
* @param array $config
|
|
* @return void
|
|
*/
|
|
public function setCustomConfig(array $config)
|
|
{
|
|
$this->values = array_replace_recursive($this->values, $config);
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getConfig()
|
|
{
|
|
return $this->values;
|
|
}
|
|
|
|
/**
|
|
* Checks whether the nested key exists
|
|
*
|
|
* @param array $data
|
|
* @param array $keys
|
|
* @return boolean
|
|
*/
|
|
public static function existsKeys(array $data, array $keys)
|
|
{
|
|
foreach ($keys as $k) {
|
|
if (!isset($data[$k])) {
|
|
return false;
|
|
}
|
|
$data = $data[$k];
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param array $data
|
|
* @param array $keys
|
|
* @param any $default
|
|
* @return boolean
|
|
*/
|
|
public static function fetchRecursive(array $data, array $keys, $default = null)
|
|
{
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Return array of keys if dot notation string is passed
|
|
* @param string|array $keys
|
|
*/
|
|
public static function parseKeys($keys)
|
|
{
|
|
if (is_string($keys)) {
|
|
$keys = explode('.', $keys);
|
|
} else if (!is_array($keys)) {
|
|
throw new \RuntimeException('Unsupported $keys paramater type');
|
|
}
|
|
return $keys;
|
|
}
|
|
|
|
/**
|
|
* Allow clearing previously loaded config
|
|
*/
|
|
public function clear()
|
|
{
|
|
$this->values = array();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* @throws \Exception
|
|
*/
|
|
public function value($keys, $default)
|
|
{
|
|
return self::fetchRecursive($this->values, self::parseKeys($keys), $default);
|
|
}
|
|
|
|
/**
|
|
* @return boolean
|
|
*/
|
|
public static function has($keys)
|
|
{
|
|
return self::existsKeys(self::get(), self::parseKeys($keys));
|
|
}
|
|
|
|
/**
|
|
* @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 = null, $default = null)
|
|
{
|
|
if (null === $name) {
|
|
return self::getInstance()->getConfig();
|
|
}
|
|
return self::getInstance()->value($name, $default);
|
|
}
|
|
|
|
/**
|
|
* @return Bsr\Config\Configuration
|
|
*/
|
|
public static function getInstance()
|
|
{
|
|
if(is_null(self::$instance)) {
|
|
self::$instance = new Configuration();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
}
|