|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Faulancer\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Faulancer\Http\Request; |
|
6
|
|
|
use Faulancer\Http\Response; |
|
7
|
|
|
use Faulancer\Service\Config; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ErrorAbstractController |
|
11
|
|
|
* |
|
12
|
|
|
* @category ErrorController |
|
13
|
|
|
* @package Faulancer\Controller |
|
14
|
|
|
* @author Florian Knapp <[email protected]> |
|
15
|
|
|
* @license MIT License |
|
16
|
|
|
* @link not provided |
|
17
|
|
|
*/ |
|
18
|
|
|
class ErrorController extends Controller |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The exception object |
|
23
|
|
|
* |
|
24
|
|
|
* @var \Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
private $_exception; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* ErrorController constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param Request $request The request object |
|
32
|
|
|
* @param \Exception $e The given exception |
|
33
|
|
|
* |
|
34
|
|
|
* @codeCoverageIgnore |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Request $request, $e) |
|
37
|
|
|
{ |
|
38
|
|
|
parent::__construct($request); |
|
39
|
|
|
$this->_exception = $e; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Decide if debug output or 404 page should be rendered |
|
44
|
|
|
* |
|
45
|
|
|
* @return Response |
|
46
|
|
|
* |
|
47
|
|
|
* @codeCoverageIgnore |
|
48
|
|
|
*/ |
|
49
|
|
|
public function displayError() |
|
50
|
|
|
{ |
|
51
|
|
|
if (getenv('APPLICATION_ENV') === 'dev') { |
|
52
|
|
|
return $this->_renderDebugPage(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->_renderNotFoundPage(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Render the debug output |
|
60
|
|
|
* |
|
61
|
|
|
* @return Response |
|
62
|
|
|
* |
|
63
|
|
|
* @codeCoverageIgnore |
|
64
|
|
|
*/ |
|
65
|
|
|
private function _renderDebugPage() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->getView()->addStylesheet('/core/css/main.css'); |
|
68
|
|
|
$this->getView()->addScript('/core/js/namespace.js'); |
|
69
|
|
|
$this->getView()->addScript('/core/js/engine.js'); |
|
70
|
|
|
$this->getView()->setTemplatePath(__DIR__ . '/../../template'); |
|
71
|
|
|
|
|
72
|
|
|
$raiser = [ |
|
73
|
|
|
'function'=> $this->_exception->getTrace()[0]['function'] ?? 'unknown', |
|
74
|
|
|
'message' => $this->_exception->getMessage(), |
|
75
|
|
|
'type' => $this->_exception->getCode(), |
|
76
|
|
|
'file' => $this->_exception->getFile(), |
|
77
|
|
|
'line' => $this->_exception->getLine() |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$trace = $this->_exception->getTrace(); |
|
81
|
|
|
|
|
82
|
|
|
if (isset($trace[0]['line']) && $trace[0]['line'] !== $raiser['line']) { |
|
83
|
|
|
array_unshift($trace, $raiser); |
|
84
|
|
|
} else { |
|
85
|
|
|
array_shift($trace); |
|
86
|
|
|
array_unshift($trace, $raiser); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->render( |
|
90
|
|
|
'/debug.phtml', |
|
91
|
|
|
[ |
|
92
|
|
|
'exception' => $this->_exception, |
|
93
|
|
|
'trace' => $trace |
|
94
|
|
|
] |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Render a 404 page |
|
100
|
|
|
* |
|
101
|
|
|
* @return Response |
|
102
|
|
|
*/ |
|
103
|
|
|
private function _renderNotFoundPage() |
|
104
|
|
|
{ |
|
105
|
|
|
/** @var Config $config */ |
|
106
|
|
|
$config = $errorController = $this->getServiceLocator()->get(Config::class); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** @var ErrorControllerInterface $errorController */ |
|
109
|
|
|
$errorController = $config->get('customErrorController'); |
|
110
|
|
|
|
|
111
|
|
|
if (in_array(ErrorControllerInterface::class, class_implements($errorController) ?? [])) { |
|
112
|
|
|
return (new $errorController($this->request))->notFoundAction(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
die(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.