Last active
August 29, 2015 13:56
-
-
Save TemporaryJam/9234612 to your computer and use it in GitHub Desktop.
PHP config class supporting dot notation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Config | |
{ | |
/** | |
*@var array | |
*/ | |
private static $config = null; | |
/** | |
* Returns a config setting. | |
* @param string $name | |
* @return mixed | |
*/ | |
public static function get($name) | |
{ | |
if (is_null(self::$config)) { | |
$base_config = require_once('config_base.php'); | |
$site_config = include_once('config.php'); | |
$config = array_merge($base_config, $site_config); | |
self::$config = $config; | |
} | |
if (strpos($name, ".")) { | |
$parts = explode(".", $name); | |
$val = self::$config; | |
foreach ($parts as $part){ | |
if (isset($val[$part])) { | |
$val = $val[$part]; | |
} else { | |
throw new \Exception("Unknown setting '" . $name . "' requested."); | |
} | |
} | |
return $val; | |
} else { | |
if (isset(self::$config[$name])) { | |
return self::$config[$name]; | |
} else { | |
throw new \Exception("Unknown setting '" . $name . "' requested."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment