This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
BaseException.php
495 lines (430 loc) · 12.8 KB
/
BaseException.php
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
<?php
/**
* BaseException is parent of all exceptions in Agile Toolkit which
* are meant to be for informational purposes. There are also some
* exceptions (StopInit) which are used for data-flow.
*/
class BaseException extends Exception
{
// Exception defines it's methods as "final", which is complete nonsence
// and incorrect behavor in my opinion. Therefore I need to re-declare
// it's class and re-define the methods so I could extend my own methods
// in my classes.
/** @var array Backtrace array */
public $my_backtrace;
/** @var int Backtrace shift */
public $shift = 0;
/** @var string Classname of exception */
public $name;
/** @var string|int error code */
public $code;
/** @var array Array with more info */
public $more_info = array();
/** @var string Plain text recommendation on how the problem can be solved */
public $recommendation;
/** @var array Array of available actions */
public $actions = array();
/** @var Exception Link to another exception which caused this one */
public $by_exception = null;
/** @var AbstractObject Link to object into which we added this object */
public $owner;
/** @var App_CLI Always points to current Application */
public $app;
/**
* @deprecated 4.3.0 Left for compatibility with ATK 4.2 and lower, use ->app instead
*/
public $api;
/**
* Initialization.
*/
public function init()
{
}
/**
* On class construct.
*
* @param string $msg Error message
* @param string|int $code Error code
*/
public function __construct($msg, $code = 0)
{
parent::__construct($msg, $code);
$this->collectBasicData($code);
}
/**
* Collect basic data of exception.
*
* @param string|int $code Error code
*/
public function collectBasicData($code)
{
$this->name = get_class($this);
$this->my_backtrace = debug_backtrace();
array_shift($this->my_backtrace);
array_shift($this->my_backtrace);
}
/**
* Call this to add additional information to the exception you are about
* to throw.
*
* @param string $key
* @param mixed $value
*
* @return $this
*/
public function addMoreInfo($key, $value)
{
$this->more_info[$key] = $value;
return $this;
}
/**
* Add reference to the object.
* Do not call this directly, exception() method takes care of that.
*
* @param AbstractObject $obj
*/
public function addThis($obj)
{
return $this->addMoreInfo('Raised by object', $obj);
}
/**
* Set error code
*
* @param int $code
*/
public function setCode(int $code)
{
$this->code = $code;
}
/**
* Records another exception as a cause of your current exception.
* Wrapping one exception inside another helps you to track problems
* better.
*
* @param Exception $e
*
* @return $this
*/
public function by(Exception $e)
{
$this->by_exception = $e;
return $this;
}
/**
* Actions will be displayed as links on the exception page allowing viewer
* to perform additional debug functions.
* addAction('show info',array('info'=>true)) will result in link to &info=1.
*
* @param string|array $key
* @param string|array $descr
*
* @return $this
*/
public function addAction($key, $descr)
{
if (is_array($key)) {
$this->recommendation = (string) $descr;
$this->actions = array_merge($this->actions, $key);
return $this;
}
$this->actions[$key] = $descr;
return $this;
}
/**
* Return collected backtrace info.
*
* @return array
*/
public function getMyTrace()
{
return $this->my_backtrace;
}
/**
* Return filename from backtrace log.
*
* @return string
*/
public function getMyFile()
{
return $this->my_backtrace[2]['file'];
}
/**
* Return line number from backtract log.
*
* @return string
*/
public function getMyLine()
{
return $this->my_backtrace[2]['line'];
}
/**
* Returns HTML representation of the exception.
*
* @return string
*/
public function getHTML()
{
$e = $this;
$o = '<div class="atk-layout">';
$o .= $this->getHTMLHeader();
$o .= $this->getHTMLSolution();
//$o.=$this->getHTMLBody();
$o .= '<div class="atk-layout-row"><div class="atk-wrapper atk-section-small">';
if (isset($e->more_info)) {
$o .= '<h3>Additional information:</h3>';
$o .= $this->print_r($e->more_info, '<ul>', '</ul>', '<li>', '</li>', ' ');
}
if (method_exists($e, 'getMyFile')) {
$o .= '<div class="atk-effect-info">'.$e->getMyFile().':'.$e->getMyLine().'</div>';
}
if (method_exists($e, 'getMyTrace')) {
$o .= $this->backtrace(3, $e->getMyTrace());
} else {
$o .= $this->backtrace(@$e->shift, $e->getTrace());
}
if (isset($e->by_exception)) {
$o .= '<h3>This error was triggered by the following error:</h3>';
if ($e->by_exception instanceof self) {
$o .= $e->by_exception->getHTML();
} elseif ($e->by_exception instanceof Exception) {
$o .= $e->by_exception->getMessage();
}
}
$o .= '</div></div>';
return $o;
}
/**
* @return string
*/
public function getHeader()
{
return get_class($this).': '.htmlspecialchars($this->getMessage()).
($this->getCode() ? ' [code: '.$this->getCode().']' : '');
}
/**
* @return string
*/
public function getHTMLHeader()
{
return
"<div class='atk-layout-row atk-effect-danger atk-swatch-red'>".
"<div class='atk-wrapper atk-section-small atk-align-center'><h2>".
$this->getHeader().
"</h2>\n".
'</div></div>';
}
/**
* @return array
*/
public function getSolution()
{
return $this->actions;
}
/**
* @return string
*/
public function getHTMLSolution()
{
$solution = $this->getSolution();
if (empty($solution)) {
return '';
}
return
"<div class='atk-layout-row atk-effect-info'>".
"<div class='atk-wrapper atk-section-small atk-swatch-white atk-align-center'>".
"<h3>".$this->recommendation."</h3>".
$this->getHTMLActions().
'</div></div>';
}
/**
* @return string
*/
public function getHTMLActions()
{
$o = '';
foreach ($this->actions as $label => $url) {
$o .= "<a href='".$url."' class='atk-button atk-swatch-yellow'>".$label."</a>\n";
}
return $o;
}
/**
* Utility.
*
* @param array|object|string $key
* @param string $gs
* @param string $ge
* @param string $ls
* @param string $le
* @param string $ind
*
* @return null|string
*/
public function print_r($key, $gs, $ge, $ls, $le, $ind = ' ')
{
$o = '';
if (strlen($ind) > 3) {
return;
}
if (is_array($key)) {
$o = $gs;
foreach ($key as $a => $b) {
$o .= $ind.$ls.$a.': '.$this->print_r($b, $gs, $ge, $ls, $le, $ind.' ').$le;
}
$o .= $ge;
} elseif (is_object($key)) {
$o .= 'Object '.get_class($key);
} else {
$o .= $gs ? htmlspecialchars($key) : $key;
}
return $o;
}
/**
* Classes define a DOC constant which points to a on-line resource
* containing documentation for given class. This method will
* return full URL for the specified object.
*
* @param AbstractObject $o
*
* @return bool|string
*/
public function getDocURL($o)
{
if (!is_object($o)) {
return false;
}
if (!$o instanceof AbstractObject) {
return false;
}
/*$refl = new ReflectionClass($o);
$parent = $refl->getParentClass();
if($parent) {
// check to make sure property is overriden in child
$const = $parent->getConstants();
var_Dump($const);
if ($const['DOC'] == $o::DOC) return false;
}
*/
$url = $o::DOC;
if (substr($url, 0, 4) != 'http') {
return 'http://book.agiletoolkit.org/'.$url.'.html';
}
return $url;
}
/**
* @param int $sh
* @param array $backtrace
*
* @return string
*/
public function backtrace($sh = null, $backtrace = null)
{
$output = '<div class="atk-box-small atk-table atk-table-zebra">';
$output .= "<table>\n";
$output .= "<tr><th align='right'>File</th><th>Object Name</th><th>Stack Trace</th><th>Help</th></tr>";
if (!isset($backtrace)) {
$backtrace = debug_backtrace();
}
$sh -= 2;
$n = 0;
foreach ($backtrace as $bt) {
++$n;
$args = '';
if (!isset($bt['args'])) {
continue;
}
foreach ($bt['args'] as $a) {
if (!empty($args)) {
$args .= ', ';
}
switch (gettype($a)) {
case 'integer':
case 'double':
$args .= $a;
break;
case 'string':
$a = htmlspecialchars(substr($a, 0, 128)).((strlen($a) > 128) ? '...' : '');
$args .= "\"$a\"";
break;
case 'array':
$args .= 'Array('.count($a).')';
break;
case 'object':
$args .= 'Object('.get_class($a).')';
break;
case 'resource':
$args .= 'Resource('.strstr((string) $a, '#').')';
break;
case 'boolean':
$args .= $a ? 'True' : 'False';
break;
case 'NULL':
$args .= 'Null';
break;
default:
$args .= 'Unknown';
}
}
if (($sh == null && strpos($bt['file'], '/atk4/lib/') === false)
|| (!is_int($sh) && $bt['function'] == $sh)
) {
$sh = $n;
}
$doc = $this->getDocURL($bt['object']);
if ($doc) {
$doc .= '#'.get_class($bt['object']).'::'.$bt['function'];
}
$output .= '<tr><td valign=top align=right class=atk-effect-'.
($sh == $n ? 'danger' : 'info').'>'.htmlspecialchars(dirname($bt['file'])).'/'.
'<b>'.htmlspecialchars(basename($bt['file'])).'</b>';
$output .= ":{$bt['line']}</font> </td>";
$name = (!isset($bt['object']->name)) ? get_class($bt['object']) : $bt['object']->name;
if ($name) {
$output .= '<td>'.$name.'</td>';
} else {
$output .= '<td></td>';
}
$output .= '<td valign=top class=atk-effect-'.($sh == $n ? 'danger' : 'success').'>'.
get_class($bt['object'])."{$bt['type']}<b>{$bt['function']}</b>($args)</td>";
if ($doc) {
$output .= "<td><a href='".$doc."' target='_blank'><i class='icon-book'></i></a></td>";
} else {
$output .= '<td> </td>';
}
$output .= '</tr>';
}
$output .= "</table></div>\n";
return $output;
}
/**
* Returns Textual representation of the exception.
*
* @return string
*/
public function getText()
{
$more_info = $this->print_r($this->more_info, '[', ']', '', ',', ' ');
$text = get_class($this).': '.$this->getMessage().' ('.$more_info.')'.
' in '.$this->getMyFile().':'.$this->getMyLine();
return $text;
}
/**
* Redefine this function to add additional HTML output.
*
* @return string
*/
public function getDetailedHTML()
{
return '';
}
/**
* Undocumented.
*
* @todo Check this method, looks something useless. Optionally used only in Logger class.
*
* @return string
*/
public function getAdditionalMessage()
{
return $this->recommendation;
}
}