]> git.mxchange.org Git - friendica.git/blob - src/Util/Introspection.php
Refactored Logging environment (cleaned up)
[friendica.git] / src / Util / Introspection.php
1 <?php
2
3 namespace Friendica\Util;
4
5 /**
6  * Get Introspection information about the current call
7  */
8 class Introspection
9 {
10         private $skipStackFramesCount;
11
12         private $skipClassesPartials;
13
14         private $skipFunctions = [
15                 'call_user_func',
16                 'call_user_func_array',
17         ];
18
19         /**
20          * @param array $skipClassesPartials  An array of classes to skip during logging
21          * @param int   $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
22          */
23         public function __construct($skipClassesPartials = array(), $skipStackFramesCount = 0)
24         {
25                 $this->skipClassesPartials  = $skipClassesPartials;
26                 $this->skipStackFramesCount = $skipStackFramesCount;
27         }
28
29         /**
30          * Adds new classes to get skipped
31          * @param array $classNames
32          */
33         public function addClasses(array $classNames)
34         {
35                 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
36         }
37
38         /**
39          * Returns the introspection record of the current call
40          *
41          * @return array
42          */
43         public function getRecord()
44         {
45                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
46
47                 $i = 1;
48
49                 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
50                         $i++;
51                 }
52
53                 $i += $this->skipStackFramesCount;
54
55                 return [
56                         'file'     => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
57                         'line'     => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
58                         'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
59                 ];
60         }
61
62         /**
63          * Checks if the current trace class or function has to be skipped
64          *
65          * @param array $trace The current trace array
66          * @param int $index The index of the current hierarchy level
67          * @return bool True if the class or function should get skipped, otherwise false
68          */
69         private function isTraceClassOrSkippedFunction(array $trace, $index)
70         {
71                 if (!isset($trace[$index])) {
72                         return false;
73                 }
74
75                 if (isset($trace[$index]['class'])) {
76                         foreach ($this->skipClassesPartials as $part) {
77                                 if (strpos($trace[$index]['class'], $part) !== false) {
78                                         return true;
79                                 }
80                         }
81                 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
82                         return true;
83                 }
84
85                 return false;
86         }
87 }