create-prisma-php-app 4.2.2 → 4.2.3

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.
@@ -260,8 +260,9 @@ final class Bootstrap extends RuntimeException
260
260
 
261
261
  $layoutsToInclude = self::collectLayouts($pathname, $groupFolder, $dynamicRoute ?? null);
262
262
  } else {
263
- $includePath = $baseDir . self::getFilePrecedence();
264
- $layoutsToInclude = self::collectRootLayouts();
263
+ $contentData = self::getFilePrecedence();
264
+ $includePath = $baseDir . $contentData['file'];
265
+ $layoutsToInclude = self::collectRootLayouts($contentData['file']);
265
266
  }
266
267
 
267
268
  return [
@@ -395,7 +396,7 @@ final class Bootstrap extends RuntimeException
395
396
  return $layoutsToInclude;
396
397
  }
397
398
 
398
- private static function collectRootLayouts(): array
399
+ private static function collectRootLayouts(?string $matchedContentFile = null): array
399
400
  {
400
401
  $layoutsToInclude = [];
401
402
  $baseDir = APP_PATH;
@@ -404,7 +405,7 @@ final class Bootstrap extends RuntimeException
404
405
  if (self::fileExistsCached($rootLayout)) {
405
406
  $layoutsToInclude[] = $rootLayout;
406
407
  } else {
407
- $layoutsToInclude = self::findFirstGroupLayout();
408
+ $layoutsToInclude = self::findFirstGroupLayout($matchedContentFile);
408
409
 
409
410
  if (empty($layoutsToInclude)) {
410
411
  return [];
@@ -414,13 +415,32 @@ final class Bootstrap extends RuntimeException
414
415
  return $layoutsToInclude;
415
416
  }
416
417
 
417
- private static function findFirstGroupLayout(): array
418
+ private static function findFirstGroupLayout(?string $matchedContentFile = null): array
418
419
  {
419
420
  $baseDir = APP_PATH;
420
421
  $layoutsToInclude = [];
421
422
 
422
423
  if (is_dir($baseDir)) {
423
424
  $items = scandir($baseDir);
425
+
426
+ if ($matchedContentFile) {
427
+ foreach ($items as $item) {
428
+ if ($item === '.' || $item === '..') {
429
+ continue;
430
+ }
431
+
432
+ if (preg_match('/^\([^)]+\)$/', $item)) {
433
+ if (strpos($matchedContentFile, "/$item/") === 0) {
434
+ $groupLayoutPath = $baseDir . '/' . $item . '/layout.php';
435
+ if (self::fileExistsCached($groupLayoutPath)) {
436
+ $layoutsToInclude[] = $groupLayoutPath;
437
+ return $layoutsToInclude;
438
+ }
439
+ }
440
+ }
441
+ }
442
+ }
443
+
424
444
  foreach ($items as $item) {
425
445
  if ($item === '.' || $item === '..') {
426
446
  continue;
@@ -439,19 +459,20 @@ final class Bootstrap extends RuntimeException
439
459
  return $layoutsToInclude;
440
460
  }
441
461
 
442
- private static function getFilePrecedence(): ?string
462
+ private static function getFilePrecedence(): array
443
463
  {
444
464
  $baseDir = APP_PATH;
465
+ $result = ['file' => null];
445
466
 
446
467
  foreach (PrismaPHPSettings::$routeFiles as $route) {
447
468
  if (pathinfo($route, PATHINFO_EXTENSION) !== 'php') {
448
469
  continue;
449
470
  }
450
471
  if (preg_match('/^\.\/src\/app\/route\.php$/', $route)) {
451
- return '/route.php';
472
+ return ['file' => '/route.php'];
452
473
  }
453
474
  if (preg_match('/^\.\/src\/app\/index\.php$/', $route)) {
454
- return '/index.php';
475
+ return ['file' => '/index.php'];
455
476
  }
456
477
  }
457
478
 
@@ -466,16 +487,16 @@ final class Bootstrap extends RuntimeException
466
487
  $groupDir = $baseDir . '/' . $item;
467
488
 
468
489
  if (file_exists($groupDir . '/route.php')) {
469
- return '/' . $item . '/route.php';
490
+ return ['file' => '/' . $item . '/route.php'];
470
491
  }
471
492
  if (file_exists($groupDir . '/index.php')) {
472
- return '/' . $item . '/index.php';
493
+ return ['file' => '/' . $item . '/index.php'];
473
494
  }
474
495
  }
475
496
  }
476
497
  }
477
498
 
478
- return null;
499
+ return $result;
479
500
  }
480
501
 
481
502
  private static function uriExtractor(string $scriptUrl): string
@@ -1124,6 +1145,22 @@ final class Bootstrap extends RuntimeException
1124
1145
 
1125
1146
  return Request::$isAjax || Request::$isXFileRequest || Request::$fileToInclude === 'route.php';
1126
1147
  }
1148
+
1149
+ public static function applyRootLayoutId(string $html): string
1150
+ {
1151
+ $rootLayoutPath = self::$layoutsToInclude[0] ?? self::$parentLayoutPath;
1152
+ $rootLayoutId = !empty($rootLayoutPath) ? md5($rootLayoutPath) : 'default-root';
1153
+
1154
+ header('X-PP-Root-Layout: ' . $rootLayoutId);
1155
+
1156
+ $rootLayoutMeta = '<meta name="pp-root-layout" content="' . $rootLayoutId . '">';
1157
+
1158
+ if (strpos($html, '<head>') !== false) {
1159
+ return preg_replace('/<head>/', "<head>\n $rootLayoutMeta", $html, 1);
1160
+ }
1161
+
1162
+ return $rootLayoutMeta . $html;
1163
+ }
1127
1164
  }
1128
1165
 
1129
1166
  Bootstrap::run();
@@ -1233,6 +1270,8 @@ try {
1233
1270
  MainLayout::$html = ob_get_clean();
1234
1271
  MainLayout::$html = TemplateCompiler::compile(MainLayout::$html);
1235
1272
  MainLayout::$html = TemplateCompiler::injectDynamicContent(MainLayout::$html);
1273
+ MainLayout::$html = Bootstrap::applyRootLayoutId(MainLayout::$html);
1274
+
1236
1275
  MainLayout::$html = "<!DOCTYPE html>\n" . MainLayout::$html;
1237
1276
 
1238
1277
  if (