1313
1414namespace CodeIgniter ;
1515
16+ use CodeIgniter \Cache \FactoriesCache ;
17+ use CodeIgniter \CLI \Console ;
1618use CodeIgniter \Config \DotEnv ;
1719use Config \Autoload ;
1820use Config \Modules ;
21+ use Config \Optimize ;
1922use Config \Paths ;
2023use Config \Services ;
2124
@@ -32,21 +35,52 @@ class Boot
3235 * Context
3336 * web: Invoked by HTTP request
3437 * php-cli: Invoked by CLI via `php public/index.php`
38+ *
39+ * @return int Exit code.
3540 */
36- public static function bootWeb (Paths $ paths ): void
41+ public static function bootWeb (Paths $ paths ): int
3742 {
38- static ::boot ($ paths );
43+ static ::definePathConstants ($ paths );
44+ if (! defined ('APP_NAMESPACE ' )) {
45+ static ::loadConstants ();
46+ }
47+ static ::checkMissingExtensions ();
48+
49+ static ::loadDotEnv ($ paths );
50+ static ::defineEnvironment ();
51+ static ::loadEnvironmentBootstrap ($ paths );
52+
53+ static ::loadCommonFunctions ();
54+ static ::loadAutoloader ();
55+ static ::setExceptionHandler ();
56+ static ::initializeKint ();
57+
58+ $ configCacheEnabled = class_exists (Optimize::class)
59+ && (new Optimize ())->configCacheEnabled ;
60+ if ($ configCacheEnabled ) {
61+ $ factoriesCache = static ::loadConfigCache ();
62+ }
63+
64+ static ::autoloadHelpers ();
65+
66+ $ app = static ::initializeCodeIgniter ();
67+ static ::runCodeIgniter ($ app );
68+
69+ if ($ configCacheEnabled ) {
70+ static ::saveConfigCache ($ factoriesCache );
71+ }
72+
73+ // Exits the application, setting the exit code for CLI-based
74+ // applications that might be watching.
75+ return EXIT_SUCCESS ;
3976 }
4077
4178 /**
4279 * Used by `spark`
80+ *
81+ * @return int Exit code.
4382 */
44- public static function bootSpark (Paths $ paths ): void
45- {
46- static ::boot ($ paths );
47- }
48-
49- protected static function boot (Paths $ paths ): void
83+ public static function bootSpark (Paths $ paths ): int
5084 {
5185 static ::definePathConstants ($ paths );
5286 if (! defined ('APP_NAMESPACE ' )) {
@@ -62,6 +96,12 @@ protected static function boot(Paths $paths): void
6296 static ::loadAutoloader ();
6397 static ::setExceptionHandler ();
6498 static ::initializeKint ();
99+ static ::autoloadHelpers ();
100+
101+ static ::initializeCodeIgniter ();
102+ $ console = static ::initializeConsole ();
103+
104+ return static ::runCommand ($ console );
65105 }
66106
67107 /**
@@ -79,6 +119,7 @@ public static function bootTest(Paths $paths): void
79119 static ::loadAutoloader ();
80120 static ::setExceptionHandler ();
81121 static ::initializeKint ();
122+ static ::autoloadHelpers ();
82123 }
83124
84125 /**
@@ -188,6 +229,10 @@ protected static function loadAutoloader(): void
188229
189230 // Initialize and register the loader with the SPL autoloader stack.
190231 Services::autoloader ()->initialize (new Autoload (), new Modules ())->register ();
232+ }
233+
234+ protected static function autoloadHelpers (): void
235+ {
191236 Services::autoloader ()->loadHelpers ();
192237 }
193238
@@ -234,4 +279,64 @@ protected static function initializeKint(): void
234279 {
235280 Services::autoloader ()->initializeKint (CI_DEBUG );
236281 }
282+
283+ protected static function loadConfigCache (): FactoriesCache
284+ {
285+ $ factoriesCache = new FactoriesCache ();
286+ $ factoriesCache ->load ('config ' );
287+
288+ return $ factoriesCache ;
289+ }
290+
291+ /**
292+ * The CodeIgniter class contains the core functionality to make
293+ * the application run, and does all the dirty work to get
294+ * the pieces all working together.
295+ */
296+ protected static function initializeCodeIgniter (): CodeIgniter
297+ {
298+ $ app = Config \Services::codeigniter ();
299+ $ app ->initialize ();
300+ $ context = is_cli () ? 'php-cli ' : 'web ' ;
301+ $ app ->setContext ($ context );
302+
303+ return $ app ;
304+ }
305+
306+ /**
307+ * Now that everything is set up, it's time to actually fire
308+ * up the engines and make this app do its thang.
309+ */
310+ protected static function runCodeIgniter (CodeIgniter $ app ): void
311+ {
312+ $ app ->run ();
313+ }
314+
315+ protected static function saveConfigCache (FactoriesCache $ factoriesCache ): void
316+ {
317+ $ factoriesCache ->save ('config ' );
318+ }
319+
320+ protected static function initializeConsole (): Console
321+ {
322+ $ console = new Console ();
323+
324+ // Show basic information before we do anything else.
325+ // @phpstan-ignore-next-line
326+ if (is_int ($ suppress = array_search ('--no-header ' , $ _SERVER ['argv ' ], true ))) {
327+ unset($ _SERVER ['argv ' ][$ suppress ]); // @phpstan-ignore-line
328+ $ suppress = true ;
329+ }
330+
331+ $ console ->showHeader ($ suppress );
332+
333+ return $ console ;
334+ }
335+
336+ protected static function runCommand (Console $ console ): int
337+ {
338+ $ exit = $ console ->run ();
339+
340+ return is_int ($ exit ) ? $ exit : EXIT_SUCCESS ;
341+ }
237342}
0 commit comments