* @license PHP License * @package WB * @subpackage base */ /** * Parameter container * * Symply store sort of global parameter * * @version 0.2.5 * @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) ), 'debug' => 0, 'dir' => array( 'config' => 'etc', 'system' => $sysDir, 'base' => $sysDir, 'resource' => array($sysDir) ), 'user' => array( 'storage' => 'Table', 'password' => array( // 'algo' => 'sha256', 'iteration' => 10000, 'algomigrate' => 0 ) ), 'template' => array( 'listtemplatemax' => 10, ), 'config' => array( 'resource' => array( 'blog' => 1, 'captcha' => 1, 'chart' => 1, 'common' => 1, 'comment' => 1, 'contact' => 1, 'default' => 1, 'event' => 1, 'info' => 1, 'its' => 1, 'landingpage' => 1, 'locale' => 1, 'log' => 1, 'login' => 1, 'menumaker' => 1, 'newsletter'=> 1, 'rate' => 1, 'table' => 1, 'user' => 1, 'ute' => 1, 'vfs' => 1, 'webinar' => 1, 'wxml' => 1 ), 'loader' => array( 'service' => array( 'site' => 'File', 'rest' => 'File' ), ) ) ) ); 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; } }