1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Dispatcher; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Phalcon\Dispatcher\Exception as DispatchException; |
16
|
|
|
use Zemit\Di\Injectable; |
17
|
|
|
use Phalcon\Events\Event; |
18
|
|
|
use Zemit\Mvc\Dispatcher; |
19
|
|
|
|
20
|
|
|
class Error extends Injectable |
21
|
|
|
{ |
22
|
|
|
public array $defaultNotFoundRoute = [ |
23
|
|
|
'module' => null, |
24
|
|
|
'namespace' => null, |
25
|
|
|
'controller' => 'error', |
26
|
|
|
'action' => 'notFound', |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
public array $defaultErrorRoute = [ |
30
|
|
|
'module' => null, |
31
|
|
|
'namespace' => null, |
32
|
|
|
'controller' => 'error', |
33
|
|
|
'action' => 'fatal', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Forward to 404 or 500 error controller |
38
|
|
|
* @throws Exception |
39
|
|
|
*/ |
40
|
|
|
public function beforeException(Event $event, Dispatcher $dispatcher, Exception $exception): bool |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
switch ($exception->getCode()) { |
43
|
|
|
|
44
|
|
|
case DispatchException::EXCEPTION_HANDLER_NOT_FOUND: |
45
|
|
|
case DispatchException::EXCEPTION_ACTION_NOT_FOUND: |
46
|
|
|
if ($exception instanceof DispatchException) { |
47
|
|
|
$route = $this->config->pathToArray('router.notFound') ?? []; |
48
|
|
|
|
49
|
|
|
$this->appendDefaultToRoute($route, $this->defaultNotFoundRoute); |
50
|
|
|
$route['params']['exception'] = $exception; |
51
|
|
|
|
52
|
|
|
$dispatcher->forward($route, true); |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
default: |
58
|
|
|
http_response_code(500); |
59
|
|
|
|
60
|
|
|
// Everything else, if debug is false, forward to fatal error 500 |
61
|
|
|
$appDebug = $this->config->path('app.debug', false); |
62
|
|
|
$debugEnable = $this->config->path('debug.enable', false); |
63
|
|
|
|
64
|
|
|
if (!$appDebug && !$debugEnable) { |
65
|
|
|
$route = $this->config->pathToArray('router.error') ?? []; |
66
|
|
|
|
67
|
|
|
$this->appendDefaultToRoute($route, $this->defaultErrorRoute); |
68
|
|
|
$route['params']['exception'] = $exception; |
69
|
|
|
|
70
|
|
|
$dispatcher->forward($route, true); |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
throw $exception; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function appendDefaultToRoute(array $route, array $default): array |
79
|
|
|
{ |
80
|
|
|
$route['module'] ??= $default['module'] ?? null; |
81
|
|
|
$route['namespace'] ??= $default['namespace'] ?? null; |
82
|
|
|
$route['controller'] ??= $default['controller'] ?? null; |
83
|
|
|
$route['action'] ??= $default['action'] ?? null; |
84
|
|
|
return $route; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.