create-prisma-php-app 1.1.3 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/app/api/api.php +52 -45
- package/package.json +1 -1
package/dist/src/app/api/api.php
CHANGED
|
@@ -6,60 +6,67 @@ require_once __DIR__ . "/../../../bootstrap.php";
|
|
|
6
6
|
|
|
7
7
|
use App\Classes\Prisma\Prisma;
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
header('Content-Type: application/json');
|
|
10
10
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
if (empty($_SERVER["HTTP_X_REQUESTED_WITH"]) || $_SERVER["HTTP_X_REQUESTED_WITH"] != "XMLHttpRequest") {
|
|
12
|
+
http_response_code(400); // Bad Request
|
|
13
|
+
echo json_encode(["error" => "This endpoint expects an XMLHttpRequest."]);
|
|
14
|
+
exit;
|
|
15
|
+
}
|
|
14
16
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
// Retrieve class name, method name, and params from POST data
|
|
18
|
+
$className = $_POST["className"] ?? "";
|
|
19
|
+
$methodName = $_POST["methodName"] ?? "";
|
|
20
|
+
$paramsJson = $_POST["params"] ?? '';
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
$fullClassName = "App\\Classes\\Prisma\\" . $className;
|
|
22
|
+
$params = null; // Initialize params as null
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
// Attempt to decode JSON only if paramsJson is not empty
|
|
25
|
+
if (!empty($paramsJson)) {
|
|
26
|
+
$params = json_decode($paramsJson, true);
|
|
27
|
+
if (json_last_error() !== JSON_ERROR_NONE) {
|
|
28
|
+
echo json_encode(['error' => 'Error: Invalid JSON in params!']);
|
|
25
29
|
exit;
|
|
26
30
|
}
|
|
31
|
+
}
|
|
27
32
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
$instance = $prisma->$className;
|
|
33
|
+
// Construct the full class name and check for class and property existence
|
|
34
|
+
$fullClassName = "App\\Classes\\Prisma\\" . $className;
|
|
35
|
+
if (!class_exists($fullClassName) || !property_exists(Prisma::class, $className)) {
|
|
36
|
+
echo json_encode(['error' => "Error: Class $fullClassName not found or property $className not found in Prisma class!"]);
|
|
37
|
+
exit;
|
|
38
|
+
}
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
// Create an instance of the class
|
|
41
|
+
$instance = (new Prisma())->$className;
|
|
42
|
+
|
|
43
|
+
// Check for method existence
|
|
44
|
+
if (!method_exists($instance, $methodName)) {
|
|
45
|
+
echo json_encode(['error' => "Error: Method $methodName not found in class $fullClassName!"]);
|
|
46
|
+
exit;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
// Call the method with params if they are provided and decoded; otherwise, call without params
|
|
51
|
+
$result = $params !== null ? call_user_method_with_params($instance, $methodName, $params) : $instance->$methodName();
|
|
52
|
+
// Encode and return the result as JSON
|
|
53
|
+
echo json_encode(['result' => $result instanceof \stdClass ? (array)$result : $result]);
|
|
54
|
+
} catch (\ArgumentCountError | \Exception $e) {
|
|
55
|
+
// Catch and return any errors during method execution
|
|
56
|
+
echo json_encode(['error' => "Error: " . $e->getMessage()]);
|
|
57
|
+
}
|
|
40
58
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
//
|
|
44
|
-
if (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
$result = $instance->$methodName($params);
|
|
51
|
-
}
|
|
52
|
-
if (is_object($result)) {
|
|
53
|
-
$result = (array) $result;
|
|
54
|
-
}
|
|
55
|
-
echo json_encode($result, JSON_INVALID_UTF8_IGNORE);
|
|
56
|
-
} catch (\ArgumentCountError $e) {
|
|
57
|
-
echo "Error: Incorrect number of arguments provided for $methodName method.";
|
|
58
|
-
} catch (\Exception $e) {
|
|
59
|
-
// Handle other potential errors here.
|
|
60
|
-
echo "Error: An error occurred while executing $methodName method. " . $e->getMessage();
|
|
61
|
-
}
|
|
59
|
+
function call_user_method_with_params($instance, $methodName, $params)
|
|
60
|
+
{
|
|
61
|
+
// Determine how to call the method based on the presence of specific keys in $params
|
|
62
|
+
if (isset($params['identifier'], $params['data'])) {
|
|
63
|
+
return $instance->$methodName($params['identifier'], $params['data']);
|
|
64
|
+
} elseif (isset($params['criteria'], $params['aggregates'])) {
|
|
65
|
+
return $instance->$methodName($params['criteria'], $params['aggregates']);
|
|
66
|
+
} elseif (isset($params['criteria'], $params['data'])) {
|
|
67
|
+
return $instance->$methodName($params['criteria'], $params['data']);
|
|
62
68
|
} else {
|
|
63
|
-
|
|
69
|
+
// If none of the specific keys are present, pass the whole $params array
|
|
70
|
+
return $instance->$methodName($params);
|
|
64
71
|
}
|
|
65
72
|
}
|