create-prisma-php-app 4.0.0-alpha.5 → 4.0.0-alpha.51

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 (53) hide show
  1. package/dist/.htaccess +54 -41
  2. package/dist/bootstrap.php +142 -97
  3. package/dist/index.js +819 -343
  4. package/dist/settings/auto-swagger-docs.ts +196 -95
  5. package/dist/settings/bs-config.ts +53 -58
  6. package/dist/settings/files-list.json +1 -1
  7. package/dist/settings/project-name.ts +2 -0
  8. package/dist/settings/restart-mcp.ts +58 -0
  9. package/dist/settings/restart-websocket.ts +51 -45
  10. package/dist/settings/utils.ts +240 -0
  11. package/dist/src/Lib/AI/ChatGPTClient.php +147 -0
  12. package/dist/src/Lib/Auth/Auth.php +544 -0
  13. package/dist/src/Lib/Auth/AuthConfig.php +89 -0
  14. package/dist/src/Lib/CacheHandler.php +121 -0
  15. package/dist/src/Lib/ErrorHandler.php +322 -0
  16. package/dist/src/Lib/FileManager/UploadFile.php +383 -0
  17. package/dist/src/Lib/Headers/Boom.php +192 -0
  18. package/dist/src/Lib/IncludeTracker.php +59 -0
  19. package/dist/src/Lib/MCP/WeatherTools.php +104 -0
  20. package/dist/src/Lib/MCP/mcp-server.php +80 -0
  21. package/dist/src/Lib/MainLayout.php +230 -0
  22. package/dist/src/Lib/Middleware/AuthMiddleware.php +157 -0
  23. package/dist/src/Lib/Middleware/CorsMiddleware.php +145 -0
  24. package/dist/src/Lib/PHPMailer/Mailer.php +169 -0
  25. package/dist/src/Lib/PartialRenderer.php +40 -0
  26. package/dist/src/Lib/PrismaPHPSettings.php +181 -0
  27. package/dist/src/Lib/Request.php +479 -0
  28. package/dist/src/Lib/Security/RateLimiter.php +33 -0
  29. package/dist/src/Lib/Set.php +102 -0
  30. package/dist/src/Lib/StateManager.php +127 -0
  31. package/dist/src/Lib/Validator.php +752 -0
  32. package/dist/src/{Websocket → Lib/Websocket}/ConnectionManager.php +1 -1
  33. package/dist/src/Lib/Websocket/websocket-server.php +118 -0
  34. package/dist/src/app/error.php +1 -1
  35. package/dist/src/app/index.php +22 -5
  36. package/dist/src/app/js/index.js +1 -1
  37. package/dist/src/app/layout.php +2 -2
  38. package/package.json +1 -1
  39. package/dist/settings/restart-websocket.bat +0 -28
  40. package/dist/src/app/assets/images/prisma-php-black.svg +0 -6
  41. package/dist/websocket-server.php +0 -22
  42. package/vendor/autoload.php +0 -25
  43. package/vendor/composer/ClassLoader.php +0 -579
  44. package/vendor/composer/InstalledVersions.php +0 -359
  45. package/vendor/composer/LICENSE +0 -21
  46. package/vendor/composer/autoload_classmap.php +0 -10
  47. package/vendor/composer/autoload_namespaces.php +0 -9
  48. package/vendor/composer/autoload_psr4.php +0 -10
  49. package/vendor/composer/autoload_real.php +0 -38
  50. package/vendor/composer/autoload_static.php +0 -25
  51. package/vendor/composer/installed.json +0 -825
  52. package/vendor/composer/installed.php +0 -132
  53. package/vendor/composer/platform_check.php +0 -26
@@ -0,0 +1,145 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib\Middleware;
6
+
7
+ final class CorsMiddleware
8
+ {
9
+ /** Entry point */
10
+ public static function handle(?array $overrides = null): void
11
+ {
12
+ // Not a CORS request
13
+ $origin = $_SERVER['HTTP_ORIGIN'] ?? '';
14
+ if ($origin === '') {
15
+ return;
16
+ }
17
+
18
+ // Resolve config (env → overrides)
19
+ $cfg = self::buildConfig($overrides);
20
+
21
+ // Not allowed? Do nothing (browser will block)
22
+ if (!self::isAllowedOrigin($origin, $cfg['allowedOrigins'])) {
23
+ return;
24
+ }
25
+
26
+ // Compute which value to send for Access-Control-Allow-Origin
27
+ // If credentials are disabled and '*' is in list, we can send '*'
28
+ $sendWildcard = (!$cfg['allowCredentials'] && self::listHasWildcard($cfg['allowedOrigins']));
29
+ $allowOriginValue = $sendWildcard ? '*' : self::normalize($origin);
30
+
31
+ // Vary for caches
32
+ header('Vary: Origin, Access-Control-Request-Method, Access-Control-Request-Headers');
33
+
34
+ header('Access-Control-Allow-Origin: ' . $allowOriginValue);
35
+ if ($cfg['allowCredentials']) {
36
+ header('Access-Control-Allow-Credentials: true');
37
+ }
38
+
39
+ if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
40
+ // Preflight response
41
+ $requestedHeaders = $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] ?? '';
42
+ $allowedHeaders = $cfg['allowedHeaders'] !== ''
43
+ ? $cfg['allowedHeaders']
44
+ : ($requestedHeaders ?: 'Content-Type, Authorization, X-Requested-With');
45
+
46
+ header('Access-Control-Allow-Methods: ' . $cfg['allowedMethods']);
47
+ header('Access-Control-Allow-Headers: ' . $allowedHeaders);
48
+ if ($cfg['maxAge'] > 0) {
49
+ header('Access-Control-Max-Age: ' . (string) $cfg['maxAge']);
50
+ }
51
+
52
+ // Optional: Private Network Access preflights (Chrome)
53
+ if (!empty($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_PRIVATE_NETWORK'])) {
54
+ header('Access-Control-Allow-Private-Network: true');
55
+ }
56
+
57
+ http_response_code(204);
58
+ header('Content-Length: 0');
59
+ exit;
60
+ }
61
+
62
+ // Simple/actual request
63
+ if ($cfg['exposeHeaders'] !== '') {
64
+ header('Access-Control-Expose-Headers: ' . $cfg['exposeHeaders']);
65
+ }
66
+ }
67
+
68
+ /** Read env + normalize + apply overrides */
69
+ private static function buildConfig(?array $overrides): array
70
+ {
71
+ $allowed = self::parseList($_ENV['CORS_ALLOWED_ORIGINS'] ?? '');
72
+ $cfg = [
73
+ 'allowedOrigins' => $allowed,
74
+ 'allowCredentials' => filter_var($_ENV['CORS_ALLOW_CREDENTIALS'] ?? 'false', FILTER_VALIDATE_BOOLEAN),
75
+ 'allowedMethods' => $_ENV['CORS_ALLOWED_METHODS'] ?? 'GET, POST, PUT, PATCH, DELETE, OPTIONS',
76
+ 'allowedHeaders' => trim($_ENV['CORS_ALLOWED_HEADERS'] ?? ''),
77
+ 'exposeHeaders' => trim($_ENV['CORS_EXPOSE_HEADERS'] ?? ''),
78
+ 'maxAge' => (int)($_ENV['CORS_MAX_AGE'] ?? 86400),
79
+ ];
80
+
81
+ if (is_array($overrides)) {
82
+ foreach ($overrides as $k => $v) {
83
+ if (array_key_exists($k, $cfg)) {
84
+ $cfg[$k] = $v;
85
+ }
86
+ }
87
+ }
88
+
89
+ // Normalize patterns
90
+ $cfg['allowedOrigins'] = array_map([self::class, 'normalize'], $cfg['allowedOrigins']);
91
+ return $cfg;
92
+ }
93
+
94
+ /** CSV or JSON array → array<string> */
95
+ private static function parseList(string $raw): array
96
+ {
97
+ $raw = trim($raw);
98
+ if ($raw === '') return [];
99
+
100
+ if ($raw[0] === '[') {
101
+ $arr = json_decode($raw, true);
102
+ if (is_array($arr)) {
103
+ return array_values(array_filter(array_map('strval', $arr), 'strlen'));
104
+ }
105
+ }
106
+ return array_values(array_filter(array_map('trim', explode(',', $raw)), 'strlen'));
107
+ }
108
+
109
+ private static function normalize(string $origin): string
110
+ {
111
+ return rtrim($origin, '/');
112
+ }
113
+
114
+ private static function isAllowedOrigin(string $origin, array $list): bool
115
+ {
116
+ $o = self::normalize($origin);
117
+
118
+ foreach ($list as $pattern) {
119
+ $p = self::normalize($pattern);
120
+
121
+ // literal "*"
122
+ if ($p === '*') return true;
123
+
124
+ // allow literal "null" for file:// or sandboxed if explicitly listed
125
+ if ($o === 'null' && strtolower($p) === 'null') return true;
126
+
127
+ // wildcard like https://*.example.com
128
+ if (strpos($p, '*') !== false) {
129
+ $regex = '/^' . str_replace('\*', '[^.]+', preg_quote($p, '/')) . '$/i';
130
+ if (preg_match($regex, $o)) return true;
131
+ } else {
132
+ if (strcasecmp($p, $o) === 0) return true;
133
+ }
134
+ }
135
+ return false;
136
+ }
137
+
138
+ private static function listHasWildcard(array $list): bool
139
+ {
140
+ foreach ($list as $p) {
141
+ if (trim($p) === '*') return true;
142
+ }
143
+ return false;
144
+ }
145
+ }
@@ -0,0 +1,169 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib\PHPMailer;
6
+
7
+ use PHPMailer\PHPMailer\PHPMailer;
8
+ use PHPMailer\PHPMailer\Exception;
9
+ use Lib\Validator;
10
+
11
+ class Mailer
12
+ {
13
+ private PHPMailer $mail;
14
+
15
+ public function __construct()
16
+ {
17
+ $this->mail = new PHPMailer(true);
18
+ $this->mail->CharSet = 'UTF-8';
19
+ $this->setup();
20
+ }
21
+
22
+ private function setup(): void
23
+ {
24
+ $this->mail->isSMTP();
25
+ $this->mail->SMTPDebug = 0;
26
+ $this->mail->Host = $_ENV['SMTP_HOST'];
27
+ $this->mail->SMTPAuth = true;
28
+ $this->mail->Username = $_ENV['SMTP_USERNAME'];
29
+ $this->mail->Password = $_ENV['SMTP_PASSWORD'];
30
+ $this->mail->SMTPSecure = $_ENV['SMTP_ENCRYPTION'];
31
+ $this->mail->Port = (int) $_ENV['SMTP_PORT'];
32
+ $this->mail->setFrom($_ENV['MAIL_FROM'], $_ENV['MAIL_FROM_NAME']);
33
+ }
34
+
35
+ /**
36
+ * Send an email.
37
+ *
38
+ * @param string $to The recipient's email address.
39
+ * @param string $subject The subject of the email.
40
+ * @param string $body The HTML body of the email.
41
+ * @param array $options (optional) Additional email options like name, altBody, CC, BCC, and attachments.
42
+ * - attachments: A string or an array of file paths, or an array of associative arrays with keys 'path' and 'name'.
43
+ *
44
+ * @return bool Returns true if the email is sent successfully, false otherwise.
45
+ *
46
+ * @throws Exception Throws an exception if the email could not be sent.
47
+ */
48
+ public function send(string $to, string $subject, string $body, array $options = []): bool
49
+ {
50
+ try {
51
+ // Validate and sanitize inputs
52
+ $to = Validator::email($to);
53
+ if (!$to) {
54
+ throw new Exception('Invalid email address for the main recipient');
55
+ }
56
+
57
+ $subject = Validator::string($subject);
58
+ $body = Validator::html($body);
59
+ $altBody = $this->convertToPlainText($body);
60
+
61
+ $name = $options['name'] ?? '';
62
+ $addCC = $options['addCC'] ?? [];
63
+ $addBCC = $options['addBCC'] ?? [];
64
+ $attachments = $options['attachments'] ?? [];
65
+
66
+ $name = Validator::string($name);
67
+
68
+ // Handle CC recipients
69
+ $this->handleRecipients($addCC, 'CC');
70
+ // Handle BCC recipients
71
+ $this->handleRecipients($addBCC, 'BCC');
72
+ // Handle file attachments if provided
73
+ if (!empty($attachments)) {
74
+ $this->handleAttachments($attachments);
75
+ }
76
+
77
+ // Set the main recipient and other email properties
78
+ $this->mail->addAddress($to, $name);
79
+ $this->mail->isHTML(true);
80
+ $this->mail->Subject = $subject;
81
+ $this->mail->Body = $body;
82
+ $this->mail->AltBody = $altBody;
83
+
84
+ // Send the email
85
+ return $this->mail->send();
86
+ } catch (Exception $e) {
87
+ throw new Exception($e->getMessage());
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Handle adding CC or BCC recipients.
93
+ *
94
+ * @param string|array $recipients Email addresses to add.
95
+ * @param string $type Type of recipient ('CC' or 'BCC').
96
+ *
97
+ * @throws Exception Throws an exception if any email address is invalid.
98
+ */
99
+ private function handleRecipients(string|array $recipients, string $type): void
100
+ {
101
+ if (!empty($recipients)) {
102
+ $method = $type === 'CC' ? 'addCC' : 'addBCC';
103
+
104
+ if (is_array($recipients)) {
105
+ foreach ($recipients as $recipient) {
106
+ $recipient = Validator::email($recipient);
107
+ if ($recipient) {
108
+ $this->mail->{$method}($recipient);
109
+ } else {
110
+ throw new Exception("Invalid email address in $type");
111
+ }
112
+ }
113
+ } else {
114
+ $recipient = Validator::email($recipients);
115
+ if ($recipient) {
116
+ $this->mail->{$method}($recipient);
117
+ } else {
118
+ throw new Exception("Invalid email address in $type");
119
+ }
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Handle adding file attachments.
126
+ *
127
+ * @param string|array $attachments File path(s) to attach.
128
+ * You can pass a string for a single file or an array of file paths.
129
+ * Alternatively, each attachment can be an array with keys 'path' and 'name' for custom naming.
130
+ *
131
+ * @throws Exception Throws an exception if any attachment file is not found.
132
+ */
133
+ private function handleAttachments(string|array $attachments): void
134
+ {
135
+ if (is_array($attachments)) {
136
+ foreach ($attachments as $attachment) {
137
+ if (is_array($attachment)) {
138
+ $file = $attachment['path'] ?? null;
139
+ $name = $attachment['name'] ?? '';
140
+ if (!$file || !file_exists($file)) {
141
+ throw new Exception("Attachment file does not exist: " . ($file ?? 'unknown'));
142
+ }
143
+ $this->mail->addAttachment($file, $name);
144
+ } else {
145
+ if (!file_exists($attachment)) {
146
+ throw new Exception("Attachment file does not exist: $attachment");
147
+ }
148
+ $this->mail->addAttachment($attachment);
149
+ }
150
+ }
151
+ } else {
152
+ if (!file_exists($attachments)) {
153
+ throw new Exception("Attachment file does not exist: $attachments");
154
+ }
155
+ $this->mail->addAttachment($attachments);
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Convert HTML content to plain text.
161
+ *
162
+ * @param string $html The HTML content to convert.
163
+ * @return string The plain text content.
164
+ */
165
+ private function convertToPlainText(string $html): string
166
+ {
167
+ return strip_tags(str_replace(['<br>', '<br/>', '<br />', '</p>'], "\n", $html));
168
+ }
169
+ }
@@ -0,0 +1,40 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib;
6
+
7
+ use DOMDocument;
8
+ use DOMXPath;
9
+ use DOMElement;
10
+
11
+ final class PartialRenderer
12
+ {
13
+ /** @return array<string,string> selector => outerHTML */
14
+ public static function extract(string $html, array $selectors): array
15
+ {
16
+ $doc = new DOMDocument('1.0', 'UTF-8');
17
+ libxml_use_internal_errors(true);
18
+ $doc->loadHTML($html, LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_HTML_NOIMPLIED);
19
+ libxml_clear_errors();
20
+
21
+ $xpath = new DOMXPath($doc);
22
+ $payload = [];
23
+
24
+ foreach ($selectors as $rawSel) {
25
+ $sel = preg_replace('/[^-_\w]/', '', (string)$rawSel);
26
+
27
+ /** @var DOMElement|null $node */
28
+ $node = $xpath->query("//*[@pp-sync='{$sel}']")->item(0);
29
+
30
+ if (!$node && $sel !== '') {
31
+ $node = $xpath->query("//*[@id='{$sel}' or contains(concat(' ',normalize-space(@class),' '),' {$sel} ')]")->item(0);
32
+ }
33
+
34
+ if ($node) {
35
+ $payload[$sel] = $doc->saveHTML($node);
36
+ }
37
+ }
38
+ return $payload;
39
+ }
40
+ }
@@ -0,0 +1,181 @@
1
+ <?php
2
+
3
+ declare(strict_types=1);
4
+
5
+ namespace Lib;
6
+
7
+ use Exception;
8
+
9
+ class BSPathRewrite
10
+ {
11
+ public string $pattern;
12
+ public string $replacement;
13
+
14
+ public function __construct(array $data)
15
+ {
16
+ $this->pattern = $data['pattern'] ?? '';
17
+ $this->replacement = $data['replacement'] ?? '';
18
+ }
19
+ }
20
+
21
+ class PrismaSettings
22
+ {
23
+ public string $projectName;
24
+ public string $projectRootPath;
25
+ public string $phpEnvironment;
26
+ public string $phpRootPathExe;
27
+ public string $phpGenerateClassPath;
28
+ public string $bsTarget;
29
+ public BSPathRewrite $bsPathRewrite;
30
+ public bool $backendOnly;
31
+ public bool $swaggerDocs;
32
+ public bool $tailwindcss;
33
+ public bool $websocket;
34
+ public bool $prisma;
35
+ public bool $docker;
36
+ public string $version;
37
+ public array $excludeFiles;
38
+
39
+ public function __construct(array $data)
40
+ {
41
+ $this->projectName = $data['projectName'] ?? '';
42
+ $this->projectRootPath = $data['projectRootPath'] ?? '';
43
+ $this->phpEnvironment = $data['phpEnvironment'] ?? '';
44
+ $this->phpRootPathExe = $data['phpRootPathExe'] ?? '';
45
+ $this->bsTarget = $data['bsTarget'] ?? '';
46
+ $this->bsPathRewrite = new BSPathRewrite($data['bsPathRewrite'] ?? []);
47
+ $this->backendOnly = $data['backendOnly'] ?? false;
48
+ $this->swaggerDocs = $data['swaggerDocs'] ?? false;
49
+ $this->tailwindcss = $data['tailwindcss'] ?? false;
50
+ $this->websocket = $data['websocket'] ?? false;
51
+ $this->prisma = $data['prisma'] ?? false;
52
+ $this->docker = $data['docker'] ?? false;
53
+ $this->version = $data['version'] ?? '';
54
+ $this->excludeFiles = $data['excludeFiles'] ?? [];
55
+ }
56
+ }
57
+
58
+ class PrismaPHPSettings
59
+ {
60
+ /**
61
+ * The settings from the prisma-php.json file.
62
+ *
63
+ * @var PrismaSettings
64
+ */
65
+ public static PrismaSettings $option;
66
+
67
+ /**
68
+ * The list of route files from the files-list.json file.
69
+ *
70
+ * @var array
71
+ */
72
+ public static array $routeFiles = [];
73
+
74
+ /**
75
+ * The list of class log files.
76
+ *
77
+ * @var array
78
+ */
79
+ public static array $classLogFiles = [];
80
+
81
+ /**
82
+ * The list of include files.
83
+ *
84
+ * @var array
85
+ */
86
+ public static array $includeFiles = [];
87
+
88
+ /**
89
+ * The local storage key for the app state.
90
+ *
91
+ * @var string
92
+ */
93
+ public static string $localStoreKey;
94
+
95
+ public static function init(): void
96
+ {
97
+ self::$option = self::getPrismaSettings();
98
+ self::$routeFiles = self::getRoutesFileList();
99
+ self::$classLogFiles = self::getClassesLogFiles();
100
+ self::$includeFiles = self::getIncludeFiles();
101
+ self::$localStoreKey = self::getLocalStorageKey();
102
+ }
103
+
104
+ /**
105
+ * Get Prisma settings from the JSON file.
106
+ *
107
+ * @return PrismaSettings
108
+ * @throws Exception if the JSON file cannot be decoded.
109
+ */
110
+ private static function getPrismaSettings(): PrismaSettings
111
+ {
112
+ $prismaPHPSettingsJson = DOCUMENT_PATH . '/prisma-php.json';
113
+
114
+ if (!file_exists($prismaPHPSettingsJson)) {
115
+ throw new Exception("Settings file not found: $prismaPHPSettingsJson");
116
+ }
117
+
118
+ $jsonContent = file_get_contents($prismaPHPSettingsJson);
119
+ $decodedJson = json_decode($jsonContent, true);
120
+
121
+ if (json_last_error() !== JSON_ERROR_NONE) {
122
+ throw new Exception("Failed to decode JSON: " . json_last_error_msg());
123
+ }
124
+
125
+ return new PrismaSettings($decodedJson);
126
+ }
127
+
128
+ private static function getRoutesFileList(): array
129
+ {
130
+ $jsonFileName = SETTINGS_PATH . '/files-list.json';
131
+ if (!file_exists($jsonFileName)) {
132
+ return [];
133
+ }
134
+
135
+ $jsonContent = file_get_contents($jsonFileName);
136
+ if ($jsonContent === false || empty(trim($jsonContent))) {
137
+ return [];
138
+ }
139
+
140
+ $routeFiles = json_decode($jsonContent, true);
141
+ return is_array($routeFiles) ? $routeFiles : [];
142
+ }
143
+
144
+ private static function getClassesLogFiles(): array
145
+ {
146
+ $jsonFileName = SETTINGS_PATH . '/class-imports.json';
147
+ if (!file_exists($jsonFileName)) {
148
+ return [];
149
+ }
150
+
151
+ $jsonContent = file_get_contents($jsonFileName);
152
+ if ($jsonContent === false || empty(trim($jsonContent))) {
153
+ return [];
154
+ }
155
+
156
+ $classLogFiles = json_decode($jsonContent, true);
157
+ return is_array($classLogFiles) ? $classLogFiles : [];
158
+ }
159
+
160
+ private static function getIncludeFiles(): array
161
+ {
162
+ $jsonFileName = SETTINGS_PATH . "/request-data.json";
163
+ if (!file_exists($jsonFileName)) {
164
+ return [];
165
+ }
166
+
167
+ $jsonContent = file_get_contents($jsonFileName);
168
+ if ($jsonContent === false || empty(trim($jsonContent))) {
169
+ return [];
170
+ }
171
+
172
+ $includeFiles = json_decode($jsonContent, true);
173
+ return is_array($includeFiles) ? $includeFiles : [];
174
+ }
175
+
176
+ private static function getLocalStorageKey(): string
177
+ {
178
+ $localStorageKey = $_ENV['LOCALSTORE_KEY'] ?? 'pphp_local_store_59e13';
179
+ return strtolower(preg_replace('/\s+/', '_', trim($localStorageKey)));
180
+ }
181
+ }