create-prisma-php-app 3.0.0-beta.3 → 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
 
@@ -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();
@@ -284,25 +306,23 @@ class TemplateCompiler
284
306
  array $incomingProps
285
307
  ): string {
286
308
  $incomingProps = self::sanitizeIncomingProps($incomingProps);
287
- $mapping = self::selectComponentMapping($componentName);
288
- $instance = self::initializeComponentInstance($mapping, $incomingProps);
309
+ $mapping = self::selectComponentMapping($componentName);
310
+ $instance = self::initializeComponentInstance($mapping, $incomingProps);
289
311
 
290
312
  $childHtml = '';
291
313
  foreach ($node->childNodes as $c) {
292
314
  $childHtml .= self::processNode($c);
293
315
  }
316
+ $instance->children = self::sanitizeEventAttributes($childHtml);
294
317
 
295
- $childHtml = self::sanitizeEventAttributes($childHtml);
296
- $instance->children = $childHtml;
297
-
298
- $baseId = 's' . base_convert(sprintf('%u', crc32($mapping['className'])), 10, 36);
299
- $idx = self::$componentInstanceCounts[$baseId] ?? 0;
318
+ $baseId = 's' . base_convert(sprintf('%u', crc32($mapping['className'])), 10, 36);
319
+ $idx = self::$componentInstanceCounts[$baseId] ?? 0;
300
320
  self::$componentInstanceCounts[$baseId] = $idx + 1;
301
321
  $sectionId = $idx === 0 ? $baseId : "{$baseId}{$idx}";
302
322
 
303
- $html = $instance->render();
304
- $fragDom = self::convertToXml($html, false);
305
- $xpath = new DOMXPath($fragDom);
323
+ $html = $instance->render();
324
+ $fragDom = self::convertToXml($html, false);
325
+ $xpath = new DOMXPath($fragDom);
306
326
 
307
327
  /** @var DOMElement $el */
308
328
  foreach ($xpath->query('//*') as $el) {
@@ -320,14 +340,11 @@ class TemplateCompiler
320
340
  $value = $attr->value;
321
341
 
322
342
  if (str_starts_with($name, 'pp-original-')) {
323
- $origName = substr($name, strlen('pp-original-'));
324
- $originalEvents[$origName] = $value;
343
+ $origName = substr($name, strlen('pp-original-'));
344
+ $originalEvents[$origName] = $value;
325
345
  } elseif (str_starts_with($name, 'on')) {
326
346
  $event = substr($name, 2);
327
- if (
328
- in_array($event, PrismaPHPSettings::$htmlEvents, true)
329
- && trim((string)$value) !== ''
330
- ) {
347
+ if ($value !== '' && in_array($event, PrismaPHPSettings::$htmlEvents, true)) {
331
348
  $componentEvents[$name] = $value;
332
349
  }
333
350
  }
@@ -337,18 +354,10 @@ class TemplateCompiler
337
354
  foreach (array_keys($componentEvents) as $k) $el->removeAttribute($k);
338
355
 
339
356
  foreach ($componentEvents as $evAttr => $compValue) {
340
-
341
- $el->setAttribute(
342
- "data-pp-child-{$evAttr}",
343
- $compValue
344
- );
357
+ $el->setAttribute("data-pp-child-{$evAttr}", $compValue);
345
358
 
346
359
  if (isset($originalEvents[$evAttr])) {
347
- $parentValue = $originalEvents[$evAttr];
348
- $el->setAttribute(
349
- "data-pp-parent-{$evAttr}",
350
- $parentValue
351
- );
360
+ $el->setAttribute("data-pp-parent-{$evAttr}", $originalEvents[$evAttr]);
352
361
  unset($originalEvents[$evAttr]);
353
362
  }
354
363
  }
@@ -366,11 +375,7 @@ class TemplateCompiler
366
375
  }
367
376
  }
368
377
 
369
- $htmlOut = '';
370
- foreach ($root->childNodes as $child) {
371
- $htmlOut .= $fragDom->saveXML($child);
372
- }
373
-
378
+ $htmlOut = self::innerXml($fragDom);
374
379
  if (
375
380
  str_contains($htmlOut, '{{') ||
376
381
  self::hasComponentTag($htmlOut) ||
@@ -399,7 +404,7 @@ class TemplateCompiler
399
404
 
400
405
  protected static function sanitizeEventAttributes(string $html): string
401
406
  {
402
- $fragDom = TemplateCompiler::convertToXml($html, false);
407
+ $fragDom = self::convertToXml($html, false);
403
408
  $xpath = new DOMXPath($fragDom);
404
409
 
405
410
  /** @var DOMElement $el */
@@ -407,31 +412,26 @@ class TemplateCompiler
407
412
  foreach (iterator_to_array($el->attributes) as $attr) {
408
413
  $name = strtolower($attr->name);
409
414
 
410
- if (str_starts_with($name, 'on')) {
411
- $event = substr($name, 2);
412
- $value = trim($attr->value);
415
+ if (!str_starts_with($name, 'on')) {
416
+ continue;
417
+ }
413
418
 
414
- if (
415
- $value !== '' &&
416
- in_array($event, PrismaPHPSettings::$htmlEvents, true)
417
- ) {
418
- $el->setAttribute("pp-original-on{$event}", $value);
419
- }
419
+ $event = substr($name, 2);
420
+ $value = trim($attr->value);
420
421
 
421
- $el->removeAttribute($name);
422
+ if ($value !== '' && in_array($event, PrismaPHPSettings::$htmlEvents, true)) {
423
+ $el->setAttribute("pp-original-on{$event}", $value);
422
424
  }
425
+
426
+ $el->removeAttribute($name);
423
427
  }
424
428
  }
425
429
 
426
- /** @var DOMDocument $doc */
427
- $doc = $fragDom;
428
- $body = $doc->getElementsByTagName('body')[0] ?? $doc;
430
+ $body = $fragDom->getElementsByTagName('body')[0] ?? null;
429
431
 
430
- $html = '';
431
- foreach ($body->childNodes as $child) {
432
- $html .= $doc->saveHTML($child);
433
- }
434
- return $html;
432
+ return $body instanceof DOMElement
433
+ ? self::innerXml($body)
434
+ : self::innerXml($fragDom);
435
435
  }
436
436
 
437
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.3",
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",