Merge pull request #7276 from MrPetovan/bug/7275-share-new-lines
[friendica.git/.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          *
68          * @return bool True if the class or function should get skipped, otherwise false
69          */
70         private function isTraceClassOrSkippedFunction(array $trace, $index)
71         {
72                 if (!isset($trace[$index])) {
73                         return false;
74                 }
75
76                 if (isset($trace[$index]['class'])) {
77                         foreach ($this->skipClassesPartials as $part) {
78                                 if (strpos($trace[$index]['class'], $part) !== false) {
79                                         return true;
80                                 }
81                         }
82                 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {
83                         return true;
84                 }
85
86                 return false;
87         }
88 }