|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\ErrorReporter; |
|
4
|
|
|
|
|
5
|
|
|
use Core\ErrorReporter\ErrorReporterService; |
|
6
|
|
|
use Core\View\ViewEngineInterface; |
|
7
|
|
|
use Core\ErrorReporter\ErrorReporterInterface; |
|
8
|
|
|
|
|
9
|
|
|
class Html implements ErrorReporterInterface |
|
10
|
|
|
{ |
|
11
|
|
|
private $settings = []; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($settings = []) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->settings = $settings; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/* |
|
19
|
|
|
$exception is not Exception type. |
|
20
|
|
|
because $exception can be Throwable type in PHP 7 |
|
21
|
|
|
*/ |
|
22
|
|
|
public function report($exception) |
|
23
|
|
|
{ |
|
24
|
|
|
$view = $this->constructViewInstance(); |
|
25
|
|
|
$view->file($this->settings['file']); |
|
26
|
|
|
$this->setReportVariables($view, $exception); |
|
27
|
|
|
|
|
28
|
|
|
return $view->render(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return ViewEngineInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private function constructViewInstance() |
|
35
|
|
|
{ |
|
36
|
|
|
$viewClassName = $this->settings['view']; |
|
37
|
|
|
$view = new $viewClassName($this->settings); |
|
38
|
|
|
|
|
39
|
|
|
return $view; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function setReportVariables(ViewEngineInterface $view, $exception) |
|
43
|
|
|
{ |
|
44
|
|
|
$view->set('message', $exception->getMessage()); |
|
45
|
|
|
$view->set('file', $exception->getFile()); |
|
46
|
|
|
$view->set('line', $exception->getLine()); |
|
47
|
|
|
$view->set('traces', $exception->getTrace()); |
|
48
|
|
|
|
|
49
|
|
|
$className = get_class($exception); |
|
50
|
|
|
$view->set('errorName', $className); |
|
51
|
|
|
$view->set('errorNameWithoutNamespace', substr($className, strrpos($className, '\\') ? strrpos($className, '\\') + 1 : 0)); |
|
52
|
|
|
|
|
53
|
|
|
$view->set('displayExceptionInfo', $this->settings['displayExceptionInfo']); |
|
54
|
|
|
$view->set('displayFileInfo', $this->settings['displayFileInfo']); |
|
55
|
|
|
$view->set('displayErrorSourceLines', $this->settings['displayErrorSourceLines']); |
|
56
|
|
|
$view->set('errorSourceLines', ErrorReporterService::getErrorCodeLine($exception)); |
|
57
|
|
|
$view->set('displayStackTrace', $this->settings['displayStackTrace']); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|