]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Util/Introspection.php
Adapt Logger\Introspection
[friendica.git] / src / Core / Logger / Util / Introspection.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Logger\Util;
23
24 use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
25
26 /**
27  * Get Introspection information about the current call
28  */
29 class Introspection implements IHaveCallIntrospections
30 {
31         /** @var int  */
32         private $skipStackFramesCount;
33
34         /** @var string[] */
35         private $skipClassesPartials;
36
37         private $skipFunctions = [
38                 'call_user_func',
39                 'call_user_func_array',
40         ];
41
42         /**
43          * @param string[] $skipClassesPartials  An array of classes to skip during logging
44          * @param int      $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
45          */
46         public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0)
47         {
48                 $this->skipClassesPartials  = $skipClassesPartials;
49                 $this->skipStackFramesCount = $skipStackFramesCount;
50         }
51
52         /**
53          * Adds new classes to get skipped
54          *
55          * @param array $classNames
56          */
57         public function addClasses(array $classNames): void
58         {
59                 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
60         }
61
62         /**
63          * Returns the introspection record of the current call
64          *
65          * @return array
66          */
67         public function getRecord(): array
68         {
69                 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
70
71                 $i = 1;
72
73                 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
74                         $i++;
75                 }
76
77                 $i += $this->skipStackFramesCount;
78
79                 return [
80                         'file'     => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
81                         'line'     => $trace[$i - 1]['line'] ?? null,
82                         'function' => $trace[$i]['function'] ?? null,
83                 ];
84         }
85
86         /**
87          * Checks if the current trace class or function has to be skipped
88          *
89          * @param array $trace The current trace array
90          * @param int   $index The index of the current hierarchy level
91          *
92          * @return bool True if the class or function should get skipped, otherwise false
93          */
94         private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
95         {
96                 if (!isset($trace[$index])) {
97                         return false;
98                 }
99
100                 if (isset($trace[$index]['class'])) {
101                         foreach ($this->skipClassesPartials as $part) {
102                                 if (strpos($trace[$index]['class'], $part) !== false) {
103                                         return true;
104                                 }
105                         }
106                 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
107                         return true;
108                 }
109
110                 return false;
111         }
112 }