17
17
use CodeIgniter \Events \Events ;
18
18
use CodeIgniter \Exceptions \FrameworkException ;
19
19
use CodeIgniter \Exceptions \PageNotFoundException ;
20
+ use CodeIgniter \Filters \Filters ;
20
21
use CodeIgniter \HTTP \CLIRequest ;
21
22
use CodeIgniter \HTTP \DownloadResponse ;
22
23
use CodeIgniter \HTTP \Exceptions \RedirectException ;
@@ -339,32 +340,83 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
339
340
$ this ->getRequestObject ();
340
341
$ this ->getResponseObject ();
341
342
342
- try {
343
- $ this ->forceSecureAccess ();
343
+ Events::trigger ('pre_system ' );
344
344
345
- $ this ->response = $ this ->handleRequest ($ routes , config (Cache::class), $ returnResponse );
346
- } catch (ResponsableInterface |DeprecatedRedirectException $ e ) {
347
- $ this ->outputBufferingEnd ();
348
- if ($ e instanceof DeprecatedRedirectException) {
349
- $ e = new RedirectException ($ e ->getMessage (), $ e ->getCode (), $ e );
350
- }
345
+ $ this ->benchmark ->stop ('bootstrap ' );
346
+
347
+ $ this ->benchmark ->start ('required_before_filters ' );
348
+ // Start up the filters
349
+ $ filters = Services::filters ();
350
+ // Run required before filters
351
+ $ possibleResponse = $ this ->runRequiredBeforeFilters ($ filters );
351
352
352
- $ this ->response = $ e ->getResponse ();
353
- } catch (PageNotFoundException $ e ) {
354
- $ this ->response = $ this ->display404errors ($ e );
355
- } catch (Throwable $ e ) {
356
- $ this ->outputBufferingEnd ();
353
+ // If a ResponseInterface instance is returned then send it back to the client and stop
354
+ if ($ possibleResponse instanceof ResponseInterface) {
355
+ $ this ->response = $ possibleResponse ;
356
+ } else {
357
+ try {
358
+ $ this ->response = $ this ->handleRequest ($ routes , config (Cache::class), $ returnResponse );
359
+ } catch (ResponsableInterface |DeprecatedRedirectException $ e ) {
360
+ $ this ->outputBufferingEnd ();
361
+ if ($ e instanceof DeprecatedRedirectException) {
362
+ $ e = new RedirectException ($ e ->getMessage (), $ e ->getCode (), $ e );
363
+ }
357
364
358
- throw $ e ;
365
+ $ this ->response = $ e ->getResponse ();
366
+ } catch (PageNotFoundException $ e ) {
367
+ $ this ->response = $ this ->display404errors ($ e );
368
+ } catch (Throwable $ e ) {
369
+ $ this ->outputBufferingEnd ();
370
+
371
+ throw $ e ;
372
+ }
359
373
}
360
374
375
+ $ this ->runRequiredAfterFilters ($ filters );
376
+
377
+ // Is there a post-system event?
378
+ Events::trigger ('post_system ' );
379
+
361
380
if ($ returnResponse ) {
362
381
return $ this ->response ;
363
382
}
364
383
365
384
$ this ->sendResponse ();
366
385
}
367
386
387
+ /**
388
+ * Run required before filters.
389
+ */
390
+ private function runRequiredBeforeFilters (Filters $ filters ): ?ResponseInterface
391
+ {
392
+ $ possibleResponse = $ filters ->runRequired ('before ' );
393
+ $ this ->benchmark ->stop ('required_before_filters ' );
394
+
395
+ // If a ResponseInterface instance is returned then send it back to the client and stop
396
+ if ($ possibleResponse instanceof ResponseInterface) {
397
+ return $ possibleResponse ;
398
+ }
399
+
400
+ return null ;
401
+ }
402
+
403
+ /**
404
+ * Run required after filters.
405
+ */
406
+ private function runRequiredAfterFilters (Filters $ filters ): void
407
+ {
408
+ $ filters ->setResponse ($ this ->response );
409
+
410
+ // Run required after filters
411
+ $ this ->benchmark ->start ('required_after_filters ' );
412
+ $ response = $ filters ->runRequired ('after ' );
413
+ $ this ->benchmark ->stop ('required_after_filters ' );
414
+
415
+ if ($ response instanceof ResponseInterface) {
416
+ $ this ->response = $ response ;
417
+ }
418
+ }
419
+
368
420
/**
369
421
* Invoked via php-cli command?
370
422
*/
@@ -405,20 +457,11 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
405
457
return $ this ->response ->setStatusCode (405 )->setBody ('Method Not Allowed ' );
406
458
}
407
459
408
- Events::trigger ('pre_system ' );
409
-
410
- // Check for a cached page. Execution will stop
411
- // if the page has been cached.
412
- if (($ response = $ this ->displayCache ($ cacheConfig )) instanceof ResponseInterface) {
413
- return $ response ;
414
- }
415
-
416
460
$ routeFilters = $ this ->tryToRouteIt ($ routes );
417
461
418
462
$ uri = $ this ->request ->getPath ();
419
463
420
464
if ($ this ->enableFilters ) {
421
- // Start up the filters
422
465
$ filters = Services::filters ();
423
466
424
467
// If any filters were specified within the routes file,
@@ -478,9 +521,6 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
478
521
$ filters = Services::filters ();
479
522
$ filters ->setResponse ($ this ->response );
480
523
481
- // After filter debug toolbar requires 'total_execution'.
482
- $ this ->totalTime = $ this ->benchmark ->getElapsedTime ('total_execution ' );
483
-
484
524
// Run "after" filters
485
525
$ this ->benchmark ->start ('after_filters ' );
486
526
$ response = $ filters ->run ($ uri , 'after ' );
@@ -496,21 +536,13 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
496
536
! $ this ->response instanceof DownloadResponse
497
537
&& ! $ this ->response instanceof RedirectResponse
498
538
) {
499
- // Cache it without the performance metrics replaced
500
- // so that we can have live speed updates along the way.
501
- // Must be run after filters to preserve the Response headers.
502
- $ this ->pageCache ->make ($ this ->request , $ this ->response );
503
-
504
539
// Save our current URI as the previous URI in the session
505
540
// for safer, more accurate use with `previous_url()` helper function.
506
541
$ this ->storePreviousURL (current_url (true ));
507
542
}
508
543
509
544
unset($ uri );
510
545
511
- // Is there a post-system event?
512
- Events::trigger ('post_system ' );
513
-
514
546
return $ this ->response ;
515
547
}
516
548
@@ -651,6 +683,8 @@ protected function getResponseObject()
651
683
* should be enforced for this URL.
652
684
*
653
685
* @return void
686
+ *
687
+ * @deprecated 4.5.0 No longer used. Moved to ForceHTTPS filter.
654
688
*/
655
689
protected function forceSecureAccess ($ duration = 31_536_000 )
656
690
{
@@ -668,6 +702,7 @@ protected function forceSecureAccess($duration = 31_536_000)
668
702
*
669
703
* @throws Exception
670
704
*
705
+ * @deprecated 4.5.0 PageCache required filter is used. No longer used.
671
706
* @deprecated 4.4.2 The parameter $config is deprecated. No longer used.
672
707
*/
673
708
public function displayCache (Cache $ config )
@@ -722,6 +757,9 @@ public function cachePage(Cache $config)
722
757
*/
723
758
public function getPerformanceStats (): array
724
759
{
760
+ // After filter debug toolbar requires 'total_execution'.
761
+ $ this ->totalTime = $ this ->benchmark ->getElapsedTime ('total_execution ' );
762
+
725
763
return [
726
764
'startTime ' => $ this ->startTime ,
727
765
'totalTime ' => $ this ->totalTime ,
@@ -750,6 +788,8 @@ protected function generateCacheName(Cache $config): string
750
788
751
789
/**
752
790
* Replaces the elapsed_time and memory_usage tag.
791
+ *
792
+ * @deprecated 4.5.0 PerformanceMetrics required filter is used. No longer used.
753
793
*/
754
794
public function displayPerformanceMetrics (string $ output ): string
755
795
{
@@ -774,6 +814,8 @@ public function displayPerformanceMetrics(string $output): string
774
814
*/
775
815
protected function tryToRouteIt (?RouteCollectionInterface $ routes = null )
776
816
{
817
+ $ this ->benchmark ->start ('routing ' );
818
+
777
819
if ($ routes === null ) {
778
820
$ routes = Services::routes ()->loadRoutes ();
779
821
}
@@ -783,9 +825,6 @@ protected function tryToRouteIt(?RouteCollectionInterface $routes = null)
783
825
784
826
$ uri = $ this ->request ->getPath ();
785
827
786
- $ this ->benchmark ->stop ('bootstrap ' );
787
- $ this ->benchmark ->start ('routing ' );
788
-
789
828
$ this ->outputBufferingStart ();
790
829
791
830
$ this ->controller = $ this ->router ->handle ($ uri );
@@ -1052,13 +1091,6 @@ public function spoofRequestMethod()
1052
1091
*/
1053
1092
protected function sendResponse ()
1054
1093
{
1055
- // Update the performance metrics
1056
- $ body = $ this ->response ->getBody ();
1057
- if ($ body !== null ) {
1058
- $ output = $ this ->displayPerformanceMetrics ($ body );
1059
- $ this ->response ->setBody ($ output );
1060
- }
1061
-
1062
1094
$ this ->response ->send ();
1063
1095
}
1064
1096
0 commit comments