Add __toString() method to all common extendable PHP classes

This permit to make context logging easier.
This commit is contained in:
Benjamin Renard 2020-09-08 17:29:10 +02:00
parent e89d13d002
commit df1c46eab6
12 changed files with 156 additions and 16 deletions

View file

@ -54,6 +54,15 @@ class LSattr_html extends LSlog_staticLoggerClass {
return true;
}
/**
* Allow conversion of LSattr_html to string
*
* @retval string The string representation of the LSattr_html
*/
public function __toString() {
return "<".get_class($this)." ".$this -> name.">";
}
/**
* Retourne le label de l'attribut
*

View file

@ -75,6 +75,15 @@ class LSform extends LSlog_staticLoggerClass {
LSsession :: loadLSclass('LSformElement');
}
/**
* Allow conversion of LSform to string
*
* @retval string The string representation of the LSform
*/
public function __toString() {
return "<LSform ".$this -> idForm." on ".$this -> ldapObject -> toString(false).">";
}
/**
* Display the form
*

View file

@ -66,6 +66,15 @@ class LSformElement extends LSlog_staticLoggerClass {
return true;
}
/**
* Allow conversion of LSformElement to string
*
* @retval string The string representation of the LSformElement
*/
public function __toString() {
return strval($this -> form)." -> <".get_class($this)." ".$this -> name.">";
}
/**
* Définis la valeur de l'élément
*

View file

@ -1898,15 +1898,32 @@ class LSldapObject extends LSlog_staticLoggerClass {
* @retval string The string representation of the LdapObject
*/
public function __toString() {
return $this -> toString(true);
}
/**
* Return string representation of the object
*
* @param[in] $enclosed boolean If true, result will be enclosed using "< >" (optional, default: true)
*
* @retval string The string representation of the LdapObject
*/
public function toString($enclosed=true) {
$str = "LdapObject ".$this -> getType();
if ($this -> dn)
return "<LdapObject ".$this -> dn.">";
$rdn_attr = $this -> getConfig('rdn');
if( $rdn_attr && isset($this -> attrs[$rdn_attr]) ) {
$rdn_val = $this -> attrs[$rdn_attr] -> getUpdateData();
if (!empty($rdn_val))
return "<LdapObject (new) $rdn_attr=".$rdn_val[0].">";
$str .= " ".$this -> dn;
else {
$str .= " (new)";
$rdn_attr = $this -> getConfig('rdn');
if( $rdn_attr && isset($this -> attrs[$rdn_attr]) ) {
$rdn_val = $this -> attrs[$rdn_attr] -> getUpdateData();
if (!empty($rdn_val))
$str .= " $rdn_attr=".$rdn_val[0];
}
}
return "<LdapObject (new)>";
if ($enclosed)
return "<$str>";
return $str;
}
/**

View file

@ -43,6 +43,19 @@ class LSlog_email extends LSlog_handler {
public function __construct($config) {
parent :: __construct($config);
$this -> recipient = self :: getConfig('recipient');
$this -> logging('TRACE', "$this Enabled", get_class($this));
}
/**
* Return list of details for the string representation of the LSlog_email
*
* @retval array List of details for the string representation of the LSlog_email
*/
public function __toStringDetails() {
return array_merge(
array("recipient=".$this -> recipient),
parent :: __toStringDetails(),
);
}
/**

View file

@ -43,6 +43,19 @@ class LSlog_file extends LSlog_handler {
$this -> path = self :: getConfig('path', LSlog :: getConfig('filename', 'tmp/LS.log'));
if (substr($this -> path, 0, 1) != '/')
$this -> path = LS_ROOT_DIR."/".$this -> path;
$this -> logging('TRACE', "$this Enabled", get_class($this));
}
/**
* Return list of details for the string representation of the LSlog_file
*
* @retval array List of details for the string representation of the LSlog_file
*/
public function __toStringDetails() {
return array_merge(
array("path=".$this -> path),
parent :: __toStringDetails(),
);
}
/**

View file

@ -65,6 +65,28 @@ class LSlog_handler {
$this -> excluded_loggers = array($this -> excluded_loggers);
}
/**
* Allow conversion of LSlog_handler to string
*
* @retval string The string representation of the LSlog_handler
*/
public function __toString() {
return "<".get_class($this)." ".implode(', ', $this -> __toStringDetails()).">";
}
/**
* Return list of details for the string representation of the LSlog_handler
*
* @retval array List of details for the string representation of the LSlog_handler
*/
public function __toStringDetails() {
return array(
"level=".($this -> level?$this -> level:'default'),
"loggers=".($this -> loggers?implode(',', $this -> loggers):'all'),
"excluded loggers=".($this -> excluded_loggers?implode(',', $this -> excluded_loggers):'no'),
);
}
/**
* Check system compatibility with this handler
*

View file

@ -52,10 +52,17 @@ class LSlog_logger {
$this -> config = $config;
$this -> enabled = $this -> getConfig('enabled', true, 'boolean');
$this -> level = $this -> getConfig('level');
if ($this -> enabled)
$this -> debug("Enabled $name logger with level=".$this -> level);
}
/**
* Allow conversion of LSlog_logger to string
*
* @retval string The string representation of the LSlog_logger
*/
public function __toString() {
return "<".get_class($this)." ".$this -> name.">";
}
/**
* Get a configuration variable value
*

View file

@ -59,6 +59,19 @@ class LSlog_syslog extends LSlog_handler {
public function __construct($config) {
parent :: __construct($config);
$this -> priority = static :: getConfig('priority');
$this -> logging('TRACE', "$this Enabled", get_class($this));
}
/**
* Return list of details for the string representation of the LSlog_email
*
* @retval array List of details for the string representation of the LSlog_email
*/
public function __toStringDetails() {
return array_merge(
array("priority=".$this -> priority),
parent :: __toStringDetails(),
);
}
/**

View file

@ -61,6 +61,15 @@ class LSrelation extends LSlog_staticLoggerClass {
}
}
/**
* Allow conversion of LSrelation to string
*
* @retval string The string representation of the LSrelation
*/
public function __toString() {
return "<LSrelation ".$this -> name." on ".$this -> obj -> toString(false).">";
}
/**
* Return a configuration parameter (or default value)
*

View file

@ -115,6 +115,16 @@ class LSsearch extends LSlog_staticLoggerClass {
}
/**
* Allow conversion of LSsearch to string
*
* @retval string The string representation of the LSsearch
*/
public function __toString() {
$search_params = $this -> formatSearchParams();
return "<LSsearch of ".$this -> LSobject." (context=".$this -> context.")".($search_params?" $search_params":"").">";
}
/**
* Load configuration from LSconfig
*
@ -912,18 +922,18 @@ class LSsearch extends LSlog_staticLoggerClass {
*/
public function run($cache=true) {
$this -> generateSearchParams();
self :: log_debug("run(".($cache?'with cache':'without cache')."): ".self :: formatSearchParams());
self :: log_debug($this." -> run(".($cache?'with cache':'without cache').")");
if( $cache && (!isset($_REQUEST['refresh'])) && (!$this -> params['withoutCache']) ) {
self :: log_debug('Cache enabled');
self :: log_debug($this.' -> run(): Cache enabled');
$this -> result = $this -> getResultFromCache();
if ($this -> result)
self :: log_debug('result retreived from cache');
self :: log_debug($this.' -> run(): result retreived from cache');
else
self :: log_debug('result not found in cache');
self :: log_debug($this.' -> run(): result not found in cache');
}
else {
self :: log_debug('Cache disabled');
self :: log_debug($this.' -> run(): Cache disabled');
$this -> setParam('withoutCache', false);
}
@ -948,7 +958,7 @@ class LSsearch extends LSlog_staticLoggerClass {
// Handle onlyAccessible parameter
if ($this -> getParam('onlyAccessible') && LSsession :: getLSuserObjectDn()) {
self :: log_debug('Filter on only accessible object');
self :: log_debug($this.' -> run(): Filter on only accessible object');
$this -> result['list'] = array();
// Check user rights on objets
@ -963,7 +973,7 @@ class LSsearch extends LSlog_staticLoggerClass {
$this -> addResultToCache();
}
self :: log_debug($this -> total. " object(s) found");
self :: log_debug($this.' -> run(): '.$this -> total. " object(s) found");
$this -> doSort();

View file

@ -75,6 +75,15 @@ class LSsearchEntry extends LSlog_staticLoggerClass {
$this -> cache =& $result[$id]['cache'];
}
/**
* Allow conversion of LSsearchEntry to string
*
* @retval string The string representation of the LSsearchEntry
*/
public function __toString() {
return $this -> LSsearch." -> <LSsearchEntry of ".$this -> dn." (ID #".$this -> id.")>";
}
/**
* Get text value of entry
*