forked from nigelgbanks/php_lib
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Backtrace.inc
155 lines (145 loc) · 4 KB
/
Backtrace.inc
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
<?php
// @codingStandardsIgnoreStart
/**
* @file
* Functions for determining call order and such...
*/
/**
* This function is only intended for use within this file.
*
* Convenience function used to account for the current function in the call stack.
* The caller assumes their function is index 0, so add one to account for this function.
*
* @param int $index
* Index to modify to account for the callee in the call stack.
*/
function _ignore_this_in_call_stack($index = NULL) {
$base_index = 1;
return (empty($index)) ? $base_index : $index + $base_index;
}
/**
* Gets the call stack properties at the specifed index.
*
* @param int $index
*
* @return array
* Return the call stack properties for the given index if found. Otherwise NULL is returned.
*/
function get_call_stack_index($index = NULL) {
$index = _ignore_this_in_call_stack($index);
$call_stack = debug_backtrace();
if (isset($call_stack[$index])) {
return $call_stack[$index];
}
return NULL;
}
/**
* Gets the given property of the caller at the specified index in the call stack.
*
* @param int $index
*
* @param string $property
* A call stack property. Expected to be one of the following.
* function -> The current function name.
* line -> The current line number.
* file -> The current file name.
* class -> The current class name.
* object -> The current object.
* type -> The current call type.
* args -> The arguments to the current function.
*/
function get_caller_property($index, $property) {
$index = _ignore_this_in_call_stack($index);
$call_stack = get_call_stack_index($index);
if (isset($call_stack) && isset($call_stack[$property])) {
return $call_stack[$property];
}
return NULL;
}
/**
* Gets the caller method at the given index.
*
* The caller of this function's index is assumed to be 0.
*
* @param int $index
*
* @return string
*/
function get_caller_function($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'function');
}
/**
* Gets the line of the caller method at the given index.
*
* The caller of this function's index is assumed to be 0.
*
* @param int $index
*
* @return int
*/
function get_caller_line($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'line');
}
/**
* Gets the name of file where the callee is located.
*
* @param int $index
*
* @return string
*/
function get_caller_file($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'file');
}
/**
* Gets the class of the object used to make the method call at the specified index.
*
* @param int $index
*
* @return string
*/
function get_caller_class($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'class');
}
/**
* Gets the object used to make the method call at the specified index.
*
* @param int $index
*
* @return object
* If defined the object used to make the call is returned otherwise NULL is returned.
*/
function get_caller_object($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'object');
}
/**
* Gets the call type of the caller at the specified index.
*
* @param int $index
*
* @return string
* If a method call, "->" is returned. If a static method call, "::" is returned.
* If a function call, nothing is returned.
*/
function get_caller_type($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'type');
}
/**
* Gets the function arguments for the function at the specified index in the call stack.
*
* @param int $index
*
* @return array
* If inside a function, this lists the functions arguments. If inside an included file,
* this lists the included file name(s).
*/
function get_caller_args($index = NULL) {
$index = _ignore_this_in_call_stack($index);
return get_caller_property($index, 'args');
}
// @codingStandardsIgnoreEnd