@@ -141,6 +141,16 @@ class CodeIgniter
141141 */
142142 protected $ useSafeOutput = false ;
143143
144+ /**
145+ * Context
146+ * web: Invoked by HTTP request
147+ * php-cli: Invoked by CLI via `php public/index.php`
148+ * spark: Invoked by CLI via the `spark` command
149+ *
150+ * @phpstan-var 'php-cli'|'spark'|'web'
151+ */
152+ protected string $ context ;
153+
144154 /**
145155 * Constructor.
146156 */
@@ -294,6 +304,11 @@ protected function initializeKint()
294304 */
295305 public function run (?RouteCollectionInterface $ routes = null , bool $ returnResponse = false )
296306 {
307+ assert (
308+ $ this ->context !== null ,
309+ 'Context must be set before run() is called. If you are upgrading from 4.1.x, you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`. '
310+ );
311+
297312 $ this ->startBenchmark ();
298313
299314 $ this ->getRequestObject ();
@@ -321,7 +336,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
321336 }
322337
323338 // spark command has nothing to do with HTTP redirect and 404
324- if (defined ( ' SPARKED ' )) {
339+ if ($ this -> isSparked ( )) {
325340 return $ this ->handleRequest ($ routes , $ cacheConfig , $ returnResponse );
326341 }
327342
@@ -358,6 +373,30 @@ public function useSafeOutput(bool $safe = true)
358373 return $ this ;
359374 }
360375
376+ /**
377+ * Invoked via spark command?
378+ */
379+ private function isSparked (): bool
380+ {
381+ return $ this ->context === 'spark ' ;
382+ }
383+
384+ /**
385+ * Invoked via php-cli command?
386+ */
387+ private function isPhpCli (): bool
388+ {
389+ return $ this ->context === 'php-cli ' ;
390+ }
391+
392+ /**
393+ * Web access?
394+ */
395+ private function isWeb (): bool
396+ {
397+ return $ this ->context === 'web ' ;
398+ }
399+
361400 /**
362401 * Handles the main request logic and fires the controller.
363402 *
@@ -389,7 +428,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
389428 }
390429
391430 // Never run filters when running through Spark cli
392- if (! defined ( ' SPARKED ' )) {
431+ if (! $ this -> isSparked ( )) {
393432 // Run "before" filters
394433 $ this ->benchmark ->start ('before_filters ' );
395434 $ possibleResponse = $ filters ->run ($ uri , 'before ' );
@@ -430,7 +469,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
430469 $ this ->gatherOutput ($ cacheConfig , $ returned );
431470
432471 // Never run filters when running through Spark cli
433- if (! defined ( ' SPARKED ' )) {
472+ if (! $ this -> isSparked ( )) {
434473 $ filters ->setResponse ($ this ->response );
435474
436475 // Run "after" filters
@@ -548,10 +587,8 @@ protected function getRequestObject()
548587 return ;
549588 }
550589
551- if (is_cli () && ENVIRONMENT !== 'testing ' ) {
552- // @codeCoverageIgnoreStart
590+ if ($ this ->isSparked () || $ this ->isPhpCli ()) {
553591 $ this ->request = Services::clirequest ($ this ->config );
554- // @codeCoverageIgnoreEnd
555592 } else {
556593 $ this ->request = Services::request ($ this ->config );
557594 // guess at protocol if needed
@@ -567,7 +604,7 @@ protected function getResponseObject()
567604 {
568605 $ this ->response = Services::response ($ this ->config );
569606
570- if (! is_cli () || ENVIRONMENT === ' testing ' ) {
607+ if ($ this -> isWeb () ) {
571608 $ this ->response ->setProtocolVersion ($ this ->request ->getProtocolVersion ());
572609 }
573610
@@ -828,7 +865,7 @@ protected function createController()
828865 protected function runController ($ class )
829866 {
830867 // If this is a console request then use the input segments as parameters
831- $ params = defined ( ' SPARKED ' ) ? $ this ->request ->getSegments () : $ this ->router ->params ();
868+ $ params = $ this -> isSparked ( ) ? $ this ->request ->getSegments () : $ this ->router ->params ();
832869
833870 if (method_exists ($ class , '_remap ' )) {
834871 $ output = $ class ->_remap ($ this ->method , ...$ params );
@@ -888,7 +925,9 @@ protected function display404errors(PageNotFoundException $e)
888925 ob_end_flush (); // @codeCoverageIgnore
889926 }
890927
891- throw PageNotFoundException::forPageNotFound (ENVIRONMENT !== 'production ' || is_cli () ? $ e ->getMessage () : '' );
928+ throw PageNotFoundException::forPageNotFound (
929+ (ENVIRONMENT !== 'production ' || ! $ this ->isWeb ()) ? $ e ->getMessage () : ''
930+ );
892931 }
893932
894933 /**
@@ -954,8 +993,8 @@ protected function gatherOutput(?Cache $cacheConfig = null, $returned = null)
954993 public function storePreviousURL ($ uri )
955994 {
956995 // Ignore CLI requests
957- if (is_cli () && ENVIRONMENT !== ' testing ' ) {
958- return ; // @codeCoverageIgnore
996+ if (! $ this -> isWeb () ) {
997+ return ;
959998 }
960999 // Ignore AJAX requests
9611000 if (method_exists ($ this ->request , 'isAJAX ' ) && $ this ->request ->isAJAX ()) {
@@ -1019,4 +1058,18 @@ protected function callExit($code)
10191058 {
10201059 exit ($ code ); // @codeCoverageIgnore
10211060 }
1061+
1062+ /**
1063+ * Sets the app context.
1064+ *
1065+ * @phpstan-param 'php-cli'|'spark'|'web' $context
1066+ *
1067+ * @return $this
1068+ */
1069+ public function setContext (string $ context )
1070+ {
1071+ $ this ->context = $ context ;
1072+
1073+ return $ this ;
1074+ }
10221075}
0 commit comments