-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
137 lines (109 loc) · 3.33 KB
/
Copy pathindex.php
File metadata and controls
137 lines (109 loc) · 3.33 KB
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
133
134
135
136
137
<?php
require_once(__DIR__ . "/system/vendor/autoload.php");
use Library\Log;
set_exception_handler(function (Throwable $exception) {
Log::write(sprintf(
"Exceção: %s - Arquivo: %s - Linha: %s",
$exception->getMessage(),
$exception->getFile(),
$exception->getLine()
));
$_SESSION["INTERNAL_SITUATION"] = 500;
header("Location: /");
});
set_error_handler(function ($errorLevel, $errorMessage, $errorFile, $errorLine) {
if (error_reporting() === 0) {
return false;
}
switch ($errorLevel) {
case E_NOTICE:
case E_USER_NOTICE:
$error = "Notice";
break;
case E_WARNING:
case E_USER_WARNING:
$error = "Warning";
break;
case E_ERROR:
case E_USER_ERROR:
$error = "Fatal Error";
break;
default:
$error = "Unknown";
break;
}
Log::write(sprintf(
"%s: %s - Arquivo: %s - Linha: %s",
$error,
$errorMessage,
$errorFile,
$errorLine
));
return true;
});
function loadEnvironmentVariables()
{
if (!file_exists(__DIR__ . "/.env")) {
throw new Exception("Arquivo .env não encontrado no projeto!");
}
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required([
"DB_HOST",
"DB_NAME",
"DB_USER",
"DB_PASSWORD",
"SCHOOL_NAME"
])->notEmpty();
define("SCHOOL_NAME", $_ENV["SCHOOL_NAME"]);
}
function handleRoute()
{
$requestUri = $_SERVER["REQUEST_URI"];
$url = parse_url(trim($requestUri, "/"), PHP_URL_PATH);
$urlParts = explode("/", $url);
$path = [
"controller" => !empty($urlParts[0]) ? strtolower($urlParts[0]) : "home",
"class" => !empty($urlParts[1]) ? strtolower($urlParts[1]) : "index",
"method" => !empty($urlParts[2]) ? strtolower($urlParts[2]) : "index",
];
// Criar o controlador de forma dinâmica
$formattedPath = implode("/", [
"App/Controller",
ucfirst($path["controller"]),
ucfirst($path["class"]) . "Controller.php"
]);
// Verificar se o controlador existe
if (!file_exists($formattedPath)) {
header("Location: /");
}
$request = file_get_contents("php://input");
if (empty($_POST) && !empty($request)) {
$_POST = json_decode($request, true);
}
if (empty($_POST)) {
$path["method"] = "index";
}
include_once($formattedPath);
$controllerClass = implode("\\", [
"App\\Controller",
ucfirst($path["controller"]),
ucfirst($path["class"]) . "Controller"
]);
$controllerObject = new $controllerClass();
// Verificar se o método existe no controlador
if (method_exists($controllerObject, $path["method"])) {
if (!defined("SCHOOL_NAME")) {
define("SCHOOL_NAME", "Escola");
}
// Chamar o método do controlador
call_user_func(array($controllerObject, $path["method"]));
} else {
header("Location: /");
}
}
session_start();
if (!isset($_SESSION["INTERNAL_SITUATION"])) {
loadEnvironmentVariables();
}
handleRoute();