-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
44 lines (30 loc) · 1.34 KB
/
index.php
File metadata and controls
44 lines (30 loc) · 1.34 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
<?php
declare(strict_types=1);
$isLive = str_contains(__DIR__, "/public_html/") ? true : false;
$localPathArr = $isLive ? explode('/', trim(__DIR__, '/')) : explode('\\', trim(__DIR__, '\\'));
$folderPath = ($isLive || str_contains(__DIR__, "\\htdocs\\")) ? "/".end($localPathArr) : "";
define("API_LOCATION", $folderPath);
require __DIR__ . "/api/Model/Exception.php";
require __DIR__ . "/api/ExceptionHandler/ErrorHandler.php";
require __DIR__ . "/api/DataLayer/Database.php";
spl_autoload_register(function ($class) {
if(str_contains($class, "Gateway"))
require __DIR__ . "/api/Gateway/$class.php";
else
require __DIR__ . "/api/Controller/$class.php";
});
set_error_handler("ErrorHandler::handleError");
set_exception_handler("ErrorHandler::handleException");
header("Content-type: application/json; charset=UTF-8");
$parts = explode("/", str_replace(API_LOCATION, "", $_SERVER["REQUEST_URI"]));
if (count($parts) > 3 && $parts[3] != "products") {
http_response_code(404);
echo json_encode(["code" => 404, "message" => "Resource not found"]);
exit;
}
$id = count($parts) > 4 ? $parts[4] ?? null : null;
$database = new Database("localhost", "product_db", "root", "");
$gateway = new ProductGateway($database);
$controller = new ProductController($gateway);
$controller->processRequest($_SERVER["REQUEST_METHOD"], $id);
?>