LSsearch: Add option to disable cache on customInfos

This commit is contained in:
Benjamin Renard 2020-05-19 17:07:42 +02:00
parent 2ed985324e
commit cb83311bd0
2 changed files with 27 additions and 5 deletions

View file

@ -518,7 +518,8 @@ class LSsearch {
if (is_callable($data['function'])) {
$this -> params['customInfos'][$name] = array (
'function' => &$data['function'],
'args' => $data['args']
'args' => (isset($data['args'])?$data['args']:null),
'cache' => (isset($data['cache'])?boolval($data['cache']):true),
);
}
else {

View file

@ -237,16 +237,24 @@ class LSsearchEntry extends LSlog_staticLoggerClass {
return (isset($this -> attrs[$key])?$this -> attrs[$key]:null);
}
elseif (array_key_exists($key,$this->params['customInfos'])) {
if(isset($this -> cache['customInfos'][$key])) {
$cache = $this -> getConfig("customInfos.$key.cache", true, 'bool');
if($cache && isset($this -> cache['customInfos'][$key])) {
self :: log_debug("__get($key): custom info retreived from cache");
return $this -> cache['customInfos'][$key];
}
if(is_array($this->params['customInfos'][$key]['function']) && is_string($this->params['customInfos'][$key]['function'][0])) {
self :: log_debug("__get($key): load class '".$this->params['customInfos'][$key]['function'][0]."'");
LSsession::loadLSclass($this->params['customInfos'][$key]['function'][0]);
}
if(is_callable($this->params['customInfos'][$key]['function'])) {
$this -> cache['customInfos'][$key]=call_user_func($this->params['customInfos'][$key]['function'],$this,$this->params['customInfos'][$key]['args']);
return $this -> cache['customInfos'][$key];
self :: log_debug("__get($key): call ".varDump($this->params['customInfos'][$key]['function'])."");
$value = call_user_func($this->params['customInfos'][$key]['function'], $this, $this->params['customInfos'][$key]['args']);
if ($cache)
$this -> cache['customInfos'][$key] = $value;
return $value;
}
else
self :: log_error("__get($key): custom info function is not callable: ".varDump($this->params['customInfos'][$key]['function']));
}
else {
self :: log_warning('LSsearchEntry : '.$this -> dn.' => Unknown property '.$key.' !');
@ -254,6 +262,19 @@ class LSsearchEntry extends LSlog_staticLoggerClass {
}
}
/**
* Return a configuration parameter (or default value)
*
* @param[] $param The configuration parameter
* @param[] $default The default value (default : null)
* @param[] $cast Cast resulting value in specific type (default : disabled)
*
* @retval mixed The configuration parameter value or default value if not set
**/
function getConfig($param, $default=null, $cast=null) {
return LSconfig :: get($param, $default, $cast, $this -> params);
}
}
/**