class-debug-backtrace.php
3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace WPML\Utils;
/**
* Class DebugBackTrace
*
* @package WPML\Utils
*/
class DebugBackTrace {
/** @var array */
private $debug_backtrace = [];
/** @var int */
private $limit;
/** @var bool */
private $provide_object;
/** @var bool */
private $ignore_args;
/** @var string */
private $debug_backtrace_function;
/**
* DebugBackTrace constructor.
*
* @param int $limit
* @param bool $provide_object
* @param bool $ignore_args
* @param null|string $debug_backtrace_function
*/
public function __construct(
$limit = 0,
$provide_object = false,
$ignore_args = true,
$debug_backtrace_function = null
) {
if ( ! $debug_backtrace_function ) {
$debug_backtrace_function = 'debug_backtrace';
}
$this->limit = $limit;
$this->provide_object = $provide_object;
$this->ignore_args = $ignore_args;
$this->debug_backtrace_function = $debug_backtrace_function;
}
/**
* @param array $functions
* @param bool $refresh
*
* @return bool
*/
public function are_functions_in_call_stack( array $functions, $refresh = true ) {
if ( empty( $this->debug_backtrace ) || $refresh ) {
$this->get_backtrace();
}
$found = false;
foreach ( $this->debug_backtrace as $frame ) {
if ( isset( $frame['class'] ) ) {
$frame_function = [ $frame['class'], $frame['function'] ];
} else {
$frame_function = $frame['function'];
}
if ( in_array( $frame_function, $functions, true ) ) {
$found = true;
break;
}
}
return $found;
}
/**
* @param string $function_name
* @param bool $refresh
*
* @return bool
*/
public function is_function_in_call_stack( $function_name, $refresh = true ) {
return $this->are_functions_in_call_stack( [ $function_name ], $refresh );
}
/**
* @param string $function_name
* @param bool $refresh
*
* @return int
*/
public function count_function_in_call_stack( $function_name, $refresh = true ) {
if ( empty( $this->debug_backtrace ) || $refresh ) {
$this->get_backtrace();
}
$count = 0;
foreach ( $this->debug_backtrace as $frame ) {
if ( ! isset( $frame['class'] ) && $frame['function'] === $function_name ) {
$count ++;
}
}
return $count;
}
/**
* @param string $class_name
* @param string $function_name
* @param bool $refresh
*
* @return bool
*/
public function is_class_function_in_call_stack( $class_name, $function_name, $refresh = true ) {
return $this->are_functions_in_call_stack( [ [ $class_name, $function_name ] ], $refresh );
}
/**
* @return array
*/
public function get_backtrace() {
$options = false;
// As of 5.3.6, 'options' parameter is a bit mask for the following options.
if ( $this->provide_object ) {
$options |= DEBUG_BACKTRACE_PROVIDE_OBJECT;
}
if ( $this->ignore_args ) {
$options |= DEBUG_BACKTRACE_IGNORE_ARGS;
}
$actual_limit = 0 === $this->limit ? 0 : $this->limit + 4;
$this->debug_backtrace = (array) call_user_func_array(
$this->debug_backtrace_function,
[
$options,
$actual_limit,
]
); // Add one item to include the current frame.
$this->remove_frames_for_this_class();
return $this->debug_backtrace;
}
private function remove_frames_for_this_class() {
/**
* We cannot rely on number of frames to remove.
* php 5.6 and 7+ provides different call stacks.
* php 5.6 adds call_user_func_array from get_backtrace()
*/
do {
$found = false;
if ( ! isset( $this->debug_backtrace[0] ) ) {
break;
}
$frame = $this->debug_backtrace[0];
if (
( isset( $frame['file'] ) && __FILE__ === $frame['file'] )
|| ( isset( $frame['class'] ) && self::class === $frame['class'] )
) {
$found = true;
$this->remove_last_frame();
}
} while ( $found );
$this->remove_last_frame(); // Remove frame with the function called this class.
}
public function remove_last_frame() {
if ( $this->debug_backtrace ) {
array_shift( $this->debug_backtrace );
}
}
}