* @license PHP License * @package WB * @subpackage base */ /** * Parameter container * * Symply store sort of global parameter * * @version 0.2.1 * @package WB * @subpackage base */ class WBParam { /** * actual parameter container * * @var array */ static $_param = array(); /** * parameter listener * * @var array */ static $_observer = array(); /** * static constructor * * - remember time of start * - load observer class */ static public function staticConstruct() { // setup system and base dirs $sysDir = realpath(dirname(__FILE__) . '/../..'); self::$_param = array( 'wb' => array( 'time' => array( 'genesis' => microtime( true ) ), 'dir' => array( 'system' => $sysDir, 'base' => $sysDir ) ) ); WBClass::load('WBParam_Observer'); } /** * receive parameter value * * @param string $path parameter path * @param mixed $default any default value * @return mixed */ static public function get($path = null, $default = null) { if (empty($path)) { return self::$_param; } if(!is_array($path)) { $path = explode('/', $path); } $path = array_map('strtolower', $path); $config =& self::$_param; foreach ($path as $p) { if (!isset($config[$p])) { return $default; } $config =& $config[$p]; } return $config; } /** * Set parameter value * * Inform observer about new value and set parameter to new value * * @param string $path parameter path * @param mixed $value * @return true on success */ static public function set($path = null, $value = null) { if (empty($path)) { self::remove( '' ); self::$_param = $value; if (isset(self::$_observer['/'])) { foreach (self::$_observer['/'] as $obs) { $obs->notifyOnSet('', $value); } } } if (!is_array($path)) { $path = explode('/', $path); } $path = array_map('strtolower', $path); $config =& self::$_param; $last = array_pop($path); foreach ($path as $p) { if (!isset($config[$p])) { $config[$p] = array(); } if (!is_array($config[$p])) { WBClass::load('WBException_Config'); throw new WBException_Config('Selected path is a leave note, it is not possible to add child nodes', 1, __CLASS__); } $config =& $config[$p]; } // remove observer $path[] = $last; $path = implode('/', $path); self::remove($path); if (isset(self::$_observer[$path . '/'])) { foreach(self::$_observer[$path . '/'] as $obs) { $obs->notifyOnSet($path, $value); } } // store value $config[$last] = $value; return true; } /** * remove observer when variable was unset * * This deeply removes all observers along the path * * @param string $path parameter path * @return bool true on success */ static protected function remove($name) { // make sure there is a "/" at the end $name = strtolower(trim($name, '/ ') . '/'); foreach (array_keys(self::$_observer) as $k) { // find matching observer $nameLength = strlen($name); if (strncmp($name, $k, $nameLength) != 0 || strlen($k) <= $nameLength) { continue; } // remove them foreach (array_keys(self::$_observer[$k]) as $obs) { unset(self::$_observer[$k][$obs]); } } return true; } /** * Add observer for parameter * * @param string $path parameter path * @param WBParam_Observer $obs observer object to be notified * @return bool true on success */ static public function attach($name, WBParam_Observer $obs) { // make sure there is a "/" at the end $name = strtolower(trim($name, '/ ') . '/'); if (!isset(self::$_observer[$name])) { self::$_observer[$name] = array(); } self::$_observer[$name][$obs->getObjectId()] = $obs; return true; } /** * Remove observer * * @param string $path parameter path * @param WBParam_Observer $obs observer object to be notified * @return bool true on success */ static public function detach($name, WBParam_Observer $obs) { // make sure there is a "/" at the end $name = strtolower(trim($name, '/ ') . '/'); if (!isset(self::$_observer[$name])) { return false; } unset(self::$_observer[$name][$obs->getObjectId()]); return true; } } ?>