FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Debug.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class Debug extends Component{
6 
7  protected $__baseClassName = null;
8  protected $__baseClass = null;
9  protected $__log = null;
10  protected $__prio = [
11  'get:before' => Log::DEBUG,
12  'get:after' => Log::DEBUG,
13  'set:before' => Log::DEBUG,
14  'set:after' => Log::DEBUG,
15  'call:before' => Log::DEBUG,
16  'call:after' => Log::DEBUG
17  ];
18 
19  public function __construct($fred,$config = null){
20  $this->__baseClassName = $config['baseClassName'];
21  $this->__baseClass = new $this->__baseClassName($fred,$config);
22  $this->__log = $fred->get('log');
23  return parent::__construct($fred,$config);
24  }
25 
26  public function __get($key){
27  $message = $this->__baseClassName . '->' . $key;
28  if(($prio = $this->__prio['get:before'] ?? null) !== null) $this->__log->add($prio,$message);
29  $value = $this->__baseClass->$key;
30  if(($prio = $this->__prio['get:after'] ?? null) !== null) $this->__log->add($prio,$message . ' => ' . (is_scalar($value) ? $value : '...'),is_array($value) ? $value : null);
31  return $value;
32  }
33 
34  public function __set($key,$value){
35  $message = $this->__baseClassName . '->' . $key;
36  if(($prio = $this->__prio['set:before'] ?? null) !== null) $this->__log->add($prio,$message . ' <= ' . (is_scalar($value) ? $value : '...'),is_array($value) ? $value : null);
37  $this->__baseClass->$key = $value;
38  if(($prio = $this->__prio['set:after'] ?? null) !== null) $this->__log->add($prio,$message . ' = ' . (is_scalar($value) ? $value : '...'),is_array($value) ? $value : null);
39  }
40 
41  public function __call($func_name,$params){
42  $message = $this->__baseClassName . "::$func_name(";
43  $context = [];
44  foreach($params as $index => $param){
45  if(is_scalar($param)) $context[] = $param;
46  elseif(is_array($param)){
47  $context[] = $param;
48  $param = '[...]';
49  }
50  elseif(is_object($param)) $context[] = $param = get_class($param);
51  else $context[] = $param = '...';
52  $message .= $param . ($index + 1 < count($params) ? ',' : ')');
53  }
54  if(($prio = $this->__prio['call:before'] ?? null) !== null) $this->__log->add($prio,$message,$context);
55  $result = call_user_func_array([$this->__baseClass,$func_name],$params);
56  if(($prio = $this->__prio['call:after'] ?? null) !== null){
57  if(is_scalar($result) || is_array($result)) $context['result'] = $result;
58  $this->__log->add($prio,$message . ' => ' . (is_scalar($result) ? $result : '...'),$context);
59  }
60  return $result;
61  }
62 
63 }
__get($key)
Definition: Debug.php:26
__construct($fred, $config=null)
Definition: Debug.php:19
const DEBUG
Debug-level message.
Definition: Log.php:15
__call($func_name, $params)
Definition: Debug.php:41
$__baseClassName
Definition: Debug.php:7
__set($key, $value)
Definition: Debug.php:34
Basic component class.
Definition: Component.php:8