1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Mvc; |
4
|
|
|
|
5
|
|
|
use Win\Html\Seo\Title; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Paginas de Erro |
9
|
|
|
*/ |
10
|
|
|
class ErrorPage { |
11
|
|
|
|
12
|
|
|
/** @var string[] */ |
13
|
|
|
public static $pages = [ |
14
|
|
|
404 => 'Página não encontrada', |
15
|
|
|
401 => 'Não autorizado', |
16
|
|
|
403 => 'Acesso negado', |
17
|
|
|
500 => 'Erro no Servidor', |
18
|
|
|
503 => 'Problemas de Conexão' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Chama pageNotFound se o usuário acessar /404 |
23
|
|
|
* |
24
|
|
|
* Isso garante que todas as funcionalidades de pageNotFound serão executadas |
25
|
|
|
* mesmo se a página existente 404 for acessada |
26
|
|
|
*/ |
27
|
|
|
public static function validate() { |
28
|
|
|
if (key_exists((int) Application::app()->getParam(0), static::$pages)): |
29
|
|
|
ErrorPage::setError((int) Application::app()->getParam(0)); |
30
|
|
|
endif; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Define a página como Página de Erro |
35
|
|
|
* @param int $errorCode |
36
|
|
|
*/ |
37
|
|
|
public static function setError($errorCode) { |
38
|
|
|
$app = Application::app(); |
39
|
|
|
if (key_exists($errorCode, static::$pages)): |
40
|
|
|
static::stopControllerIf403($errorCode); |
41
|
|
|
$app->setPage((string) $errorCode); |
42
|
|
|
$app->view = new View($errorCode); |
43
|
|
|
|
44
|
|
|
$app->controller = ControllerFactory::create('Error' . $errorCode); |
45
|
|
|
$app->controller->setTitle(Title::otimize(static::$pages[$errorCode])); |
46
|
|
|
http_response_code($errorCode); |
47
|
|
|
$app->controller->reload(); |
48
|
|
|
endif; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Trava o carregamento do Controller, se ele definir um erro 403 |
53
|
|
|
* Isso evita que códigos sem permissões de acesso nunca sejam executados |
54
|
|
|
* @param int $errorCode |
55
|
|
|
* @codeCoverageIgnore |
56
|
|
|
*/ |
57
|
|
|
private static function stopControllerIf403($errorCode) { |
58
|
|
|
$app = Application::app(); |
59
|
|
|
if ($errorCode == 403 && $app->getParam(0) !== (string) $errorCode): |
60
|
|
|
$app->redirect(403 . '/index/' . $app->getUrl()); |
61
|
|
|
endif; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @return boolean */ |
65
|
|
|
public static function isErrorPage() { |
66
|
|
|
return (boolean) (key_exists((int) Application::app()->getPage(), static::$pages)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|