create-prisma-php-app 1.25.2 → 1.25.501

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.
@@ -7,13 +7,21 @@ if (session_status() == PHP_SESSION_NONE) {
7
7
  require_once __DIR__ . '/vendor/autoload.php';
8
8
  require_once __DIR__ . '/settings/paths.php';
9
9
 
10
+ use Dotenv\Dotenv;
11
+ use Lib\Request;
12
+ use Lib\PrismaPHPSettings;
13
+ use Lib\StateManager;
10
14
  use Lib\Middleware\AuthMiddleware;
11
15
  use Lib\Auth\Auth;
12
- use Dotenv\Dotenv;
16
+ use Lib\MainLayout;
13
17
 
14
18
  $dotenv = Dotenv::createImmutable(\DOCUMENT_PATH);
15
19
  $dotenv->load();
16
20
 
21
+ PrismaPHPSettings::init();
22
+ Request::init();
23
+ StateManager::init();
24
+
17
25
  date_default_timezone_set($_ENV['APP_TIMEZONE'] ?? 'UTC');
18
26
 
19
27
  function determineContentToInclude()
@@ -37,24 +45,24 @@ function determineContentToInclude()
37
45
  * ============================================
38
46
  */
39
47
  $scriptUrl = explode('?', $requestUri, 2)[0];
40
- $uri = $_SERVER['SCRIPT_URL'] ?? $scriptUrl;
41
- $uri = ltrim($uri, '/');
48
+ $pathname = $_SERVER['SCRIPT_URL'] ?? $scriptUrl;
49
+ $pathname = ltrim($pathname, '/');
42
50
  $baseDir = APP_PATH;
43
51
  $includePath = '';
44
52
  $layoutsToInclude = [];
45
53
 
46
54
  /**
47
55
  * ============ Middleware Management ============
48
- * AuthMiddleware is invoked to handle authentication logic for the current route ($uri).
56
+ * AuthMiddleware is invoked to handle authentication logic for the current route ($pathname).
49
57
  * ================================================
50
58
  */
51
- AuthMiddleware::handle($uri);
59
+ AuthMiddleware::handle($pathname);
52
60
  /**
53
61
  * ============ End of Middleware Management ======
54
62
  * ================================================
55
63
  */
56
64
 
57
- $isDirectAccessToPrivateRoute = preg_match('/_/', $uri);
65
+ $isDirectAccessToPrivateRoute = preg_match('/_/', $pathname);
58
66
  if ($isDirectAccessToPrivateRoute) {
59
67
  $sameSiteFetch = false;
60
68
  $serverFetchSite = $_SERVER['HTTP_SEC_FETCH_SITE'] ?? '';
@@ -63,12 +71,12 @@ function determineContentToInclude()
63
71
  }
64
72
 
65
73
  if (!$sameSiteFetch) {
66
- return ['path' => $includePath, 'layouts' => $layoutsToInclude, 'uri' => $uri, 'requestUri' => $requestUri];
74
+ return ['path' => $includePath, 'layouts' => $layoutsToInclude, 'pathname' => $pathname, 'uri' => $requestUri];
67
75
  }
68
76
  }
69
77
 
70
- if ($uri) {
71
- $groupFolder = findGroupFolder($uri);
78
+ if ($pathname) {
79
+ $groupFolder = findGroupFolder($pathname);
72
80
  if ($groupFolder) {
73
81
  $path = __DIR__ . $groupFolder;
74
82
  if (file_exists($path)) {
@@ -77,7 +85,7 @@ function determineContentToInclude()
77
85
  }
78
86
 
79
87
  if (empty($includePath)) {
80
- $dynamicRoute = dynamicRoute($uri);
88
+ $dynamicRoute = dynamicRoute($pathname);
81
89
  if ($dynamicRoute) {
82
90
  $path = __DIR__ . $dynamicRoute;
83
91
  if (file_exists($path)) {
@@ -88,12 +96,12 @@ function determineContentToInclude()
88
96
 
89
97
  $currentPath = $baseDir;
90
98
  $getGroupFolder = getGroupFolder($groupFolder);
91
- $modifiedUri = $uri;
99
+ $modifiedPathname = $pathname;
92
100
  if (!empty($getGroupFolder)) {
93
- $modifiedUri = trim($getGroupFolder, "/src/app/");
101
+ $modifiedPathname = trim($getGroupFolder, "/src/app/");
94
102
  }
95
103
 
96
- foreach (explode('/', $modifiedUri) as $segment) {
104
+ foreach (explode('/', $modifiedPathname) as $segment) {
97
105
  if (empty($segment)) continue;
98
106
 
99
107
  $currentPath .= '/' . $segment;
@@ -125,14 +133,12 @@ function determineContentToInclude()
125
133
  $includePath = $baseDir . getFilePrecedence();
126
134
  }
127
135
 
128
- return ['path' => $includePath, 'layouts' => $layoutsToInclude, 'uri' => $uri, 'requestUri' => $requestUri];
136
+ return ['path' => $includePath, 'layouts' => $layoutsToInclude, 'pathname' => $pathname, 'uri' => $requestUri];
129
137
  }
130
138
 
131
139
  function getFilePrecedence()
132
140
  {
133
- global $_filesListRoutes;
134
-
135
- foreach ($_filesListRoutes as $route) {
141
+ foreach (PrismaPHPSettings::$routeFiles as $route) {
136
142
  // Check if the file has a .php extension
137
143
  if (pathinfo($route, PATHINFO_EXTENSION) !== 'php') {
138
144
  continue; // Skip files that are not PHP files
@@ -155,9 +161,7 @@ function getFilePrecedence()
155
161
 
156
162
  function uriExtractor(string $scriptUrl): string
157
163
  {
158
- global $_prismaPHPSettings;
159
-
160
- $projectName = $_prismaPHPSettings['projectName'] ?? '';
164
+ $projectName = PrismaPHPSettings::$option['projectName'] ?? '';
161
165
  if (empty($projectName)) {
162
166
  return "/";
163
167
  }
@@ -170,18 +174,10 @@ function uriExtractor(string $scriptUrl): string
170
174
  return "/";
171
175
  }
172
176
 
173
- function getFilesListRoutes()
177
+ function findGroupFolder($pathname): string
174
178
  {
175
- $jsonFileName = SETTINGS_PATH . '/files-list.json';
176
- $routeFiles = file_exists($jsonFileName) ? json_decode(file_get_contents($jsonFileName), true) : [];
177
-
178
- return $routeFiles;
179
- }
180
-
181
- function findGroupFolder($uri): string
182
- {
183
- $uriSegments = explode('/', $uri);
184
- foreach ($uriSegments as $segment) {
179
+ $pathnameSegments = explode('/', $pathname);
180
+ foreach ($pathnameSegments as $segment) {
185
181
  if (!empty($segment)) {
186
182
  if (isGroupIdentifier($segment)) {
187
183
  return $segment;
@@ -189,7 +185,7 @@ function findGroupFolder($uri): string
189
185
  }
190
186
  }
191
187
 
192
- $matchedGroupFolder = matchGroupFolder($uri);
188
+ $matchedGroupFolder = matchGroupFolder($pathname);
193
189
  if ($matchedGroupFolder) {
194
190
  return $matchedGroupFolder;
195
191
  } else {
@@ -197,16 +193,14 @@ function findGroupFolder($uri): string
197
193
  }
198
194
  }
199
195
 
200
- function dynamicRoute($uri)
196
+ function dynamicRoute($pathname)
201
197
  {
202
- global $_filesListRoutes, $dynamicRouteParams;
203
-
204
- $uriMatch = null;
205
- $normalizedUri = ltrim(str_replace('\\', '/', $uri), './');
206
- $normalizedUriEdited = "src/app/$normalizedUri";
207
- $uriSegments = explode('/', $normalizedUriEdited);
198
+ $pathnameMatch = null;
199
+ $normalizedPathname = ltrim(str_replace('\\', '/', $pathname), './');
200
+ $normalizedPathnameEdited = "src/app/$normalizedPathname";
201
+ $pathnameSegments = explode('/', $normalizedPathnameEdited);
208
202
 
209
- foreach ($_filesListRoutes as $route) {
203
+ foreach (PrismaPHPSettings::$routeFiles as $route) {
210
204
  $normalizedRoute = trim(str_replace('\\', '/', $route), '.');
211
205
 
212
206
  // Skip non-.php files to improve performance
@@ -223,23 +217,23 @@ function dynamicRoute($uri)
223
217
  $singleDynamic = preg_match_all('/\[[^\]]+\]/', $normalizedRoute, $matches) === 1 && strpos($normalizedRoute, '[...') === false;
224
218
 
225
219
  if ($singleDynamic) {
226
- $segmentMatch = singleDynamicRoute($uriSegments, $filteredRouteSegments);
220
+ $segmentMatch = singleDynamicRoute($pathnameSegments, $filteredRouteSegments);
227
221
  $index = array_search($segmentMatch, $filteredRouteSegments);
228
222
 
229
- if ($index !== false && isset($uriSegments[$index])) {
223
+ if ($index !== false && isset($pathnameSegments[$index])) {
230
224
  $trimSegmentMatch = trim($segmentMatch, '[]');
231
- $dynamicRouteParams = new \ArrayObject([$trimSegmentMatch => $uriSegments[$index]], \ArrayObject::ARRAY_AS_PROPS);
225
+ Request::$dynamicParams = new \ArrayObject([$trimSegmentMatch => $pathnameSegments[$index]], \ArrayObject::ARRAY_AS_PROPS);
232
226
 
233
- $dynamicRouteUri = str_replace($segmentMatch, $uriSegments[$index], $normalizedRoute);
234
- $dynamicRouteUri = preg_replace('/\(.+\)/', '', $dynamicRouteUri);
235
- $dynamicRouteUri = preg_replace('/\/+/', '/', $dynamicRouteUri);
236
- $dynamicRouteUriDirname = rtrim(dirname($dynamicRouteUri), '/');
227
+ $dynamicRoutePathname = str_replace($segmentMatch, $pathnameSegments[$index], $normalizedRoute);
228
+ $dynamicRoutePathname = preg_replace('/\(.+\)/', '', $dynamicRoutePathname);
229
+ $dynamicRoutePathname = preg_replace('/\/+/', '/', $dynamicRoutePathname);
230
+ $dynamicRoutePathnameDirname = rtrim(dirname($dynamicRoutePathname), '/');
237
231
 
238
- $expectedUri = rtrim('/src/app/' . $normalizedUri, '/');
232
+ $expectedPathname = rtrim('/src/app/' . $normalizedPathname, '/');
239
233
 
240
234
  if (strpos($normalizedRoute, 'route.php') !== false || strpos($normalizedRoute, 'index.php') !== false) {
241
- if ($expectedUri === $dynamicRouteUriDirname) {
242
- $uriMatch = $normalizedRoute;
235
+ if ($expectedPathname === $dynamicRoutePathnameDirname) {
236
+ $pathnameMatch = $normalizedRoute;
243
237
  break;
244
238
  }
245
239
  }
@@ -250,20 +244,20 @@ function dynamicRoute($uri)
250
244
  $cleanedNormalizedRoute = preg_replace('/\/+/', '/', $cleanedNormalizedRoute);
251
245
  $dynamicSegmentRoute = preg_replace('/\[\.\.\..*?\].*/', '', $cleanedNormalizedRoute);
252
246
 
253
- // Check if the normalized URI starts with the cleaned route
254
- if (strpos("/src/app/$normalizedUri", $dynamicSegmentRoute) === 0) {
255
- $trimmedUri = str_replace($dynamicSegmentRoute, '', "/src/app/$normalizedUri");
256
- $uriParts = explode('/', trim($trimmedUri, '/'));
247
+ // Check if the normalized pathname starts with the cleaned route
248
+ if (strpos("/src/app/$normalizedPathname", $dynamicSegmentRoute) === 0) {
249
+ $trimmedPathname = str_replace($dynamicSegmentRoute, '', "/src/app/$normalizedPathname");
250
+ $pathnameParts = explode('/', trim($trimmedPathname, '/'));
257
251
 
258
252
  // Extract the dynamic segment content
259
253
  if (preg_match('/\[\.\.\.(.*?)\]/', $normalizedRoute, $matches)) {
260
254
  $dynamicParam = $matches[1];
261
- $dynamicRouteParams = new \ArrayObject([$dynamicParam => $uriParts], \ArrayObject::ARRAY_AS_PROPS);
255
+ Request::$dynamicParams = new \ArrayObject([$dynamicParam => $pathnameParts], \ArrayObject::ARRAY_AS_PROPS);
262
256
  }
263
257
 
264
258
  // Check for 'route.php'
265
259
  if (strpos($normalizedRoute, 'route.php') !== false) {
266
- $uriMatch = $normalizedRoute;
260
+ $pathnameMatch = $normalizedRoute;
267
261
  break;
268
262
  }
269
263
 
@@ -272,16 +266,16 @@ function dynamicRoute($uri)
272
266
  $segmentMatch = "[...$dynamicParam]";
273
267
  $index = array_search($segmentMatch, $filteredRouteSegments);
274
268
 
275
- if ($index !== false && isset($uriSegments[$index])) {
276
- // Generate the dynamic URI
277
- $dynamicRouteUri = str_replace($segmentMatch, implode('/', $uriParts), $cleanedNormalizedRoute);
278
- $dynamicRouteUriDirname = rtrim(dirname($dynamicRouteUri), '/');
269
+ if ($index !== false && isset($pathnameSegments[$index])) {
270
+ // Generate the dynamic pathname
271
+ $dynamicRoutePathname = str_replace($segmentMatch, implode('/', $pathnameParts), $cleanedNormalizedRoute);
272
+ $dynamicRoutePathnameDirname = rtrim(dirname($dynamicRoutePathname), '/');
279
273
 
280
- $expectedUri = rtrim("/src/app/$normalizedUri", '/');
274
+ $expectedPathname = rtrim("/src/app/$normalizedPathname", '/');
281
275
 
282
- // Compare the expected and dynamic URIs
283
- if ($expectedUri === $dynamicRouteUriDirname) {
284
- $uriMatch = $normalizedRoute;
276
+ // Compare the expected and dynamic pathname
277
+ if ($expectedPathname === $dynamicRoutePathnameDirname) {
278
+ $pathnameMatch = $normalizedRoute;
285
279
  break;
286
280
  }
287
281
  }
@@ -290,7 +284,7 @@ function dynamicRoute($uri)
290
284
  }
291
285
  }
292
286
 
293
- return $uriMatch;
287
+ return $pathnameMatch;
294
288
  }
295
289
 
296
290
  function isGroupIdentifier($segment): bool
@@ -300,15 +294,13 @@ function isGroupIdentifier($segment): bool
300
294
 
301
295
  function matchGroupFolder($constructedPath): ?string
302
296
  {
303
- global $_filesListRoutes;
304
-
305
297
  $bestMatch = null;
306
298
  $normalizedConstructedPath = ltrim(str_replace('\\', '/', $constructedPath), './');
307
299
 
308
300
  $routeFile = "/src/app/$normalizedConstructedPath/route.php";
309
301
  $indexFile = "/src/app/$normalizedConstructedPath/index.php";
310
302
 
311
- foreach ($_filesListRoutes as $route) {
303
+ foreach (PrismaPHPSettings::$routeFiles as $route) {
312
304
  if (pathinfo($route, PATHINFO_EXTENSION) !== 'php') {
313
305
  continue;
314
306
  }
@@ -327,10 +319,10 @@ function matchGroupFolder($constructedPath): ?string
327
319
  return $bestMatch;
328
320
  }
329
321
 
330
- function getGroupFolder($uri): string
322
+ function getGroupFolder($pathname): string
331
323
  {
332
- $lastSlashPos = strrpos($uri, '/');
333
- $pathWithoutFile = substr($uri, 0, $lastSlashPos);
324
+ $lastSlashPos = strrpos($pathname, '/');
325
+ $pathWithoutFile = substr($pathname, 0, $lastSlashPos);
334
326
 
335
327
  if (preg_match('/\(([^)]+)\)[^()]*$/', $pathWithoutFile, $matches)) {
336
328
  return $pathWithoutFile;
@@ -339,14 +331,14 @@ function getGroupFolder($uri): string
339
331
  return "";
340
332
  }
341
333
 
342
- function singleDynamicRoute($uriSegments, $routeSegments)
334
+ function singleDynamicRoute($pathnameSegments, $routeSegments)
343
335
  {
344
336
  $segmentMatch = "";
345
337
  foreach ($routeSegments as $index => $segment) {
346
338
  if (preg_match('/^\[[^\]]+\]$/', $segment)) {
347
339
  return "{$segment}";
348
340
  } else {
349
- if ($segment !== $uriSegments[$index]) {
341
+ if ($segment !== $pathnameSegments[$index]) {
350
342
  return $segmentMatch;
351
343
  }
352
344
  }
@@ -358,9 +350,8 @@ function checkForDuplicateRoutes()
358
350
  {
359
351
  if (isset($_ENV['APP_ENV']) && $_ENV['APP_ENV'] === 'production') return;
360
352
 
361
- global $_filesListRoutes;
362
353
  $normalizedRoutesMap = [];
363
- foreach ($_filesListRoutes as $route) {
354
+ foreach (PrismaPHPSettings::$routeFiles as $route) {
364
355
  if (pathinfo($route, PATHINFO_EXTENSION) !== 'php') {
365
356
  continue;
366
357
  }
@@ -395,25 +386,24 @@ function checkForDuplicateRoutes()
395
386
  }
396
387
  }
397
388
 
398
- function containsChildContent($filePath)
389
+ function containsChildLayoutChildren($filePath)
399
390
  {
400
391
  $fileContent = file_get_contents($filePath);
401
392
 
402
- // Regular expression to match different ways of echoing $childContent
403
- $pattern = '/\<\?=\s*\$childContent\s*;?\s*\?>|echo\s*\$childContent\s*;?/';
393
+ // Updated regular expression to match MainLayout::$childLayoutChildren
394
+ $pattern = '/\<\?=\s*MainLayout::\$childLayoutChildren\s*;?\s*\?>|echo\s*MainLayout::\$childLayoutChildren\s*;?/';
404
395
 
405
- // Return true if $childContent variables are found, false otherwise
396
+ // Return true if MainLayout::$childLayoutChildren variables are found, false otherwise
406
397
  return preg_match($pattern, $fileContent) === 1;
407
398
  }
408
399
 
409
- function containsContent($filePath)
400
+ function containsChildren($filePath)
410
401
  {
411
402
  $fileContent = file_get_contents($filePath);
412
403
 
413
- // Improved regular expression to match different ways of echoing $content
414
- $pattern = '/\<\?=\s*\$content\s*;?\s*\?>|echo\s*\$content\s*;?/';
415
-
416
- // Return true if content variables are found, false otherwise
404
+ // Updated regular expression to match MainLayout::$children
405
+ $pattern = '/\<\?=\s*MainLayout::\$children\s*;?\s*\?>|echo\s*MainLayout::\$children\s*;?/';
406
+ // Return true if the new content variables are found, false otherwise
417
407
  return preg_match($pattern, $fileContent) === 1;
418
408
  }
419
409
 
@@ -535,25 +525,23 @@ function wireCallback()
535
525
 
536
526
  function getLoadingsFiles()
537
527
  {
538
- global $_filesListRoutes, $uri, $pathname, $dynamicRouteParams, $params, $referer;
539
-
540
- $loadingFiles = array_filter($_filesListRoutes, function ($route) {
528
+ $loadingFiles = array_filter(PrismaPHPSettings::$routeFiles, function ($route) {
541
529
  $normalizedRoute = str_replace('\\', '/', $route);
542
530
  return preg_match('/\/loading\.php$/', $normalizedRoute);
543
531
  });
544
532
 
545
- $haveLoadingFileContent = array_reduce($loadingFiles, function ($carry, $route) use ($uri, $pathname, $dynamicRouteParams, $params, $referer) {
533
+ $haveLoadingFileContent = array_reduce($loadingFiles, function ($carry, $route) {
546
534
  $normalizeUri = str_replace('\\', '/', $route);
547
535
  $fileUrl = str_replace('./src/app', '', $normalizeUri);
548
536
  $route = str_replace(['\\', './'], ['/', ''], $route);
549
537
 
550
538
  ob_start();
551
- include($route); // This will execute the PHP code in loading.php
552
- $content = ob_get_clean();
539
+ include($route);
540
+ $loadingContent = ob_get_clean();
553
541
 
554
- if ($content !== false) {
542
+ if ($loadingContent !== false) {
555
543
  $url = $fileUrl === '/loading.php' ? '/' : str_replace('/loading.php', '', $fileUrl);
556
- $carry .= '<div pp-loading-url="' . $url . '">' . $content . '</div>';
544
+ $carry .= '<div pp-loading-url="' . $url . '">' . $loadingContent . '</div>';
557
545
  }
558
546
 
559
547
  return $carry;
@@ -566,36 +554,8 @@ function getLoadingsFiles()
566
554
  return '';
567
555
  }
568
556
 
569
- function getPrismaSettings(): \ArrayObject
570
- {
571
- $_prismaPHPSettingsFile = DOCUMENT_PATH . '/prisma-php.json';
572
-
573
- if (file_exists($_prismaPHPSettingsFile)) {
574
- $jsonContent = file_get_contents($_prismaPHPSettingsFile);
575
- $decodedJson = json_decode($jsonContent, true);
576
-
577
- if (json_last_error() === JSON_ERROR_NONE) {
578
- return new \ArrayObject($decodedJson, \ArrayObject::ARRAY_AS_PROPS);
579
- } else {
580
- return new \ArrayObject([]);
581
- }
582
- }
583
- }
584
-
585
557
  function modifyOutputLayoutForError($contentToAdd)
586
558
  {
587
- global
588
- $baseUrl,
589
- $content,
590
- $childContent,
591
- $uri,
592
- $pathname,
593
- $dynamicRouteParams,
594
- $params,
595
- $referer,
596
- $mainLayoutHead,
597
- $mainLayoutFooter;
598
-
599
559
  $errorFile = APP_PATH . '/error.php';
600
560
  $errorFileExists = file_exists($errorFile);
601
561
 
@@ -616,7 +576,7 @@ function modifyOutputLayoutForError($contentToAdd)
616
576
 
617
577
  ob_start();
618
578
  include_once $errorFile;
619
- $content = ob_get_clean();
579
+ MainLayout::$children = ob_get_clean();
620
580
  include $layoutFile;
621
581
  } else {
622
582
  echo $errorContent;
@@ -629,8 +589,6 @@ function modifyOutputLayoutForError($contentToAdd)
629
589
 
630
590
  function createUpdateRequestData()
631
591
  {
632
- global $_requestUriForFilesIncludes;
633
-
634
592
  $requestJsonData = SETTINGS_PATH . '/request-data.json';
635
593
 
636
594
  // Check if the JSON file exists
@@ -654,7 +612,7 @@ function createUpdateRequestData()
654
612
  }
655
613
 
656
614
  // Extract the current request URL
657
- $currentUrl = $_requestUriForFilesIncludes;
615
+ $currentUrl = Request::$uri;
658
616
 
659
617
  // If the URL already exists in the data, merge new included files with the existing ones
660
618
  if (isset($currentData[$currentUrl])) {
@@ -675,20 +633,17 @@ function createUpdateRequestData()
675
633
  file_put_contents($requestJsonData, $jsonData);
676
634
  }
677
635
 
678
- set_error_handler(function ($severity, $message, $file, $line) {
679
- if (!(error_reporting() & $severity)) {
680
- // This error code is not included in error_reporting
681
- return;
682
- }
683
-
684
- // Capture the specific severity types, including warnings (E_WARNING)
685
- $errorContent = "<div class='error'>Error: {$severity} - {$message} in {$file} on line {$line}</div>";
686
-
687
- // If needed, log it or output immediately based on severity
688
- if ($severity === E_WARNING || $severity === E_NOTICE) {
689
- modifyOutputLayoutForError($errorContent);
636
+ function authenticateUserToken()
637
+ {
638
+ $token = Request::getBearerToken();
639
+ if ($token) {
640
+ $auth = Auth::getInstance();
641
+ $verifyToken = $auth->verifyToken($token);
642
+ if ($verifyToken) {
643
+ $auth->signIn($verifyToken);
644
+ }
690
645
  }
691
- });
646
+ }
692
647
 
693
648
  set_exception_handler(function ($exception) {
694
649
  $errorContent = "<div class='error'>Exception: " . htmlspecialchars($exception->getMessage(), ENT_QUOTES, 'UTF-8') . "</div>";
@@ -706,63 +661,21 @@ register_shutdown_function(function () {
706
661
  }
707
662
  });
708
663
 
709
- $_prismaPHPSettings = getPrismaSettings();
710
- $_filesListRoutes = getFilesListRoutes();
711
-
712
- require_once SETTINGS_PATH . '/public-functions.php';
713
- require_once SETTINGS_PATH . '/request-methods.php';
714
- $_fileToInclude = '';
715
-
716
- function authenticateUserToken()
717
- {
718
- $token = getBearerToken();
719
- if ($token) {
720
- $auth = Auth::getInstance();
721
- $verifyToken = $auth->verifyToken($token);
722
- if ($verifyToken) {
723
- $auth->authenticate($verifyToken);
724
- }
725
- }
726
- }
727
-
728
- /**
729
- * @var string $pathname The pathname of the current request
730
- */
731
- $pathname = '';
732
- /**
733
- * @var array $dynamicRouteParams The dynamic route parameters
734
- */
735
- $dynamicRouteParams = [];
736
- /**
737
- * @var string $content The content to be included in the main layout file
738
- */
739
- $content = '';
740
- /**
741
- * @var string $childContent The child content to be included in the layout file
742
- */
743
- $childContent = '';
744
- /**
745
- * @var string $requestUrl - The request URL.
746
- */
747
- $requestUri = '';
748
-
749
664
  try {
750
665
  $_determineContentToInclude = determineContentToInclude();
751
666
  $_contentToInclude = $_determineContentToInclude['path'] ?? '';
752
667
  $_layoutsToInclude = $_determineContentToInclude['layouts'] ?? [];
753
- $pathname = $_determineContentToInclude['uri'] ? '/' . $_determineContentToInclude['uri'] : '/';
754
- $requestUri = $_determineContentToInclude['requestUri'] ? $_determineContentToInclude['requestUri'] : '/';
755
- $_requestUriForFilesIncludes = $requestUri;
756
- $_fileToInclude = null;
668
+ Request::$pathname = $_determineContentToInclude['pathname'] ? '/' . $_determineContentToInclude['pathname'] : '/';
669
+ Request::$uri = $_determineContentToInclude['uri'] ? $_determineContentToInclude['uri'] : '/';
757
670
  if (is_file($_contentToInclude)) {
758
- $_fileToInclude = basename($_contentToInclude); // returns the file name
671
+ Request::$fileToInclude = basename($_contentToInclude); // returns the file name
759
672
  }
760
673
 
761
674
  checkForDuplicateRoutes();
762
675
  authenticateUserToken();
763
676
 
764
677
  if (empty($_contentToInclude)) {
765
- if (!$isXFilRequest && $_prismaPHPSettings['backendOnly']) {
678
+ if (!Request::$isXFileRequest && PrismaPHPSettings::$option['backendOnly']) {
766
679
  // Set the header and output a JSON response for permission denied
767
680
  header('Content-Type: application/json');
768
681
  echo json_encode([
@@ -773,22 +686,22 @@ try {
773
686
  exit;
774
687
  }
775
688
 
776
- $filePath = APP_PATH . $pathname;
777
- if (is_file($filePath)) {
778
- if (file_exists($filePath) && $isXFilRequest) {
689
+ $_requestFilePath = APP_PATH . Request::$pathname;
690
+ if (is_file($_requestFilePath)) {
691
+ if (file_exists($_requestFilePath) && $isXFileRequest) {
779
692
  // Check if the file is a PHP file
780
- if (pathinfo($filePath, PATHINFO_EXTENSION) === 'php') {
693
+ if (pathinfo($_requestFilePath, PATHINFO_EXTENSION) === 'php') {
781
694
  // Include the PHP file without setting the JSON header
782
- include $filePath;
695
+ include $_requestFilePath;
783
696
  } else {
784
697
  // Set the appropriate content-type for non-PHP files if needed
785
698
  // and read the content
786
- header('Content-Type: ' . mime_content_type($filePath)); // Dynamic content type
787
- readfile($filePath);
699
+ header('Content-Type: ' . mime_content_type($_requestFilePath)); // Dynamic content type
700
+ readfile($_requestFilePath);
788
701
  }
789
702
  exit;
790
703
  }
791
- } else if ($_prismaPHPSettings['backendOnly']) {
704
+ } else if (PrismaPHPSettings::$option['backendOnly']) {
792
705
  // Set the header and output a JSON response for file not found
793
706
  header('Content-Type: application/json');
794
707
  echo json_encode([
@@ -800,7 +713,7 @@ try {
800
713
  }
801
714
  }
802
715
 
803
- if (!empty($_contentToInclude) && $_fileToInclude === 'route.php') {
716
+ if (!empty($_contentToInclude) && Request::$fileToInclude === 'route.php') {
804
717
  header('Content-Type: application/json');
805
718
  require_once $_contentToInclude;
806
719
  exit;
@@ -811,74 +724,75 @@ try {
811
724
 
812
725
  $_isContentIncluded = false;
813
726
  $_isChildContentIncluded = false;
814
- $_isContentVariableIncluded = containsContent($_parentLayoutPath);
727
+ $_isContentVariableIncluded = containsChildren($_parentLayoutPath);
815
728
  if (!$_isContentVariableIncluded) {
816
729
  $_isContentIncluded = true;
817
730
  }
818
731
 
819
- if (!empty($_contentToInclude) && !empty($_fileToInclude)) {
732
+ if (!empty($_contentToInclude) && !empty(Request::$fileToInclude)) {
820
733
  if (!$_isParentLayout) {
821
734
  ob_start();
822
735
  require_once $_contentToInclude;
823
- $childContent = ob_get_clean();
736
+ MainLayout::$childLayoutChildren = ob_get_clean();
824
737
  }
825
738
  foreach (array_reverse($_layoutsToInclude) as $layoutPath) {
826
739
  if ($_parentLayoutPath === $layoutPath) {
827
740
  continue;
828
741
  }
829
742
 
830
- $_isChildContentVariableIncluded = containsChildContent($layoutPath);
743
+ $_isChildContentVariableIncluded = containsChildLayoutChildren($layoutPath);
831
744
  if (!$_isChildContentVariableIncluded) {
832
745
  $_isChildContentIncluded = true;
833
746
  }
834
747
 
835
748
  ob_start();
836
749
  require_once $layoutPath;
837
- $childContent = ob_get_clean();
750
+ MainLayout::$childLayoutChildren = ob_get_clean();
838
751
  }
839
752
  } else {
840
753
  ob_start();
841
754
  require_once APP_PATH . '/not-found.php';
842
- $childContent = ob_get_clean();
755
+ MainLayout::$childLayoutChildren = ob_get_clean();
843
756
  }
844
757
 
845
758
  if ($_isParentLayout && !empty($_contentToInclude)) {
846
759
  ob_start();
847
760
  require_once $_contentToInclude;
848
- $childContent = ob_get_clean();
761
+ MainLayout::$childLayoutChildren = ob_get_clean();
849
762
  }
850
763
 
851
764
  if (!$_isContentIncluded && !$_isChildContentIncluded) {
852
- $secondRequestC69CD = $_data53C84['secondRequestC69CD'] ?? false;
765
+ $_secondRequestC69CD = Request::$data['secondRequestC69CD'] ?? false;
853
766
 
854
- if (!$secondRequestC69CD) {
767
+ if (!$_secondRequestC69CD) {
855
768
  createUpdateRequestData();
856
769
  }
857
770
 
858
- if ($isWire && !$secondRequestC69CD) {
771
+ if (Request::$isWire && !$_secondRequestC69CD) {
859
772
  $_requestFilesJson = SETTINGS_PATH . '/request-data.json';
860
773
  $_requestFilesData = file_exists($_requestFilesJson) ? json_decode(file_get_contents($_requestFilesJson), true) : [];
861
774
 
862
- if ($_requestFilesData[$_requestUriForFilesIncludes]) {
863
- $_requestDataToLoop = $_requestFilesData[$_requestUriForFilesIncludes];
775
+ if ($_requestFilesData[Request::$uri]) {
776
+ $_requestDataToLoop = $_requestFilesData[Request::$uri];
864
777
 
865
778
  foreach ($_requestDataToLoop['includedFiles'] as $file) {
866
779
  if (file_exists($file)) {
867
780
  ob_start();
868
781
  require_once $file;
869
- $childContent .= ob_get_clean();
782
+ MainLayout::$childLayoutChildren .= ob_get_clean();
870
783
  }
871
784
  }
872
785
  }
873
786
  }
874
787
 
875
- $content .= $childContent;
876
- $content .= getLoadingsFiles();
788
+ MainLayout::$children = MainLayout::$childLayoutChildren;
789
+ MainLayout::$children .= getLoadingsFiles();
790
+ MainLayout::$children = '<div id="pphp-7CA7BB68A3656A88">' . MainLayout::$children . '</div>';
877
791
 
878
792
  ob_start();
879
793
  require_once APP_PATH . '/layout.php';
880
794
 
881
- if ($isWire && !$secondRequestC69CD) {
795
+ if (Request::$isWire && !$_secondRequestC69CD) {
882
796
  ob_end_clean();
883
797
  wireCallback();
884
798
  } else {
@@ -886,16 +800,39 @@ try {
886
800
  }
887
801
  } else {
888
802
  if ($_isContentIncluded) {
889
- echo "<div class='error'>The parent layout file does not contain &lt;?php echo \$content; ?&gt; Or &lt;?= \$content ?&gt;<br>" . "<strong>$_parentLayoutPath</strong></div>";
803
+ echo "<div class='error'>The parent layout file does not contain &lt;?php echo MainLayout::\$children; ?&gt; Or &lt;?= MainLayout::\$children ?&gt;<br>" . "<strong>$_parentLayoutPath</strong></div>";
890
804
  } else {
891
- $errorDetails = "<div class='error'>The layout file does not contain &lt;?php echo \$childContent; ?&gt; or &lt;?= \$childContent ?&gt;<br><strong>$layoutPath</strong></div>";
892
- modifyOutputLayoutForError($errorDetails);
805
+ $_errorDetails = "<div class='error'>The layout file does not contain &lt;?php echo MainLayout::\$childLayoutChildren; ?&gt; or &lt;?= MainLayout::\$childLayoutChildren ?&gt;<br><strong>$layoutPath</strong></div>";
806
+ modifyOutputLayoutForError($_errorDetails);
893
807
  }
894
808
  }
895
809
  } catch (Throwable $e) {
896
- $errorDetails = "Unhandled Exception: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
897
- $errorDetails .= "<br>File: " . htmlspecialchars($e->getFile(), ENT_QUOTES, 'UTF-8');
898
- $errorDetails .= "<br>Line: " . htmlspecialchars($e->getLine(), ENT_QUOTES, 'UTF-8');
899
- $errorDetails = "<div class='error'>$errorDetails</div>";
900
- modifyOutputLayoutForError($errorDetails);
810
+ $_errorDetails = "Unhandled Exception: " . htmlspecialchars($e->getMessage(), ENT_QUOTES, 'UTF-8');
811
+ $_errorDetails .= "<br>File: " . htmlspecialchars($e->getFile(), ENT_QUOTES, 'UTF-8');
812
+ $_errorDetails .= "<br>Line: " . htmlspecialchars($e->getLine(), ENT_QUOTES, 'UTF-8');
813
+ $_errorDetails = "<div class='error'>$_errorDetails</div>";
814
+ modifyOutputLayoutForError($_errorDetails);
901
815
  }
816
+
817
+ (function () {
818
+ $lastErrorCapture = error_get_last();
819
+ if ($lastErrorCapture !== null) {
820
+ $errorContent = "<div class='error'>Error: " . $lastErrorCapture['message'] . " in " . $lastErrorCapture['file'] . " on line " . $lastErrorCapture['line'] . "</div>";
821
+ modifyOutputLayoutForError($errorContent);
822
+ }
823
+ })();
824
+
825
+ set_error_handler(function ($severity, $message, $file, $line) {
826
+ if (!(error_reporting() & $severity)) {
827
+ // This error code is not included in error_reporting
828
+ return;
829
+ }
830
+
831
+ // Capture the specific severity types, including warnings (E_WARNING)
832
+ $errorContent = "<div class='error'>Error: {$severity} - {$message} in {$file} on line {$line}</div>";
833
+
834
+ // If needed, log it or output immediately based on severity
835
+ if ($severity === E_WARNING || $severity === E_NOTICE) {
836
+ modifyOutputLayoutForError($errorContent);
837
+ }
838
+ });