changed package name to Utils to add the logger into it
This commit is contained in:
204
src/Utils/Configuration/Configuration.php
Normal file
204
src/Utils/Configuration/Configuration.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?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)
|
||||
{
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \RuntimeException("The file path $filePath, does not exist");
|
||||
}
|
||||
self::$customConfigFilePath = $filePath;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
public static function getConfigFilePath()
|
||||
{
|
||||
if (null === self::$customConfigFilePath) {
|
||||
self::$customConfigFilePath = realpath(dirname(__FILE__) . '/../../../../../../config/configuration.local.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
|
||||
*/
|
||||
public 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user