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.
@@ -6,60 +6,67 @@ require_once __DIR__ . "/../../../bootstrap.php";
6
6
 
7
7
  use App\Classes\Prisma\Prisma;
8
8
 
9
- if (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && $_SERVER["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest") {
9
+ header('Content-Type: application/json');
10
10
 
11
- $className = $_POST["className"] ?? "";
12
- $methodName = $_POST["methodName"] ?? "";
13
- $params = !empty($_POST["params"]) && is_string($_POST["params"]) ? json_decode($_POST["params"], true) : [];
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
- if (json_last_error() != JSON_ERROR_NONE) {
16
- echo "Error: Invalid JSON in params!";
17
- exit;
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
- // Append the namespace to the class name
21
- $fullClassName = "App\\Classes\\Prisma\\" . $className;
22
+ $params = null; // Initialize params as null
22
23
 
23
- if (!class_exists($fullClassName)) {
24
- echo "Error: Class $fullClassName not found!";
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
- $prisma = new Prisma();
29
- // Ensure that we correctly handle the case where $className might not directly map to a property.
30
- if (!property_exists($prisma, $className)) {
31
- echo "Error: Property $className not found in Prisma class!";
32
- exit;
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
- if (!method_exists($instance, $methodName)) {
37
- echo "Error: Method $methodName not found in class $fullClassName!";
38
- exit;
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
- // Check if $params is correctly structured for the expected arguments.
42
- // Adjust the logic here based on how $params should be passed to the method.
43
- // This example assumes $methodName expects an array of arguments.
44
- if (is_array($params)) {
45
- // Attempt to call the method with unpacked arguments.
46
- try {
47
- if (isset($params['identifier'], $params['data'])) {
48
- $result = $instance->$methodName($params['identifier'], $params['data']);
49
- } else {
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
- echo "Error: Parameters for $methodName method are not correctly formatted or missing.";
69
+ // If none of the specific keys are present, pass the whole $params array
70
+ return $instance->$methodName($params);
64
71
  }
65
72
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",