create-prisma-php-app 3.0.0-beta.2 → 3.0.0-beta.4

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.
@@ -17,10 +17,10 @@ class IncludeTracker
17
17
  /**
18
18
  * Includes and echoes a file wrapped in a unique pp-section-id container.
19
19
  * Supported $mode values: 'include', 'include_once', 'require', 'require_once'
20
- *
20
+ *
21
21
  * @param string $filePath The path to the file to be included.
22
- * @param string $mode The mode of inclusion. Can be 'include', 'include_once', 'require', or 'require_once'.
23
- * @throws RuntimeException If the file does not exist.
22
+ * @param string $mode The mode of inclusion.
23
+ * @throws RuntimeException If the file does not exist.
24
24
  * @throws InvalidArgumentException If an invalid mode is provided.
25
25
  * @return void
26
26
  */
@@ -36,20 +36,16 @@ class IncludeTracker
36
36
  'include_once' => include_once $filePath,
37
37
  'require' => require $filePath,
38
38
  'require_once' => require_once $filePath,
39
- default => throw new InvalidArgumentException("Invalid include mode: $mode")
39
+ default => throw new InvalidArgumentException("Invalid include mode: $mode"),
40
40
  };
41
41
  $html = ob_get_clean();
42
42
 
43
- $wrapped = self::wrapWithId($filePath, $html);
44
-
45
- $fragDom = TemplateCompiler::convertToXml($wrapped, false);
43
+ $wrapped = self::wrapWithId($filePath, $html);
44
+ $fragDom = TemplateCompiler::convertToXml($wrapped, false);
46
45
 
47
46
  self::prefixInlineHandlers($fragDom);
48
47
 
49
- $newHtml = '';
50
- foreach ($fragDom->documentElement->childNodes as $c) {
51
- $newHtml .= $fragDom->saveXML($c);
52
- }
48
+ $newHtml = TemplateCompiler::innerXml($fragDom);
53
49
 
54
50
  self::$sections[$filePath] = [
55
51
  'path' => $filePath,
@@ -62,7 +58,6 @@ class IncludeTracker
62
58
  private static function wrapWithId(string $filePath, string $html): string
63
59
  {
64
60
  $id = 's' . base_convert(sprintf('%u', crc32($filePath)), 10, 36);
65
-
66
61
  return "<div pp-section-id=\"$id\">\n$html\n</div>";
67
62
  }
68
63
 
@@ -121,21 +121,6 @@ class PHPX implements IPHPX
121
121
  array_flip(array_merge($reserved, $exclude))
122
122
  );
123
123
 
124
- $props = array_combine(
125
- array_map('strtolower', array_keys($props)),
126
- $props
127
- );
128
-
129
- foreach ($props as $key => $val) {
130
- if (str_starts_with($key, 'on')) {
131
- $event = substr($key, 2);
132
- if (in_array($event, PrismaPHPSettings::$htmlEvents, true) && trim((string)$val) !== '') {
133
- $props["pp-original-on{$event}"] = (string)$val;
134
- }
135
- unset($props[$key]);
136
- }
137
- }
138
-
139
124
  foreach ($params as $k => $v) {
140
125
  $props[$k] = $v;
141
126
  }
@@ -169,6 +169,28 @@ class TemplateCompiler
169
169
  return $dom;
170
170
  }
171
171
 
172
+ /**
173
+ * Extracts the inner XML of a DOMNode, including all child nodes.
174
+ *
175
+ * @param DOMNode $node The node from which to extract the inner XML.
176
+ * @return string The inner XML as a string.
177
+ */
178
+ public static function innerXml(DOMNode $node): string
179
+ {
180
+ if ($node instanceof DOMDocument) {
181
+ $node = $node->documentElement;
182
+ }
183
+
184
+ /** @var DOMDocument $doc */
185
+ $doc = $node->ownerDocument;
186
+
187
+ $html = '';
188
+ foreach ($node->childNodes as $child) {
189
+ $html .= $doc->saveXML($child);
190
+ }
191
+ return $html;
192
+ }
193
+
172
194
  protected static function getXmlErrors(): array
173
195
  {
174
196
  $errors = libxml_get_errors();
@@ -283,25 +305,24 @@ class TemplateCompiler
283
305
  string $componentName,
284
306
  array $incomingProps
285
307
  ): string {
286
- $mapping = self::selectComponentMapping($componentName);
287
- $instance = self::initializeComponentInstance($mapping, $incomingProps);
308
+ $incomingProps = self::sanitizeIncomingProps($incomingProps);
309
+ $mapping = self::selectComponentMapping($componentName);
310
+ $instance = self::initializeComponentInstance($mapping, $incomingProps);
288
311
 
289
312
  $childHtml = '';
290
313
  foreach ($node->childNodes as $c) {
291
314
  $childHtml .= self::processNode($c);
292
315
  }
316
+ $instance->children = self::sanitizeEventAttributes($childHtml);
293
317
 
294
- $childHtml = self::sanitizeEventAttributes($childHtml);
295
- $instance->children = $childHtml;
296
-
297
- $baseId = 's' . base_convert(sprintf('%u', crc32($mapping['className'])), 10, 36);
298
- $idx = self::$componentInstanceCounts[$baseId] ?? 0;
318
+ $baseId = 's' . base_convert(sprintf('%u', crc32($mapping['className'])), 10, 36);
319
+ $idx = self::$componentInstanceCounts[$baseId] ?? 0;
299
320
  self::$componentInstanceCounts[$baseId] = $idx + 1;
300
321
  $sectionId = $idx === 0 ? $baseId : "{$baseId}{$idx}";
301
322
 
302
- $html = $instance->render();
303
- $fragDom = self::convertToXml($html, false);
304
- $xpath = new DOMXPath($fragDom);
323
+ $html = $instance->render();
324
+ $fragDom = self::convertToXml($html, false);
325
+ $xpath = new DOMXPath($fragDom);
305
326
 
306
327
  /** @var DOMElement $el */
307
328
  foreach ($xpath->query('//*') as $el) {
@@ -319,14 +340,11 @@ class TemplateCompiler
319
340
  $value = $attr->value;
320
341
 
321
342
  if (str_starts_with($name, 'pp-original-')) {
322
- $origName = substr($name, strlen('pp-original-'));
323
- $originalEvents[$origName] = $value;
343
+ $origName = substr($name, strlen('pp-original-'));
344
+ $originalEvents[$origName] = $value;
324
345
  } elseif (str_starts_with($name, 'on')) {
325
346
  $event = substr($name, 2);
326
- if (
327
- in_array($event, PrismaPHPSettings::$htmlEvents, true)
328
- && trim((string)$value) !== ''
329
- ) {
347
+ if ($value !== '' && in_array($event, PrismaPHPSettings::$htmlEvents, true)) {
330
348
  $componentEvents[$name] = $value;
331
349
  }
332
350
  }
@@ -336,18 +354,10 @@ class TemplateCompiler
336
354
  foreach (array_keys($componentEvents) as $k) $el->removeAttribute($k);
337
355
 
338
356
  foreach ($componentEvents as $evAttr => $compValue) {
339
-
340
- $el->setAttribute(
341
- "data-pp-child-{$evAttr}",
342
- $compValue
343
- );
357
+ $el->setAttribute("data-pp-child-{$evAttr}", $compValue);
344
358
 
345
359
  if (isset($originalEvents[$evAttr])) {
346
- $parentValue = $originalEvents[$evAttr];
347
- $el->setAttribute(
348
- "data-pp-parent-{$evAttr}",
349
- $parentValue
350
- );
360
+ $el->setAttribute("data-pp-parent-{$evAttr}", $originalEvents[$evAttr]);
351
361
  unset($originalEvents[$evAttr]);
352
362
  }
353
363
  }
@@ -365,11 +375,7 @@ class TemplateCompiler
365
375
  }
366
376
  }
367
377
 
368
- $htmlOut = '';
369
- foreach ($root->childNodes as $child) {
370
- $htmlOut .= $fragDom->saveXML($child);
371
- }
372
-
378
+ $htmlOut = self::innerXml($fragDom);
373
379
  if (
374
380
  str_contains($htmlOut, '{{') ||
375
381
  self::hasComponentTag($htmlOut) ||
@@ -381,9 +387,24 @@ class TemplateCompiler
381
387
  return $htmlOut;
382
388
  }
383
389
 
390
+ protected static function sanitizeIncomingProps(array $props): array
391
+ {
392
+ foreach ($props as $key => $val) {
393
+ if (str_starts_with($key, 'on')) {
394
+ $event = substr($key, 2);
395
+ if (in_array($event, PrismaPHPSettings::$htmlEvents, true) && trim((string)$val) !== '') {
396
+ $props["pp-original-on{$event}"] = (string)$val;
397
+ unset($props[$key]);
398
+ }
399
+ }
400
+ }
401
+
402
+ return $props;
403
+ }
404
+
384
405
  protected static function sanitizeEventAttributes(string $html): string
385
406
  {
386
- $fragDom = TemplateCompiler::convertToXml($html, false);
407
+ $fragDom = self::convertToXml($html, false);
387
408
  $xpath = new DOMXPath($fragDom);
388
409
 
389
410
  /** @var DOMElement $el */
@@ -391,31 +412,26 @@ class TemplateCompiler
391
412
  foreach (iterator_to_array($el->attributes) as $attr) {
392
413
  $name = strtolower($attr->name);
393
414
 
394
- if (str_starts_with($name, 'on')) {
395
- $event = substr($name, 2);
396
- $value = trim($attr->value);
415
+ if (!str_starts_with($name, 'on')) {
416
+ continue;
417
+ }
397
418
 
398
- if (
399
- $value !== '' &&
400
- in_array($event, PrismaPHPSettings::$htmlEvents, true)
401
- ) {
402
- $el->setAttribute("pp-original-on{$event}", $value);
403
- }
419
+ $event = substr($name, 2);
420
+ $value = trim($attr->value);
404
421
 
405
- $el->removeAttribute($name);
422
+ if ($value !== '' && in_array($event, PrismaPHPSettings::$htmlEvents, true)) {
423
+ $el->setAttribute("pp-original-on{$event}", $value);
406
424
  }
425
+
426
+ $el->removeAttribute($name);
407
427
  }
408
428
  }
409
429
 
410
- /** @var DOMDocument $doc */
411
- $doc = $fragDom;
412
- $body = $doc->getElementsByTagName('body')[0] ?? $doc;
430
+ $body = $fragDom->getElementsByTagName('body')[0] ?? null;
413
431
 
414
- $html = '';
415
- foreach ($body->childNodes as $child) {
416
- $html .= $doc->saveHTML($child);
417
- }
418
- return $html;
432
+ return $body instanceof DOMElement
433
+ ? self::innerXml($body)
434
+ : self::innerXml($fragDom);
419
435
  }
420
436
 
421
437
  private static function selectComponentMapping(string $componentName): array
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-prisma-php-app",
3
- "version": "3.0.0-beta.2",
3
+ "version": "3.0.0-beta.4",
4
4
  "description": "Prisma-PHP: A Revolutionary Library Bridging PHP with Prisma ORM",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",