create-prisma-php-app 3.1.11 → 4.0.0-alpha.2
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/bootstrap.php +10 -10
- package/dist/index.js +12 -8
- package/dist/settings/restart-websocket.bat +1 -1
- package/dist/settings/restart-websocket.ts +2 -9
- package/dist/src/{Lib/Websocket → Websocket}/ConnectionManager.php +1 -1
- package/dist/src/app/error.php +1 -1
- package/dist/src/app/index.php +1 -1
- package/dist/src/app/js/index.js +1 -1
- package/dist/src/app/layout.php +2 -2
- package/dist/{src/Lib/Websocket/websocket-server.php → websocket-server.php} +2 -7
- package/package.json +1 -1
- package/dist/src/Lib/AI/ChatGPTClient.php +0 -147
- package/dist/src/Lib/Auth/Auth.php +0 -544
- package/dist/src/Lib/Auth/AuthConfig.php +0 -89
- package/dist/src/Lib/CacheHandler.php +0 -121
- package/dist/src/Lib/ErrorHandler.php +0 -322
- package/dist/src/Lib/FileManager/UploadFile.php +0 -383
- package/dist/src/Lib/Headers/Boom.php +0 -208
- package/dist/src/Lib/IncludeTracker.php +0 -59
- package/dist/src/Lib/MainLayout.php +0 -215
- package/dist/src/Lib/Middleware/AuthMiddleware.php +0 -154
- package/dist/src/Lib/PHPMailer/Mailer.php +0 -169
- package/dist/src/Lib/PHPX/Exceptions/ComponentValidationException.php +0 -49
- package/dist/src/Lib/PHPX/IPHPX.php +0 -22
- package/dist/src/Lib/PHPX/PHPX.php +0 -173
- package/dist/src/Lib/PHPX/TemplateCompiler.php +0 -571
- package/dist/src/Lib/PHPX/TwMerge.php +0 -195
- package/dist/src/Lib/PHPX/TypeCoercer.php +0 -490
- package/dist/src/Lib/PartialRenderer.php +0 -40
- package/dist/src/Lib/PrismaPHPSettings.php +0 -181
- package/dist/src/Lib/Request.php +0 -476
- package/dist/src/Lib/Set.php +0 -102
- package/dist/src/Lib/StateManager.php +0 -127
- package/dist/src/Lib/Validator.php +0 -738
|
@@ -1,383 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
<?php
|
|
2
|
-
|
|
3
|
-
declare(strict_types=1);
|
|
4
|
-
|
|
5
|
-
namespace Lib\Headers;
|
|
6
|
-
|
|
7
|
-
class Boom
|
|
8
|
-
{
|
|
9
|
-
/**
|
|
10
|
-
* HTTP status code.
|
|
11
|
-
*
|
|
12
|
-
* @var int
|
|
13
|
-
*/
|
|
14
|
-
protected int $statusCode = 500;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Error message.
|
|
18
|
-
*
|
|
19
|
-
* @var string
|
|
20
|
-
*/
|
|
21
|
-
protected string $errorMessage = 'Internal Server Error';
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Additional error details.
|
|
25
|
-
*
|
|
26
|
-
* @var array
|
|
27
|
-
*/
|
|
28
|
-
protected array $errorDetails = [];
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Boom constructor.
|
|
32
|
-
*
|
|
33
|
-
* @param int $statusCode HTTP status code.
|
|
34
|
-
* @param string $errorMessage Error message.
|
|
35
|
-
* @param array $errorDetails Additional error details.
|
|
36
|
-
*/
|
|
37
|
-
public function __construct(int $statusCode, string $errorMessage, array $errorDetails = [])
|
|
38
|
-
{
|
|
39
|
-
$this->statusCode = $statusCode;
|
|
40
|
-
$this->errorMessage = $errorMessage;
|
|
41
|
-
$this->errorDetails = $errorDetails;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Factory method for 400 Bad Request.
|
|
46
|
-
*
|
|
47
|
-
* @param string $message Error message.
|
|
48
|
-
* @param array $details Additional error details.
|
|
49
|
-
*
|
|
50
|
-
* @return self
|
|
51
|
-
*/
|
|
52
|
-
public static function badRequest(string $message = 'Bad Request', array $details = []): self
|
|
53
|
-
{
|
|
54
|
-
return new self(400, $message, $details);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Factory method for 401 Unauthorized.
|
|
59
|
-
*
|
|
60
|
-
* @param string $message Error message.
|
|
61
|
-
* @param array $details Additional error details.
|
|
62
|
-
*
|
|
63
|
-
* @return self
|
|
64
|
-
*/
|
|
65
|
-
public static function unauthorized(string $message = 'Unauthorized', array $details = []): self
|
|
66
|
-
{
|
|
67
|
-
return new self(401, $message, $details);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Factory method for 402 Payment Required.
|
|
72
|
-
*
|
|
73
|
-
* @param string $message Error message.
|
|
74
|
-
* @param array $details Additional error details.
|
|
75
|
-
*
|
|
76
|
-
* @return self
|
|
77
|
-
*/
|
|
78
|
-
public static function paymentRequired(string $message = 'Payment Required', array $details = []): self
|
|
79
|
-
{
|
|
80
|
-
return new self(402, $message, $details);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Factory method for 403 Forbidden.
|
|
85
|
-
*
|
|
86
|
-
* @param string $message Error message.
|
|
87
|
-
* @param array $details Additional error details.
|
|
88
|
-
*
|
|
89
|
-
* @return self
|
|
90
|
-
*/
|
|
91
|
-
public static function forbidden(string $message = 'Forbidden', array $details = []): self
|
|
92
|
-
{
|
|
93
|
-
return new self(403, $message, $details);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Factory method for 404 Not Found.
|
|
98
|
-
*
|
|
99
|
-
* @param string $message Error message.
|
|
100
|
-
* @param array $details Additional error details.
|
|
101
|
-
*
|
|
102
|
-
* @return self
|
|
103
|
-
*/
|
|
104
|
-
public static function notFound(string $message = 'Not Found', array $details = []): self
|
|
105
|
-
{
|
|
106
|
-
return new self(404, $message, $details);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Factory method for 405 Method Not Allowed.
|
|
111
|
-
*
|
|
112
|
-
* @param string $message Error message.
|
|
113
|
-
* @param array $details Additional error details.
|
|
114
|
-
*
|
|
115
|
-
* @return self
|
|
116
|
-
*/
|
|
117
|
-
public static function methodNotAllowed(string $message = 'Method Not Allowed', array $details = []): self
|
|
118
|
-
{
|
|
119
|
-
return new self(405, $message, $details);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Factory method for 406 Not Acceptable.
|
|
124
|
-
*
|
|
125
|
-
* @param string $message Error message.
|
|
126
|
-
* @param array $details Additional error details.
|
|
127
|
-
*
|
|
128
|
-
* @return self
|
|
129
|
-
*/
|
|
130
|
-
public static function notAcceptable(string $message = 'Not Acceptable', array $details = []): self
|
|
131
|
-
{
|
|
132
|
-
return new self(406, $message, $details);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* Factory method for 500 Internal Server Error.
|
|
137
|
-
*
|
|
138
|
-
* @param string $message Error message.
|
|
139
|
-
* @param array $details Additional error details.
|
|
140
|
-
*
|
|
141
|
-
* @return self
|
|
142
|
-
*/
|
|
143
|
-
public static function internal(string $message = 'Internal Server Error', array $details = []): self
|
|
144
|
-
{
|
|
145
|
-
return new self(500, $message, $details);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Sends the HTTP error response and terminates the script.
|
|
150
|
-
*
|
|
151
|
-
* @return void
|
|
152
|
-
*/
|
|
153
|
-
public function toResponse(): void
|
|
154
|
-
{
|
|
155
|
-
http_response_code($this->statusCode);
|
|
156
|
-
header('Content-Type: application/json');
|
|
157
|
-
|
|
158
|
-
echo json_encode([
|
|
159
|
-
'statusCode' => $this->statusCode,
|
|
160
|
-
'error' => $this->errorMessage,
|
|
161
|
-
'details' => $this->errorDetails,
|
|
162
|
-
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
|
|
163
|
-
|
|
164
|
-
exit; // Ensures no further execution after sending the response
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
* Checks if the provided error is an instance of Boom.
|
|
169
|
-
*
|
|
170
|
-
* @param mixed $error The error to check.
|
|
171
|
-
*
|
|
172
|
-
* @return bool
|
|
173
|
-
*/
|
|
174
|
-
public static function isBoom($error): bool
|
|
175
|
-
{
|
|
176
|
-
return $error instanceof self;
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Gets the HTTP status code.
|
|
181
|
-
*
|
|
182
|
-
* @return int
|
|
183
|
-
*/
|
|
184
|
-
public function getStatusCode(): int
|
|
185
|
-
{
|
|
186
|
-
return $this->statusCode;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
* Gets the error message.
|
|
191
|
-
*
|
|
192
|
-
* @return string
|
|
193
|
-
*/
|
|
194
|
-
public function getErrorMessage(): string
|
|
195
|
-
{
|
|
196
|
-
return $this->errorMessage;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* Gets the additional error details.
|
|
201
|
-
*
|
|
202
|
-
* @return array
|
|
203
|
-
*/
|
|
204
|
-
public function getErrorDetails(): array
|
|
205
|
-
{
|
|
206
|
-
return $this->errorDetails;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
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
|
-
public 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
|
-
}
|