* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBCache'); /** * Instance creator * * Load XML config files * * @version 0.5.0 * @package WB * @subpackage base */ class WBConfig extends WBStdClass { /** * previously loaded config * @var array */ private static $_configBuffer = array(); /** * currently loaded configuration values * @var array */ private $_current = array(); /** * XML config string * @var WBConfig_Loader */ private $loader; /** * constructor * * start patI18n */ public function __construct() { WBClass::create('patI18n'); $this->setLoader(); } /** * Choose loader * * Tell config class how to load XML-strings of config * * @param string $loader */ public function setLoader($loader = 'File') { $this->loader = WBClass::create('WBConfig_Loader_'. $loader); } /** * Receive current config loader object * * @return WBConfig_Loader */ public function getLoader() { return $this->loader; } /** * receive config value by path * * * * @param string|array $path sort of x-path expression * @param mixed $default value to return if not found * @return string|array configuration value of selected path or "null" if not found */ public function get( $path = null, $default = null ) { if( empty( $path ) ) { return $this->_current; } if( !is_array( $path ) ) { $path = explode( '/', $path ); } // walk through path $config =& $this->_current; foreach( $path as $p ) { if (!isset($config[$p]) || !is_array($config)) { return $default; } $config =& $config[$p]; } return $config; } /** * load configuration * * Well, load it from file or cache or buffer * * @param string $file * @param bool $try try to load, don't throw an exception * @return bool true on success * @throws WBException_File */ public function load($file, $try = false) { // loaded before? $lang = patI18n::getLocale(); if (isset(self::$_configBuffer[$lang . '-' . $file])) { $this->_current = self::$_configBuffer[$lang . '-' . $file]; return true; } // shortcut using cache $cache = WBCache::get($lang . '/' . $file); if ($cache) { $this->_current = $cache; return true; } $expire = array(); if (!$this->loader->load($file, $data, $expire, $try)) { return false; } $this->_current = array(); if (empty($data)) { return false; } $unser = WBClass::create('WBConfig_Unserializer'); $unser->setLoader($this->loader->getName()); $this->_current = $unser->unserialize($data); if (empty($this->_current)) { return false; } self::$_configBuffer[$lang . '-' . $file] = $this->_current; WBCache::set($lang . '/' . $file, $this->_current, $expire); return true; } /** * Check if Configuration Exists * * In case current config is empty, return false * * @return bool */ public function has() { if (empty($this->_current)) { return false; } return true; } /** * force reload of config file * * Empty local config buffer and load config normally. This allows to * reload a configuration file within the same process * * @uses load() * @param string $file * @param bool $try try to load, don't throw an exception * @return bool true on success */ public function reload($file, $try = false) { // loaded before? if (isset(self::$_configBuffer[$file])) { unset(self::$_configBuffer[$file]); } clearstatcache(); return $this->load($file, $try); } }