-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
129 lines (111 loc) · 3.38 KB
/
api.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
<?php
require 'vendor/autoload.php';
require_once __DIR__ . '/lib/db.php';
// Autoloader function to load the required class file based on the endpoint
spl_autoload_register(function ($className) {
$filePath = __DIR__ . "/lib/$className.php";
if (file_exists($filePath)) {
require_once $filePath;
} else {
try {
$myclass = file_get_contents("lib/Collection.php");
$myclass = preg_replace("/\%\%(.+?)\%\%/", $className, $myclass);
eval($myclass);
$test = new $className();
if (!$test) {
throw new Exception('Invalid collection');
}
} catch(e) {
http_response_code(404);
// Set headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
echo json_encode(["error" => "Class $className not found"]);
exit;
}
}
});
session_start();
// Parse request
$method = $_SERVER['REQUEST_METHOD'];
$actions = [];
if (isset($_SERVER['PATH_INFO'])) {
$actions = preg_split("/\//", $_SERVER['PATH_INFO']);
array_shift($actions);
$endpoint = ucfirst(array_shift($actions));
}
$id = $_GET['id'] ?? null;
$posted = json_decode(file_get_contents('php://input'), true);
// Set headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
// Verify that the endpoint class exists
if (!$endpoint || !class_exists($endpoint)) {
http_response_code(400);
echo json_encode(["error" => "Invalid endpoint"]);
exit;
}
// Instantiate the required class dynamically
$instance = new $endpoint();
if (count($actions)) {
foreach ($actions as $action) {
$action = preg_replace_callback("/\-(\w)/", function($m) {
return strtoupper($m[1]);
}, $action);
$out = $instance->$action($posted);
print json_encode($out);
exit;
}
}
// Handle CRUD operations based on HTTP method
switch ($method) {
case 'POST':
$data = $posted;
if ($data) {
$result = $instance->create($data);
echo json_encode($result);
} else {
http_response_code(400);
echo json_encode(["error" => "Invalid data"]);
}
break;
case 'GET':
// Read
if ($id) {
$result = $instance->read($id);
} else {
$result = $instance->read();
}
echo json_encode($result);
break;
case 'PUT':
// Update
if ($id) {
$data = json_decode(file_get_contents('php://input'), true);
if ($data) {
$result = $instance->update($id, $data);
echo json_encode($result);
} else {
http_response_code(400);
echo json_encode(["error" => "Invalid data"]);
}
} else {
http_response_code(400);
echo json_encode(["error" => "ID required"]);
}
break;
case 'DELETE':
// Delete
if ($id) {
$result = $instance->delete($id);
echo json_encode($result);
} else {
http_response_code(400);
echo json_encode(["error" => "ID required"]);
}
break;
default:
http_response_code(405);
echo json_encode(["error" => "Method not allowed"]);
break;
}