]> git.mxchange.org Git - friendica.git/blob - src/Util/Introspection.php
- Revert HTTPSignature change
[friendica.git] / src / Util / Introspection.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 /**
25  * Get Introspection information about the current call
26  */
27 class Introspection
28 {
29         private $skipStackFramesCount;
30
31         private $skipClassesPartials;
32
33         private $skipFunctions = [
34                 'call_user_func',
35                 'call_user_func_array',
36         ];
37
38         /**
39          * @param array $skipClassesPartials  An array of classes to skip during logging
40          * @param int   $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
41          */
42         public function __construct($skipClassesPartials = array(), $skipStackFramesCount = 0)
43         {
44                 $this->skipClassesPartials  = $skipClassesPartials;
45                 $this->skipStackFramesCount = $skipStackFramesCount;
46         }
47
48         /**
49          * Adds new classes to get skipped
50          * @param array $classNames
51          */
52         public function addClasses(array $classNames)
53         {
54                 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
55         }
56
57         /**
58          * Returns the introspection record of the current call
59          *
60          * @return array
61          */
62         public function getRecord()
63         {
64                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
65
66                 $i = 1;
67
68                 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
69                         $i++;
70                 }
71
72                 $i += $this->skipStackFramesCount;
73
74                 return [
75                         'file'     => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
76                         'line'     => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
77                         'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
78                 ];
79         }
80
81         /**
82          * Checks if the current trace class or function has to be skipped
83          *
84          * @param array $trace The current trace array
85          * @param int   $index The index of the current hierarchy level
86          *
87          * @return bool True if the class or function should get skipped, otherwise false
88          */
89         private function isTraceClassOrSkippedFunction(array $trace, $index)
90         {
91                 if (!isset($trace[$index])) {
92                         return false;
93                 }
94
95                 if (isset($trace[$index]['class'])) {
96                         foreach ($this->skipClassesPartials as $part) {
97                                 if (strpos($trace[$index]['class'], $part) !== false) {
98                                         return true;
99                                 }
100                         }
101                 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
102                         return true;
103                 }
104
105                 return false;
106         }
107 }