. * * The copright holder may be reached by emailing john@johnkleijn.nl */ require_once 'Phreamp/Loader.php'; require_once 'Phreamp/Config/Loader/Interface.php'; /** * Config class * */ class Phreamp_Config { /** * Static instance * * @var Phreamp_Config */ private static $_default; /** * Loader Strategy * * @var Phreamp_Config_Loader_Interface */ private $_loader = array(); /** * Configuration data * * @var array */ private $_data = array(); /** * Get default Instance * * @return Phreamp_Config */ public static function getDefault() { return self::$_default; } /** * Set the default instance * * @param Phreamp_Config $config */ public static function setDefault(Phreamp_Config $config) { self::$_default = $config; } /** * Constructor * */ public function __construct(array $data = array()) { $this->setData($data); } /** * Set the configuration data * * @param array $data */ public function setData(array $data) { $this->_data = $data; } /** * Return the internal array * * @return array */ public function getData() { return $this->_data; } /** * Return a value * * @return array|string */ public function get($key, $section = null, $subsection = null) { if($subsection !== null) { return @$this->_data[$section][$subsection][$key]; } if($section !== null) { return @$this->_data[$section][$key]; } else { return @$this->_data[$key]; } } /** * Set the loader object * * @param Phreamp_Config_Loader_Interface $loader */ public function setLoader(Phreamp_Config_Loader_Interface $loader) { $this->_loader = $loader; } /** * Get the loader * * @return Phreamp_Config_Loader_Interface */ public function getLoader() { return $this->_loader; } /** * Cloning not allowed * */ private function __clone() {} /** * Create a new instance and load a file * * @param string $filePath * @return Phreamp_Config */ public static function loadFile($filePath) { $self = new self(); $self->_loadFile($filePath); return $self; } /** * Load a file * * @param string $filePath */ private function _loadFile($filePath) { if(!$this->getLoader()) { $this->setLoader($this->fileLoaderFactory($filePath)); } $this->setData($this->getLoader()->load($filePath)); } /** * Get a file loader object based on filename * * @param string $filePath * @return Phreamp_Config_Loader_Interface */ public function fileLoaderFactory($filePath) { $segs = explode('.', $filePath); $class = 'Phreamp_Config_Loader_' . ucfirst(end($segs)); Phreamp_Loader::load($class); return new $class($filePath); } }