Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
ExceptionHandler | |
100.00% |
25 / 25 |
|
100.00% |
7 / 7 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
addException | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
hasExceptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExceptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepare | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
prepareHeaderAsString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
prepareAsString | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Debug\Handler; |
15 | |
16 | /** |
17 | * Debug exception handler class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Debug |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 2.0.0 |
25 | */ |
26 | class ExceptionHandler extends AbstractHandler |
27 | { |
28 | |
29 | /** |
30 | * Verbose flag |
31 | * @var bool |
32 | */ |
33 | protected bool $verbose = false; |
34 | |
35 | /** |
36 | * Exceptions |
37 | * @var array |
38 | */ |
39 | protected array $exceptions = []; |
40 | |
41 | /** |
42 | * Constructor |
43 | * |
44 | * Instantiate a handler object |
45 | * |
46 | * @param bool $verbose |
47 | * @param ?string $name |
48 | */ |
49 | public function __construct(bool $verbose = false, ?string $name = null) |
50 | { |
51 | parent::__construct($name); |
52 | $this->verbose = $verbose; |
53 | } |
54 | |
55 | /** |
56 | * Add exception |
57 | * |
58 | * @param \Exception $exception |
59 | * @return ExceptionHandler |
60 | */ |
61 | public function addException(\Exception $exception): ExceptionHandler |
62 | { |
63 | $this->exceptions[(string)microtime(true)] = $exception; |
64 | return $this; |
65 | } |
66 | |
67 | /** |
68 | * Determine if the handler has exceptions |
69 | * |
70 | * @return bool |
71 | */ |
72 | public function hasExceptions(): bool |
73 | { |
74 | return (count($this->exceptions) > 0); |
75 | } |
76 | |
77 | /** |
78 | * Get exceptions |
79 | * |
80 | * @return array |
81 | */ |
82 | public function getExceptions(): array |
83 | { |
84 | return $this->exceptions; |
85 | } |
86 | |
87 | /** |
88 | * Prepare handler data for storage |
89 | * |
90 | * @return array |
91 | */ |
92 | public function prepare(): array |
93 | { |
94 | $data = []; |
95 | |
96 | foreach ($this->exceptions as $time => $exception) { |
97 | $data[number_format($time, 5, '.', '')] = $exception; |
98 | } |
99 | |
100 | return $data; |
101 | } |
102 | |
103 | /** |
104 | * Prepare header string |
105 | * |
106 | * @return string |
107 | */ |
108 | public function prepareHeaderAsString(): string |
109 | { |
110 | $string = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Exception Handler'; |
111 | $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL; |
112 | |
113 | return $string; |
114 | } |
115 | |
116 | /** |
117 | * Prepare handler data as string |
118 | * |
119 | * @return string |
120 | */ |
121 | public function prepareAsString(): string |
122 | { |
123 | $string = ''; |
124 | |
125 | foreach ($this->exceptions as $time => $exception) { |
126 | if ($this->verbose) { |
127 | $string .= number_format($time, 5, '.', '') . |
128 | "\tCode: " . $exception->getCode() . |
129 | "\tLine: " . $exception->getLine() . |
130 | "\t" . $exception->getFile() . |
131 | "\t" . $exception->getMessage() . |
132 | "\t" . $exception->getTraceAsString() . PHP_EOL . PHP_EOL; |
133 | } else { |
134 | $string .= number_format($time, 5, '.', '') . "\t" . $exception->getMessage() . PHP_EOL; |
135 | } |
136 | |
137 | } |
138 | $string .= PHP_EOL; |
139 | |
140 | return $string; |
141 | } |
142 | |
143 | } |