create-prisma-php-app 4.0.0-alpha.2 → 4.0.0-alpha.21

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.
Files changed (59) hide show
  1. package/dist/.htaccess +54 -41
  2. package/dist/bootstrap.php +143 -98
  3. package/dist/index.js +264 -99
  4. package/dist/settings/auto-swagger-docs.ts +196 -95
  5. package/dist/settings/bs-config.ts +56 -58
  6. package/dist/settings/files-list.json +1 -1
  7. package/dist/settings/restart-mcp.ts +58 -0
  8. package/dist/settings/restart-websocket.ts +51 -45
  9. package/dist/settings/utils.ts +240 -0
  10. package/dist/src/Lib/AI/ChatGPTClient.php +147 -0
  11. package/dist/src/Lib/Auth/Auth.php +544 -0
  12. package/dist/src/Lib/Auth/AuthConfig.php +89 -0
  13. package/dist/src/Lib/CacheHandler.php +121 -0
  14. package/dist/src/Lib/ErrorHandler.php +322 -0
  15. package/dist/src/Lib/FileManager/UploadFile.php +383 -0
  16. package/dist/src/Lib/Headers/Boom.php +192 -0
  17. package/dist/src/Lib/IncludeTracker.php +59 -0
  18. package/dist/src/Lib/MCP/WeatherTools.php +104 -0
  19. package/dist/src/Lib/MCP/mcp-server.php +80 -0
  20. package/dist/src/Lib/MainLayout.php +230 -0
  21. package/dist/src/Lib/Middleware/AuthMiddleware.php +154 -0
  22. package/dist/src/Lib/Middleware/CorsMiddleware.php +145 -0
  23. package/dist/src/Lib/PHPMailer/Mailer.php +169 -0
  24. package/dist/src/Lib/PHPX/Exceptions/ComponentValidationException.php +49 -0
  25. package/dist/src/Lib/PHPX/Fragment.php +32 -0
  26. package/dist/src/Lib/PHPX/IPHPX.php +22 -0
  27. package/dist/src/Lib/PHPX/PHPX.php +287 -0
  28. package/dist/src/Lib/PHPX/TemplateCompiler.php +641 -0
  29. package/dist/src/Lib/PHPX/TwMerge.php +346 -0
  30. package/dist/src/Lib/PHPX/TypeCoercer.php +490 -0
  31. package/dist/src/Lib/PartialRenderer.php +40 -0
  32. package/dist/src/Lib/PrismaPHPSettings.php +181 -0
  33. package/dist/src/Lib/Request.php +479 -0
  34. package/dist/src/Lib/Security/RateLimiter.php +33 -0
  35. package/dist/src/Lib/Set.php +102 -0
  36. package/dist/src/Lib/StateManager.php +127 -0
  37. package/dist/src/Lib/Validator.php +752 -0
  38. package/dist/src/{Websocket → Lib/Websocket}/ConnectionManager.php +1 -1
  39. package/dist/src/Lib/Websocket/websocket-server.php +118 -0
  40. package/dist/src/app/error.php +1 -1
  41. package/dist/src/app/index.php +24 -5
  42. package/dist/src/app/js/index.js +1 -1
  43. package/dist/src/app/layout.php +2 -2
  44. package/package.json +1 -1
  45. package/dist/settings/restart-websocket.bat +0 -28
  46. package/dist/src/app/assets/images/prisma-php-black.svg +0 -6
  47. package/dist/websocket-server.php +0 -22
  48. package/vendor/autoload.php +0 -25
  49. package/vendor/composer/ClassLoader.php +0 -579
  50. package/vendor/composer/InstalledVersions.php +0 -359
  51. package/vendor/composer/LICENSE +0 -21
  52. package/vendor/composer/autoload_classmap.php +0 -10
  53. package/vendor/composer/autoload_namespaces.php +0 -9
  54. package/vendor/composer/autoload_psr4.php +0 -10
  55. package/vendor/composer/autoload_real.php +0 -38
  56. package/vendor/composer/autoload_static.php +0 -25
  57. package/vendor/composer/installed.json +0 -825
  58. package/vendor/composer/installed.php +0 -132
  59. package/vendor/composer/platform_check.php +0 -26
@@ -0,0 +1,383 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib\FileManager;
6
+
7
+ class UploadFile
8
+ {
9
+ protected string $destination = '';
10
+ protected array $messages = [];
11
+ protected array $errorCode = [];
12
+ protected array $successfulUploads = [];
13
+ protected int $maxSize = 51200; // 50KB default
14
+ protected array $permittedTypes = [
15
+ 'image/jpeg',
16
+ 'image/pjpeg',
17
+ 'image/gif',
18
+ 'image/png',
19
+ 'image/webp'
20
+ ];
21
+ protected string $newName = '';
22
+ protected bool $typeCheckingOn = true;
23
+ protected array $notTrusted = ['bin', 'cgi', 'exe', 'js', 'pl', 'php', 'py', 'sh'];
24
+ protected string $suffix = '.upload';
25
+ protected bool $renameDuplicates = true;
26
+
27
+ /**
28
+ * Constructor for the UploadFile class.
29
+ *
30
+ * @param string $uploadFolder The folder to which uploaded files will be moved.
31
+ * @throws \Exception If the upload folder is not a valid, writable folder.
32
+ */
33
+ public function __construct(string $uploadFolder)
34
+ {
35
+ if (!is_dir($uploadFolder) || !is_writable($uploadFolder)) {
36
+ throw new \Exception("$uploadFolder must be a valid, writable folder.");
37
+ }
38
+ // Ensure the folder ends with a '/'
39
+ $this->destination = rtrim($uploadFolder, '/') . '/';
40
+ }
41
+
42
+ /**
43
+ * Sets the maximum size for uploaded files.
44
+ *
45
+ * @param int $bytes The maximum size in bytes.
46
+ * @return void
47
+ */
48
+ public function setMaxSize(int $bytes): void
49
+ {
50
+ $serverMax = self::convertToBytes(ini_get('upload_max_filesize'));
51
+ if ($bytes > $serverMax) {
52
+ throw new \Exception('Maximum size cannot exceed server limit for individual files: ' . self::convertFromBytes($serverMax));
53
+ }
54
+ if ($bytes > 0) {
55
+ $this->maxSize = $bytes;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Converts a string value representing a file size to bytes.
61
+ *
62
+ * @param string $val The string value representing the file size.
63
+ * @return int The file size in bytes.
64
+ */
65
+ public static function convertToBytes(string $val): int
66
+ {
67
+ $val = trim($val);
68
+ $last = strtolower($val[strlen($val) - 1]);
69
+ $multiplier = match ($last) {
70
+ 'g' => 1024 * 1024 * 1024,
71
+ 'm' => 1024 * 1024,
72
+ 'k' => 1024,
73
+ default => 1,
74
+ };
75
+ return (int) $val * $multiplier;
76
+ }
77
+
78
+ /**
79
+ * Converts the given number of bytes to a human-readable string representation.
80
+ *
81
+ * @param int $bytes The number of bytes to convert.
82
+ * @return string The human-readable string representation of the converted bytes.
83
+ */
84
+ public static function convertFromBytes(int $bytes): string
85
+ {
86
+ return $bytes >= 1024 * 1024
87
+ ? number_format($bytes / (1024 * 1024), 1) . ' MB'
88
+ : number_format($bytes / 1024, 1) . ' KB';
89
+ }
90
+
91
+ /**
92
+ * Disable type checking and set the allowed file types.
93
+ *
94
+ * @param string|null $suffix The file suffix to allow. If null, the current suffix will be used.
95
+ * @return void
96
+ */
97
+ public function allowAllTypes(?string $suffix = null): void
98
+ {
99
+ $this->typeCheckingOn = false;
100
+ $this->suffix = $suffix ? (strpos($suffix, '.') === 0 ? $suffix : ".$suffix") : $this->suffix;
101
+ }
102
+
103
+ /**
104
+ * Uploads file(s) to the server.
105
+ *
106
+ * @param bool $renameDuplicates (optional) Whether to rename duplicate files. Default is true.
107
+ * @return void
108
+ */
109
+ public function upload(bool $renameDuplicates = true): void
110
+ {
111
+ $this->renameDuplicates = $renameDuplicates;
112
+ if (empty($_FILES) || !is_array(current($_FILES))) {
113
+ // No file was uploaded or the structure is invalid, handle this as an error
114
+ $this->messages[] = "No files were uploaded.";
115
+ $this->errorCode[] = UPLOAD_ERR_NO_FILE;
116
+ return;
117
+ }
118
+
119
+ $uploaded = current($_FILES);
120
+
121
+ // Handle single and multiple file uploads using a unified approach
122
+ $files = is_array($uploaded['name']) ? $this->rearrangeFilesArray($uploaded) : [$uploaded];
123
+
124
+ foreach ($files as $file) {
125
+ if ($this->checkFile($file)) {
126
+ $this->moveFile($file);
127
+ }
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Updates an existing file by deleting the old file and uploading the new one, using the old file's name.
133
+ *
134
+ * @param array $file The new file information from $_FILES.
135
+ * @param string $oldFilename The name of the file to be replaced.
136
+ * @return bool True if the update was successful, false otherwise.
137
+ */
138
+ public function update(array $file, string $oldFilename): bool
139
+ {
140
+ // First, delete the old file
141
+ if (!$this->delete($oldFilename)) {
142
+ $this->messages[] = "Failed to delete the old file $oldFilename. Update aborted.";
143
+ $this->errorCode[] = UPLOAD_ERR_CANT_WRITE; // Error code for failure to update
144
+ return false;
145
+ }
146
+
147
+ // Now proceed to upload the new file with the old filename
148
+ if ($this->checkFile($file)) {
149
+ // Set the new file name to match the old file's name
150
+ $this->newName = $oldFilename;
151
+ $this->moveFile($file);
152
+ return true;
153
+ }
154
+
155
+ return false;
156
+ }
157
+
158
+ /**
159
+ * Renames a file in the destination folder.
160
+ *
161
+ * @param string $oldName The current name of the file.
162
+ * @param string $newName The new name for the file.
163
+ * @return bool True if the rename was successful, false otherwise.
164
+ */
165
+ public function rename(string $oldName, string $newName): bool
166
+ {
167
+ $oldPath = $this->destination . $oldName;
168
+
169
+ // Extract the file extension from the old file
170
+ $extension = pathinfo($oldName, PATHINFO_EXTENSION);
171
+
172
+ // Add the extension to the new name
173
+ $newNameWithExtension = str_replace(' ', '_', $newName) . '.' . $extension;
174
+ $newPath = $this->destination . $newNameWithExtension;
175
+
176
+ // Check if the file exists
177
+ if (!file_exists($oldPath)) {
178
+ $this->messages[] = "File $oldName does not exist.";
179
+ $this->errorCode[] = UPLOAD_ERR_NO_FILE; // Error code for file not found
180
+ return false;
181
+ }
182
+
183
+ // Validate that the new name doesn't already exist
184
+ if (file_exists($newPath)) {
185
+ $this->messages[] = "A file with the name $newNameWithExtension already exists.";
186
+ $this->errorCode[] = UPLOAD_ERR_CANT_WRITE; // Error code for name conflict
187
+ return false;
188
+ }
189
+
190
+ // Attempt to rename the file
191
+ if (rename($oldPath, $newPath)) {
192
+ $this->messages[] = "File $oldName renamed successfully to $newNameWithExtension";
193
+ $this->errorCode[] = 0; // Success code
194
+ return true;
195
+ } else {
196
+ $this->messages[] = "Failed to rename $oldName to $newNameWithExtension";
197
+ $this->errorCode[] = UPLOAD_ERR_CANT_WRITE; // Error code for rename failure
198
+ return false;
199
+ }
200
+ }
201
+
202
+ /**
203
+ * Deletes a file from the destination folder.
204
+ *
205
+ * @param string $filename The name of the file to delete.
206
+ * @return bool True if the file was deleted, false otherwise.
207
+ */
208
+ public function delete(string $filename): bool
209
+ {
210
+ $filePath = $this->destination . $filename;
211
+
212
+ if (!file_exists($filePath)) {
213
+ $this->messages[] = "File $filename does not exist.";
214
+ $this->errorCode[] = UPLOAD_ERR_NO_FILE; // Error code for file not found
215
+ return false;
216
+ }
217
+
218
+ if (unlink($filePath)) {
219
+ $this->messages[] = "File $filename deleted successfully.";
220
+ $this->errorCode[] = 0; // Success code
221
+ return true;
222
+ } else {
223
+ $this->messages[] = "Failed to delete $filename.";
224
+ $this->errorCode[] = UPLOAD_ERR_CANT_WRITE; // Error code for failure to delete
225
+ return false;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Retrieves the messages associated with the file upload.
231
+ *
232
+ * @return array The array of messages.
233
+ */
234
+ public function getMessages(): array
235
+ {
236
+ return $this->messages;
237
+ }
238
+
239
+ /**
240
+ * Retrieves the error codes associated with the file upload.
241
+ *
242
+ * @return array The array of error codes.
243
+ */
244
+ public function getErrorCode(): array
245
+ {
246
+ return $this->errorCode;
247
+ }
248
+
249
+ protected function checkFile(array $file): bool
250
+ {
251
+ if ($file['error'] !== UPLOAD_ERR_OK) {
252
+ $this->getErrorMessage($file);
253
+ return false;
254
+ }
255
+ if (!$this->checkSize($file) || ($this->typeCheckingOn && !$this->checkType($file))) {
256
+ return false;
257
+ }
258
+ $this->checkName($file);
259
+ return true;
260
+ }
261
+
262
+ protected function getErrorMessage(array $file): void
263
+ {
264
+ $errorMessages = [
265
+ UPLOAD_ERR_INI_SIZE => $file['name'] . ' exceeds the maximum size: ' . self::convertFromBytes($this->maxSize),
266
+ UPLOAD_ERR_FORM_SIZE => $file['name'] . ' exceeds the form limit.',
267
+ UPLOAD_ERR_PARTIAL => $file['name'] . ' was only partially uploaded.',
268
+ UPLOAD_ERR_NO_FILE => 'No file submitted.',
269
+ ];
270
+
271
+ $this->errorCode[] = $file['error'];
272
+ $this->messages[] = $errorMessages[$file['error']] ?? 'Problem uploading ' . $file['name'];
273
+ }
274
+
275
+ protected function checkSize(array $file): bool
276
+ {
277
+ if ($file['size'] == 0) {
278
+ $this->messages[] = $file['name'] . ' is empty.';
279
+ $this->errorCode[] = UPLOAD_ERR_NO_FILE; // Log an error code for empty files
280
+ return false;
281
+ }
282
+ if ($file['size'] > $this->maxSize) {
283
+ $this->messages[] = $file['name'] . ' exceeds the maximum size: ' . self::convertFromBytes($this->maxSize);
284
+ $this->errorCode[] = UPLOAD_ERR_INI_SIZE; // Log an error code for exceeding size
285
+ return false;
286
+ }
287
+ return true;
288
+ }
289
+
290
+ protected function checkType(array $file): bool
291
+ {
292
+ if (!in_array($file['type'], $this->permittedTypes)) {
293
+ $this->messages[] = $file['name'] . ' is not a permitted type.';
294
+ $this->errorCode[] = UPLOAD_ERR_EXTENSION; // Log an error code for invalid file type
295
+ return false;
296
+ }
297
+ return true;
298
+ }
299
+
300
+ protected function checkName(array $file): void
301
+ {
302
+ $this->newName = '';
303
+ $noSpaces = str_replace(' ', '_', $file['name']);
304
+ if ($noSpaces != $file['name']) {
305
+ $this->newName = $noSpaces;
306
+ }
307
+ $nameParts = pathinfo($noSpaces);
308
+ $extension = $nameParts['extension'] ?? '';
309
+ if (!$this->typeCheckingOn && !empty($this->suffix)) {
310
+ if (in_array($extension, $this->notTrusted) || empty($extension)) {
311
+ $this->newName = $noSpaces . $this->suffix;
312
+ }
313
+ }
314
+ if ($this->renameDuplicates) {
315
+ $name = isset($this->newName) ? $this->newName : $file['name'];
316
+ $existing = scandir($this->destination);
317
+ if (in_array($name, $existing)) {
318
+ $i = 1;
319
+ do {
320
+ $this->newName = $nameParts['filename'] . '_' . $i++;
321
+ if (!empty($extension)) {
322
+ $this->newName .= ".$extension";
323
+ }
324
+ if (in_array($extension, $this->notTrusted)) {
325
+ $this->newName .= $this->suffix;
326
+ }
327
+ } while (in_array($this->newName, $existing));
328
+ }
329
+ }
330
+ }
331
+
332
+ protected function moveFile(array $file): void
333
+ {
334
+ // Ensure the newName is set or fallback to the original file name
335
+ $filename = $this->newName ?: $file['name'];
336
+ $destination = $this->destination . $filename;
337
+ $success = move_uploaded_file($file['tmp_name'], $destination);
338
+
339
+ if ($success) {
340
+ $message = "{$file['name']} uploaded successfully.";
341
+ if ($this->newName && $this->newName !== $file['name']) {
342
+ $message .= " Renamed to {$this->newName}";
343
+ }
344
+
345
+ $this->successfulUploads[] = [
346
+ 'original' => $file['name'],
347
+ 'final' => $filename
348
+ ];
349
+ } else {
350
+ $message = "Failed to upload {$file['name']}.";
351
+ }
352
+
353
+ $this->messages[] = $message;
354
+ // Add a success/error code for file move
355
+ $this->errorCode[] = $success ? 0 : UPLOAD_ERR_CANT_WRITE; // 0 for success, error code for failure
356
+ }
357
+
358
+ // Utility function to restructure the $_FILES array for multiple uploads
359
+ protected function rearrangeFilesArray(array $files): array
360
+ {
361
+ $rearranged = [];
362
+ foreach ($files['name'] as $key => $name) {
363
+ $rearranged[] = [
364
+ 'name' => $files['name'][$key],
365
+ 'type' => $files['type'][$key],
366
+ 'tmp_name' => $files['tmp_name'][$key],
367
+ 'error' => $files['error'][$key],
368
+ 'size' => $files['size'][$key],
369
+ ];
370
+ }
371
+ return $rearranged;
372
+ }
373
+
374
+ /**
375
+ * Retrieves the successfully uploaded file names.
376
+ *
377
+ * @return array An array of arrays containing 'original' and 'final' file names.
378
+ */
379
+ public function getSuccessfulUploads(): array
380
+ {
381
+ return $this->successfulUploads;
382
+ }
383
+ }
@@ -0,0 +1,192 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib\Headers;
6
+
7
+ use InvalidArgumentException;
8
+ use JsonException;
9
+
10
+ /**
11
+ * HTTP‑error helper.
12
+ *
13
+ * @method static self badRequest(string $message = 'Bad Request', array $details = [])
14
+ * @method static self unauthorized(string $message = 'Unauthorized', array $details = [])
15
+ * @method static self paymentRequired(string $message = 'Payment Required', array $details = [])
16
+ * @method static self forbidden(string $message = 'Forbidden', array $details = [])
17
+ * @method static self notFound(string $message = 'Not Found', array $details = [])
18
+ * @method static self methodNotAllowed(string $message = 'Method Not Allowed', array $details = [])
19
+ * @method static self notAcceptable(string $message = 'Not Acceptable', array $details = [])
20
+ * @method static self conflict(string $message = 'Conflict', array $details = [])
21
+ * @method static self gone(string $message = 'Gone', array $details = [])
22
+ * @method static self lengthRequired(string $message = 'Length Required', array $details = [])
23
+ * @method static self preconditionFailed(string $message = 'Precondition Failed', array $details = [])
24
+ * @method static self payloadTooLarge(string $message = 'Payload Too Large', array $details = [])
25
+ * @method static self uriTooLarge(string $message = 'URI Too Large', array $details = [])
26
+ * @method static self unsupportedMediaType(string $message = 'Unsupported Media Type', array $details = [])
27
+ * @method static self rangeNotSatisfiable(string $message = 'Range Not Satisfiable', array $details = [])
28
+ * @method static self expectationFailed(string $message = 'Expectation Failed', array $details = [])
29
+ * @method static self iAmATeapot(string $message = "I'm a teapot", array $details = [])
30
+ * @method static self misdirectedRequest(string $message = 'Misdirected Request', array $details = [])
31
+ * @method static self unprocessableEntity(string $message = 'Unprocessable Entity', array $details = [])
32
+ * @method static self locked(string $message = 'Locked', array $details = [])
33
+ * @method static self failedDependency(string $message = 'Failed Dependency', array $details = [])
34
+ * @method static self tooEarly(string $message = 'Too Early', array $details = [])
35
+ * @method static self upgradeRequired(string $message = 'Upgrade Required', array $details = [])
36
+ * @method static self preconditionRequired(string $message = 'Precondition Required', array $details = [])
37
+ * @method static self tooManyRequests(string $message = 'Too Many Requests', array $details = [])
38
+ * @method static self requestHeaderFieldsTooLarge(string $message = 'Request Header Fields Too Large', array $details = [])
39
+ * @method static self unavailableForLegalReasons(string $message = 'Unavailable for Legal Reasons', array $details = [])
40
+ * @method static self internal(string $message = 'Internal Server Error', array $details = [])
41
+ * @method static self notImplemented(string $message = 'Not Implemented', array $details = [])
42
+ * @method static self badGateway(string $message = 'Bad Gateway', array $details = [])
43
+ * @method static self serviceUnavailable(string $message = 'Service Unavailable', array $details = [])
44
+ * @method static self gatewayTimeout(string $message = 'Gateway Timeout', array $details = [])
45
+ * @method static self httpVersionNotSupported(string $message = 'HTTP Version Not Supported', array $details = [])
46
+ * @method static self insufficientStorage(string $message = 'Insufficient Storage', array $details = [])
47
+ * @method static self loopDetected(string $message = 'Loop Detected', array $details = [])
48
+ * @method static self notExtended(string $message = 'Not Extended', array $details = [])
49
+ * @method static self networkAuthenticationRequired(string $message = 'Network Authentication Required', array $details = [])
50
+ * @method static self networkConnectTimeoutError(string $message = 'Network Connect Timeout Error', array $details = [])
51
+ */
52
+ class Boom
53
+ {
54
+ private const PHRASES = [
55
+ /* 4XX Client error */
56
+ 400 => 'Bad Request',
57
+ 401 => 'Unauthorized',
58
+ 402 => 'Payment Required',
59
+ 403 => 'Forbidden',
60
+ 404 => 'Not Found',
61
+ 405 => 'Method Not Allowed',
62
+ 406 => 'Not Acceptable',
63
+ 407 => 'Proxy Authentication Required',
64
+ 408 => 'Request Timeout',
65
+ 409 => 'Conflict',
66
+ 410 => 'Gone',
67
+ 411 => 'Length Required',
68
+ 412 => 'Precondition Failed',
69
+ 413 => 'Payload Too Large',
70
+ 414 => 'URI Too Large',
71
+ 415 => 'Unsupported Media Type',
72
+ 416 => 'Range Not Satisfiable',
73
+ 417 => 'Expectation Failed',
74
+ 418 => "I'm a teapot",
75
+ 421 => 'Misdirected Request',
76
+ 422 => 'Unprocessable Entity',
77
+ 423 => 'Locked',
78
+ 424 => 'Failed Dependency',
79
+ 425 => 'Too Early',
80
+ 426 => 'Upgrade Required',
81
+ 428 => 'Precondition Required',
82
+ 429 => 'Too Many Requests',
83
+ 431 => 'Request Header Fields Too Large',
84
+ 451 => 'Unavailable for Legal Reasons',
85
+ 499 => 'Client Closed Request',
86
+
87
+ /* 5XX Server error */
88
+ 500 => 'Internal Server Error',
89
+ 501 => 'Not Implemented',
90
+ 502 => 'Bad Gateway',
91
+ 503 => 'Service Unavailable',
92
+ 504 => 'Gateway Timeout',
93
+ 505 => 'HTTP Version Not Supported',
94
+ 507 => 'Insufficient Storage',
95
+ 508 => 'Loop Detected',
96
+ 510 => 'Not Extended',
97
+ 511 => 'Network Authentication Required',
98
+ 599 => 'Network Connect Timeout Error',
99
+ ];
100
+
101
+ /** @var int */
102
+ protected int $statusCode;
103
+
104
+ /** @var string */
105
+ protected string $errorMessage;
106
+
107
+ /** @var array<string,mixed> */
108
+ protected array $errorDetails;
109
+
110
+ public function __construct(
111
+ int $statusCode,
112
+ string $errorMessage = '',
113
+ array $errorDetails = [],
114
+ ) {
115
+ if (!isset(self::PHRASES[$statusCode])) {
116
+ throw new InvalidArgumentException("Unsupported HTTP status code: $statusCode");
117
+ }
118
+
119
+ $this->statusCode = $statusCode;
120
+ $this->errorMessage = $errorMessage ?: self::PHRASES[$statusCode];
121
+ $this->errorDetails = $errorDetails;
122
+ }
123
+
124
+ public static function create(int $code, ?string $msg = null, array $details = []): self
125
+ {
126
+ return new self($code, $msg ?? '', $details);
127
+ }
128
+
129
+ /**
130
+ * Dynamic factories: Boom::tooManyRequests(), Boom::badRequest(), …
131
+ *
132
+ * @param array{0?:string,1?:array<mixed>} $args
133
+ */
134
+ public static function __callStatic(string $method, array $args): self
135
+ {
136
+ // Convert camelCase to Studly Caps → Reason‑Phrase → code
137
+ $normalized = strtolower(preg_replace('/([a-z])([A-Z])/', '$1 $2', $method) ?? '');
138
+ $code = array_search(
139
+ ucwords(str_replace(' ', ' ', $normalized)),
140
+ self::PHRASES,
141
+ true
142
+ );
143
+
144
+ if ($code === false) {
145
+ throw new InvalidArgumentException("Undefined Boom factory: $method()");
146
+ }
147
+
148
+ $msg = $args[0] ?? '';
149
+ $details = $args[1] ?? [];
150
+
151
+ return new self((int)$code, $msg, $details);
152
+ }
153
+
154
+ public function toResponse(): void
155
+ {
156
+ http_response_code($this->statusCode);
157
+ header('Content-Type: application/json; charset=utf-8');
158
+
159
+ try {
160
+ echo json_encode(
161
+ [
162
+ 'statusCode' => $this->statusCode,
163
+ 'error' => $this->errorMessage,
164
+ 'details' => (object)$this->errorDetails,
165
+ ],
166
+ JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
167
+ );
168
+ } catch (JsonException $e) {
169
+ echo '{"statusCode":500,"error":"JSON encoding error"}';
170
+ }
171
+
172
+ exit; // Ensure no further output
173
+ }
174
+
175
+ public static function isBoom(mixed $err): bool
176
+ {
177
+ return $err instanceof self;
178
+ }
179
+
180
+ public function getStatusCode(): int
181
+ {
182
+ return $this->statusCode;
183
+ }
184
+ public function getErrorMessage(): string
185
+ {
186
+ return $this->errorMessage;
187
+ }
188
+ public function getErrorDetails(): array
189
+ {
190
+ return $this->errorDetails;
191
+ }
192
+ }
@@ -0,0 +1,59 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib;
6
+
7
+ use RuntimeException;
8
+ use InvalidArgumentException;
9
+ use Lib\PHPX\TemplateCompiler;
10
+
11
+ class IncludeTracker
12
+ {
13
+ private static array $sections = [];
14
+
15
+ /**
16
+ * Includes and echoes a file wrapped in a unique pp-component container.
17
+ * Supported $mode values: 'include', 'include_once', 'require', 'require_once'
18
+ *
19
+ * @param string $filePath The path to the file to be included.
20
+ * @param string $mode The mode of inclusion.
21
+ * @throws RuntimeException If the file does not exist.
22
+ * @throws InvalidArgumentException If an invalid mode is provided.
23
+ * @return void
24
+ */
25
+ public static function render(string $filePath, string $mode = 'include_once'): void
26
+ {
27
+ if (!file_exists($filePath)) {
28
+ throw new RuntimeException("File not found: $filePath");
29
+ }
30
+
31
+ ob_start();
32
+ match ($mode) {
33
+ 'include' => include $filePath,
34
+ 'include_once' => include_once $filePath,
35
+ 'require' => require $filePath,
36
+ 'require_once' => require_once $filePath,
37
+ default => throw new InvalidArgumentException("Invalid include mode: $mode"),
38
+ };
39
+ $html = ob_get_clean();
40
+
41
+ $wrapped = self::wrapWithId($filePath, $html);
42
+ $fragDom = TemplateCompiler::convertToXml($wrapped);
43
+
44
+ $newHtml = TemplateCompiler::innerXml($fragDom);
45
+
46
+ self::$sections[$filePath] = [
47
+ 'path' => $filePath,
48
+ 'html' => $newHtml,
49
+ ];
50
+
51
+ echo $newHtml;
52
+ }
53
+
54
+ private static function wrapWithId(string $filePath, string $html): string
55
+ {
56
+ $id = 's' . base_convert(sprintf('%u', crc32($filePath)), 10, 36);
57
+ return "<div pp-component=\"$id\">\n$html\n</div>";
58
+ }
59
+ }