1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class ErrorController | ErrorController.php |
4
|
|
|
* |
5
|
|
|
* @package Faulancer\AbstractController |
6
|
|
|
* @author Florian Knapp <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
namespace Faulancer\Controller; |
9
|
|
|
|
10
|
|
|
use Faulancer\Exception\Exception; |
11
|
|
|
use Faulancer\Http\Request; |
12
|
|
|
use Faulancer\Service\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ErrorAbstractController |
16
|
|
|
*/ |
17
|
|
|
class ErrorController extends AbstractController |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
private $exception; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ErrorController constructor. |
24
|
|
|
* @param Request $request |
25
|
|
|
* @param \Exception $e |
26
|
|
|
* @codeCoverageIgnore |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Request $request, $e) |
29
|
|
|
{ |
30
|
|
|
parent::__construct($request); |
31
|
|
|
$this->exception = $e; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return \Faulancer\Http\Response |
36
|
|
|
* @codeCoverageIgnore |
37
|
|
|
*/ |
38
|
|
|
public function displayError() |
39
|
|
|
{ |
40
|
|
|
ob_end_clean(); |
41
|
|
|
|
42
|
|
|
if (getenv('APPLICATION_ENV') === 'development' || defined('APPLICATION_ENV') && APPLICATION_ENV === 'development') { |
43
|
|
|
return $this->renderDebugPage(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->renderNotFoundPage(); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Faulancer\Http\Response |
52
|
|
|
* @codeCoverageIgnore |
53
|
|
|
*/ |
54
|
|
|
private function renderDebugPage() |
55
|
|
|
{ |
56
|
|
|
$this->getView()->addStylesheet('/core/css/main.css'); |
57
|
|
|
$this->getView()->addScript('/core/js/namespace.js'); |
58
|
|
|
$this->getView()->addScript('/core/js/engine.js'); |
59
|
|
|
$this->getView()->setTemplatePath(__DIR__ . '/../../template'); |
60
|
|
|
|
61
|
|
|
$raiser = [ |
62
|
|
|
'function'=> !empty($this->exception->getTrace()[0]['function']) ? $this->exception->getTrace()[0]['function'] : 'unknown', |
63
|
|
|
'message' => $this->exception->getMessage(), |
64
|
|
|
'type' => $this->exception->getCode(), |
65
|
|
|
'file' => $this->exception->getFile(), |
66
|
|
|
'line' => $this->exception->getLine() |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
$trace = $this->exception->getTrace(); |
70
|
|
|
|
71
|
|
|
if (isset($trace[0]['line']) && $trace[0]['line'] !== $raiser['line']) { |
72
|
|
|
array_unshift($trace, $raiser); |
73
|
|
|
} else { |
74
|
|
|
array_shift($trace); |
75
|
|
|
array_unshift($trace, $raiser); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->render('/debug.phtml', ['exception' => $this->exception, 'trace' => $trace]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @codeCoverageIgnore |
83
|
|
|
*/ |
84
|
|
|
private function renderNotFoundPage() |
85
|
|
|
{ |
86
|
|
|
$errorController = $this->getServiceLocator()->get(Config::class)->get('customErrorController'); |
87
|
|
|
|
88
|
|
|
if ($errorController) { |
89
|
|
|
return (new $errorController($this->request))->notFoundAction(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
die(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.