* @license PHP License
* @package WB
* @subpackage base
*/
WBClass::load('WBConfig_Loader');
/**
* Config string loader: Config table
*
* Load xml config string from tree trable
*
* @version 0.1.0
* @package WB
* @subpackage base
*/
class WBConfig_Loader_ConfigTable extends WBConfig_Loader
{
/**
* datasource tree table
* @var WBDatasource_Tree
*/
private $tree;
/**
* current user
* @var WBUser_Auth
*/
private $user;
/**
* constructor
*
* Init table tree
*
* @return unknown_type
*/
public function __construct($params)
{
$params = array(
'treetable' => 'config',
'alphalength' => 4,
);
$this->tree = WBClass::create('WBDatasource_Tree', $params);
}
/**
* list child nodes
*
* something like getChildren()
*
* @param string $file
* @return array list of child nodes
*/
public function ls($file)
{
// prevent mangling "file"
$copy = $file;
$id = $this->normalizePath($copy);
try {
$node = $this->tree->get($id);
} catch (WBException_Datasource $e) {
return array();
}
$list = array();
$children = $this->tree->getChildren($id);
foreach ($children as $c) {
$list[] = $file . '/' . $c['branch'];
}
return $list;
}
/**
* load configuration from file
*
* Well, load it from file or cache or buffer
*
* @param string $file
* @param string $data actual data loaded
* @param array $expire expire hint for cache
* @param bool $try try to load, don't throw an exception
* @return bool true on success
* @throws WBException_File
*/
public function load($file, &$data, &$expire, $try = false)
{
$id = $this->normalizePath($file);
$data = '';
try {
$node = $this->tree->get($id);
} catch (WBException_Datasource $e) {
if ($try) {
return false;
}
throw $e;
}
// cache config values for 60 seconds
$data = $node['config'];
$expire = array(
'ttl' => 60
);
return true;
}
/**
* save node
*
* Use file as path to save record
*
* @todo eventually flush cache
* @param string $file
* @param string $data
*/
public function save($file, $data)
{
$id = $this->normalizePath($file);
$path = explode('/', $file);
$branch = array_pop($path);
$path = implode('/', $path);
$parent = md5($path);
$uid = $this->tree->getIdentifier('user');
$node = array(
$uid => $this->getUserId(),
'config' => $data,
'pathcs' => $id,
'branch' => $branch
);
try {
$this->tree->get($id);
$this->tree->set($id,$node);
} catch (WBException_Datasource $e) {
$this->tree->addChild($parent, $node);
}
}
/**
* create sub tries in root
*
* Save nodes [[BASE]] and [[ETC]] to seperate between config
* and other XML stuff
*/
public function prepareRoot()
{
$uid = $this->tree->getIdentifier('user');
$node = array(
$uid => 0,
'config' => '',
'pathcs' => md5('[[BASE]]'),
'branch' => '[[BASE]]'
);
$this->tree->addChild(null, $node);
$node = array(
$uid => 0,
'config' => '',
'pathcs' => md5('[[ETC]]'),
'branch' => '[[ETC]]'
);
$this->tree->addChild(null, $node);
}
/**
* receive id of current user
*
* Start user object and get user id
*
* @return string
*/
private function getUserId()
{
if ($this->user) {
return $this->user->getId();
}
WBClass::load('WBUser');
$this->user = WBUser::getCurrent();
return $this->user->getId();
}
/**
* normalize file
*
* Store pathes starting with "/" in sub tree [[BASE]], the rest goes
* to [[ETC]].
*
* The file variable will be mangled accordingly. Also this method returns
* the md5-checksum used as identifier to save and load single node records.
*
* @param string $file
* @return string $id
*/
protected function normalizePath(&$file)
{
if (!empty($file) && '/' == $file[0]) {
$file = '[[BASE]]' . $file;
$id = md5($file);
return $id;
}
if (strncmp($file, '[[ETC]]', 7) != 0) {
$file = '[[ETC]]/' . $file;
}
$id = md5($file);
return $id;
}
}
?>