|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Handles backtrace requests to output the content |
|
5
|
|
|
* |
|
6
|
|
|
* PHP Version 5 |
|
7
|
|
|
* |
|
8
|
|
|
* @category Core |
|
9
|
|
|
* @package Debug |
|
10
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
11
|
|
|
* @copyright 2013 cSphere Team |
|
12
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
13
|
|
|
* @link http://www.csphere.eu |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
namespace csphere\core\debug; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handles backtrace requests to output the content |
|
20
|
|
|
* |
|
21
|
|
|
* @category Core |
|
22
|
|
|
* @package Debug |
|
23
|
|
|
* @author Hans-Joachim Piepereit <[email protected]> |
|
24
|
|
|
* @copyright 2013 cSphere Team |
|
25
|
|
|
* @license http://opensource.org/licenses/bsd-license Simplified BSD License |
|
26
|
|
|
* @link http://www.csphere.eu |
|
27
|
|
|
**/ |
|
28
|
|
|
|
|
29
|
|
|
class Backtrace |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Local file path prefix |
|
33
|
|
|
**/ |
|
34
|
|
|
private $_path = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Length in chars to cut from file path |
|
38
|
|
|
**/ |
|
39
|
|
|
private $_cut = 0; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Holds the most current content |
|
43
|
|
|
**/ |
|
44
|
|
|
private $_content = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create traceback for the exception |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Exception $exception Exception class object |
|
50
|
|
|
* |
|
51
|
|
|
* @return \csphere\core\debug\Backtrace |
|
52
|
|
|
**/ |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
public function __construct(\Exception $exception) |
|
55
|
|
|
{ |
|
56
|
|
|
// This prepares the file name shortening later on |
|
57
|
|
|
$this->_path = \csphere\core\init\path(); |
|
58
|
|
|
$this->_cut = strlen($this->_path); |
|
59
|
|
|
|
|
60
|
|
|
// Create backtrace content |
|
61
|
|
|
$this->_content['error'] = $this->_formatError($exception); |
|
62
|
|
|
$this->_content['trace'] = $this->_formatTrace($exception); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Return the string to attach to the template |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
**/ |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
public function content() |
|
72
|
|
|
{ |
|
73
|
|
|
// Get loader and view |
|
74
|
|
|
$loader = \csphere\core\service\Locator::get(); |
|
75
|
|
|
$view = $loader->load('view'); |
|
76
|
|
|
|
|
77
|
|
|
// Format data for template usage |
|
78
|
|
|
$data = $this->_content; |
|
79
|
|
|
|
|
80
|
|
|
// Send data to view and fetch box result |
|
81
|
|
|
$view->template('debug', 'core_backtrace', $data, true); |
|
82
|
|
|
|
|
83
|
|
|
$result = $view->box(); |
|
84
|
|
|
|
|
85
|
|
|
return $result; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Format call |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $call The call data as an array |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
**/ |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
private function _formatCall(array $call) |
|
97
|
|
|
{ |
|
98
|
|
|
$args = ''; |
|
99
|
|
|
|
|
100
|
|
|
// Check for call details and add most important data |
|
101
|
|
|
if (isset($call[0])) { |
|
102
|
|
|
|
|
103
|
|
|
foreach ($call AS $data) { |
|
104
|
|
|
|
|
105
|
|
|
// Type of data might be interesting |
|
106
|
|
|
if (is_object($data)) { |
|
107
|
|
|
|
|
108
|
|
|
$args .= 'Object: ' . get_class($data) . ', '; |
|
109
|
|
|
} elseif (is_array($data)) { |
|
110
|
|
|
|
|
111
|
|
|
$args .= 'Array, '; |
|
112
|
|
|
} else { |
|
113
|
|
|
|
|
114
|
|
|
$args .= "'" . $data . "', "; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$args = substr($args, 0, -2); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $args; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Format error message |
|
126
|
|
|
* |
|
127
|
|
|
* @param \Exception $exception Exception class object |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
**/ |
|
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
private function _formatError(\Exception $exception) |
|
133
|
|
|
{ |
|
134
|
|
|
$msg = ['message' => $exception->getMessage(), |
|
135
|
|
|
'code' => $exception->getCode(), |
|
136
|
|
|
'file' => $exception->getFile(), |
|
137
|
|
|
'line' => $exception->getLine()]; |
|
138
|
|
|
|
|
139
|
|
|
return $msg; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Format trace |
|
144
|
|
|
* |
|
145
|
|
|
* @param \Exception $exception Exception class object |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
**/ |
|
|
|
|
|
|
149
|
|
|
|
|
150
|
|
|
private function _formatTrace(\Exception $exception) |
|
151
|
|
|
{ |
|
152
|
|
|
$trace = $exception->getTrace(); |
|
153
|
|
|
$out = []; |
|
154
|
|
|
$count = count($trace); |
|
155
|
|
|
|
|
156
|
|
|
// All steps starting with the last one as zero |
|
157
|
|
|
for ($back = 0; $back < $count; $back++) { |
|
158
|
|
|
|
|
159
|
|
|
$out[] = $this->_formatStep($trace, $back); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return $out; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Format step |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $trace Trace as an array |
|
169
|
|
|
* @param integer $back Number of target step |
|
170
|
|
|
* |
|
171
|
|
|
* @return array |
|
172
|
|
|
**/ |
|
|
|
|
|
|
173
|
|
|
|
|
174
|
|
|
private function _formatStep(array $trace, $back) |
|
175
|
|
|
{ |
|
176
|
|
|
$args = ''; |
|
177
|
|
|
$call = ''; |
|
178
|
|
|
|
|
179
|
|
|
// Check if details are available |
|
180
|
|
|
if (isset($trace[$back]['args'])) { |
|
181
|
|
|
|
|
182
|
|
|
$args = $trace[$back]['args']; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if (is_array($args)) { |
|
186
|
|
|
|
|
187
|
|
|
$args = $this->_formatCall($args); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
if (isset($trace[$back]['class'])) { |
|
191
|
|
|
|
|
192
|
|
|
$call .= $trace[$back]['class']; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
if (isset($trace[$back]['type'])) { |
|
196
|
|
|
|
|
197
|
|
|
$call .= $trace[$back]['type']; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// Some rare cases have no file and line details |
|
201
|
|
|
if (empty($trace[$back]['file'])) { |
|
202
|
|
|
|
|
203
|
|
|
$trace[$back]['file'] = ''; |
|
204
|
|
|
$trace[$back]['line'] = ''; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
$file = $this->_formatFile($trace[$back]['file']); |
|
208
|
|
|
|
|
209
|
|
|
// Build array with details |
|
210
|
|
|
$call = $call . $trace[$back]['function'] . '(' . $args . ')'; |
|
211
|
|
|
|
|
212
|
|
|
$out = ['step' => $back, |
|
213
|
|
|
'file' => $file, |
|
214
|
|
|
'line' => $trace[$back]['line'], |
|
215
|
|
|
'call' => $call]; |
|
216
|
|
|
|
|
217
|
|
|
return $out; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Format filename |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $filename Filename |
|
224
|
|
|
* |
|
225
|
|
|
* @return string |
|
226
|
|
|
**/ |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
private function _formatFile($filename) |
|
229
|
|
|
{ |
|
230
|
|
|
// Shorten and style file names |
|
231
|
|
|
$filename = str_replace('\\', '/', $filename); |
|
232
|
|
|
|
|
233
|
|
|
$filepath = substr($filename, 0, $this->_cut); |
|
234
|
|
|
|
|
235
|
|
|
if ($filepath == $this->_path) { |
|
236
|
|
|
|
|
237
|
|
|
$filename = substr($filename, $this->_cut); |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return $filename; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.