-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActions.php
132 lines (115 loc) · 3.76 KB
/
Actions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace App\Core\Server;
use App\Core\Application\Configuration;
use App\Core\Application\SharedConsts;
use App\Core\Framework\Classes\Strings;
use App\Core\Framework\Classes\LanguageManager;
use App\Core\Exceptions\AppException;
use App\Core\Exceptions\ViewException;
use LogicException;
use InvalidArgumentException;
use App\Core\Server\Logger;
class Actions
{
public static function redirect($URL)
{
header('location: ' . $URL);
}
public static function rootRedirect($URL)
{
header('location: //' . self::getRootURL() . $URL);
}
public static function printLocalized($key)
{
return LanguageManager::getInstance()->get($key);
}
public static function getDisplayLang()
{
if (Router::getUserLanguage() == "default") {
return Configuration::APP_LANG_DISPLAY;
} else {
return Router::getUserLanguage();
}
}
public static function requireView($route, ?array $Params = null)
{
if (!is_null($Params)) {
try {
foreach ($Params as $key => $value) {
$$key = $value;
}
} catch (\Exception $e) {
throw new LogicException($e->getMessage(), 0);
}
}
if (file_exists('Views/' . $route)) {
try {
require_once('Views/' . $route);
} catch (\Throwable $th) {
throw new ViewException($th->getMessage(), $th->getCode(), $th->getPrevious());
}
} else {
if (Configuration::LOG_ERRORS) {
Logger::LogError("IO Exception", self::printLocalized(Strings::VIEW_NOT_FOUND));
}
throw new InvalidArgumentException(self::printLocalized(Strings::VIEW_NOT_FOUND) . SharedConsts::SPACE . $route . "'", 404);
}
}
public static function requireController($route)
{
if (file_exists('App/Controllers/' . $route)) {
try {
require_once('App/Controllers/' . $route);
} catch (\Throwable $th) {
throw new AppException($th->getMessage(), $th->getCode(), $th->getPrevious());
}
} else {
if (Configuration::LOG_ERRORS) {
Logger::LogError("IO Exception", self::printLocalized(Strings::CONTROLLER_NOT_FOUND));
}
throw new InvalidArgumentException(self::printLocalized(Strings::CONTROLLER_NOT_FOUND) . $route . "'", 404);
}
}
public static function renderNotFound()
{
self::clearOutputBuffer();
http_response_code(404);
self::requireView(SharedConsts::PATH_VIEW_NOT_FOUND);
}
public static function renderError(int $code = 500)
{
self::clearOutputBuffer();
http_response_code($code);
self::requireView(SharedConsts::PATH_VIEW_ERROR);
}
public static function clearOutputBuffer()
{
if (ob_get_level() > 0) {
ob_end_clean();
}
}
public static function getRootURL()
{
return Configuration::LOCAL_ENVIRONMENT ? $_SERVER['SERVER_NAME'] . Configuration::PATH_URL : Configuration::PATH_URL;
}
public static function printScript($NombreArchivo)
{
return Configuration::APP_ROOT_PATH . self::getRootURL() . Configuration::RESOURCES_PATH . 'Scripts/' . $NombreArchivo . SharedConsts::STR_VERSION_PARAM . Configuration::APP_VERSION;
}
public static function printCSS($NombreArchivo)
{
return Configuration::APP_ROOT_PATH . self::getRootURL() . Configuration::RESOURCES_PATH . 'Styles/' . $NombreArchivo . SharedConsts::STR_VERSION_PARAM . Configuration::APP_VERSION;
}
public static function printResource($Route, $printVersion = false)
{
return Configuration::APP_ROOT_PATH . self::getRootURL() . Configuration::RESOURCES_PATH . $Route . ($printVersion ? SharedConsts::STR_VERSION_PARAM . Configuration::APP_VERSION : "");
}
public static function printFile($Route, $printVersion = false)
{
return Configuration::APP_ROOT_PATH . self::getRootURL() . "Files/{$Route}" . ($printVersion ? SharedConsts::STR_VERSION_PARAM . Configuration::APP_VERSION : "");
}
public static function printRoute(?string $Route = null)
{
return Configuration::APP_ROOT_PATH . self::getRootURL() . $Route;
}
}