diff --git a/app/Config/Cache.php b/app/Config/Cache.php index b29c13a9ea71..3fbade6840cc 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -46,25 +46,6 @@ class Cache extends BaseConfig */ public string $storePath = WRITEPATH . 'cache/'; - /** - * -------------------------------------------------------------------------- - * Cache Include Query String - * -------------------------------------------------------------------------- - * - * Whether to take the URL query string into consideration when generating - * output cache files. Valid options are: - * - * false = Disabled - * true = Enabled, take all query parameters into account. - * Please be aware that this may result in numerous cache - * files generated for the same page over and over again. - * ['q'] = Enabled, but only take into account the specified list - * of query parameters. - * - * @var bool|list - */ - public $cacheQueryString = false; - /** * -------------------------------------------------------------------------- * Key Prefix @@ -168,4 +149,23 @@ class Cache extends BaseConfig 'redis' => RedisHandler::class, 'wincache' => WincacheHandler::class, ]; + + /** + * -------------------------------------------------------------------------- + * Web Page Caching: Cache Include Query String + * -------------------------------------------------------------------------- + * + * Whether to take the URL query string into consideration when generating + * output cache files. Valid options are: + * + * false = Disabled + * true = Enabled, take all query parameters into account. + * Please be aware that this may result in numerous cache + * files generated for the same page over and over again. + * ['q'] = Enabled, but only take into account the specified list + * of query parameters. + * + * @var bool|list + */ + public $cacheQueryString = false; } diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php new file mode 100644 index 000000000000..6fb441fd2288 --- /dev/null +++ b/app/Config/Optimize.php @@ -0,0 +1,32 @@ +systemDirectory . '/Boot.php'; -CodeIgniter\Boot::BootWeb($paths); -// Load Config Cache -// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); -// $factoriesCache->load('config'); -// ^^^ Uncomment these lines if you want to use Config Caching. - -/* - * --------------------------------------------------------------- - * GRAB OUR CODEIGNITER INSTANCE - * --------------------------------------------------------------- - * - * The CodeIgniter class contains the core functionality to make - * the application run, and does all the dirty work to get - * the pieces all working together. - */ - -$app = Config\Services::codeigniter(); -$app->initialize(); -$context = is_cli() ? 'php-cli' : 'web'; -$app->setContext($context); - -/* - *--------------------------------------------------------------- - * LAUNCH THE APPLICATION - *--------------------------------------------------------------- - * Now that everything is set up, it's time to actually fire - * up the engines and make this app do its thang. - */ - -$app->run(); - -// Save Config Cache -// $factoriesCache->save('config'); -// ^^^ Uncomment this line if you want to use Config Caching. - -// Exits the application, setting the exit code for CLI-based applications -// that might be watching. -exit(EXIT_SUCCESS); +exit(CodeIgniter\Boot::bootWeb($paths)); diff --git a/spark b/spark index a1b42e35b0b7..a56fbc1bd7b6 100755 --- a/spark +++ b/spark @@ -80,41 +80,5 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -CodeIgniter\Boot::BootSpark($paths); -/* - * --------------------------------------------------------------- - * GRAB OUR CODEIGNITER INSTANCE - * --------------------------------------------------------------- - */ - -$app = Config\Services::codeigniter(); -$app->initialize(); - -/* - * --------------------------------------------------------------- - * GRAB OUR CONSOLE - * --------------------------------------------------------------- - */ - -$console = new CodeIgniter\CLI\Console(); - -// SHOW HEADER -// Show basic information before we do anything else. -if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { - unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore - $suppress = true; -} - -$console->showHeader($suppress); - -/* - *--------------------------------------------------------------- - * EXECUTE THE COMMAND - *--------------------------------------------------------------- - */ - -// fire off the command in the main framework. -$exit = $console->run(); - -exit(is_int($exit) ? $exit : EXIT_SUCCESS); +exit(CodeIgniter\Boot::bootSpark($paths)); diff --git a/system/Boot.php b/system/Boot.php index ba94cce6b910..8a769bbb6866 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -13,9 +13,12 @@ namespace CodeIgniter; +use CodeIgniter\Cache\FactoriesCache; +use CodeIgniter\CLI\Console; use CodeIgniter\Config\DotEnv; use Config\Autoload; use Config\Modules; +use Config\Optimize; use Config\Paths; use Config\Services; @@ -32,21 +35,52 @@ class Boot * Context * web: Invoked by HTTP request * php-cli: Invoked by CLI via `php public/index.php` + * + * @return int Exit code. */ - public static function bootWeb(Paths $paths): void + public static function bootWeb(Paths $paths): int { - static::boot($paths); + static::definePathConstants($paths); + if (! defined('APP_NAMESPACE')) { + static::loadConstants(); + } + static::checkMissingExtensions(); + + static::loadDotEnv($paths); + static::defineEnvironment(); + static::loadEnvironmentBootstrap($paths); + + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::initializeKint(); + + $configCacheEnabled = class_exists(Optimize::class) + && (new Optimize())->configCacheEnabled; + if ($configCacheEnabled) { + $factoriesCache = static::loadConfigCache(); + } + + static::autoloadHelpers(); + + $app = static::initializeCodeIgniter(); + static::runCodeIgniter($app); + + if ($configCacheEnabled) { + static::saveConfigCache($factoriesCache); + } + + // Exits the application, setting the exit code for CLI-based + // applications that might be watching. + return EXIT_SUCCESS; } /** * Used by `spark` + * + * @return int Exit code. */ - public static function bootSpark(Paths $paths): void - { - static::boot($paths); - } - - protected static function boot(Paths $paths): void + public static function bootSpark(Paths $paths): int { static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { @@ -62,6 +96,12 @@ protected static function boot(Paths $paths): void static::loadAutoloader(); static::setExceptionHandler(); static::initializeKint(); + static::autoloadHelpers(); + + static::initializeCodeIgniter(); + $console = static::initializeConsole(); + + return static::runCommand($console); } /** @@ -79,6 +119,7 @@ public static function bootTest(Paths $paths): void static::loadAutoloader(); static::setExceptionHandler(); static::initializeKint(); + static::autoloadHelpers(); } /** @@ -188,6 +229,10 @@ protected static function loadAutoloader(): void // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); + } + + protected static function autoloadHelpers(): void + { Services::autoloader()->loadHelpers(); } @@ -234,4 +279,64 @@ protected static function initializeKint(): void { Services::autoloader()->initializeKint(CI_DEBUG); } + + protected static function loadConfigCache(): FactoriesCache + { + $factoriesCache = new FactoriesCache(); + $factoriesCache->load('config'); + + return $factoriesCache; + } + + /** + * The CodeIgniter class contains the core functionality to make + * the application run, and does all the dirty work to get + * the pieces all working together. + */ + protected static function initializeCodeIgniter(): CodeIgniter + { + $app = Config\Services::codeigniter(); + $app->initialize(); + $context = is_cli() ? 'php-cli' : 'web'; + $app->setContext($context); + + return $app; + } + + /** + * Now that everything is set up, it's time to actually fire + * up the engines and make this app do its thang. + */ + protected static function runCodeIgniter(CodeIgniter $app): void + { + $app->run(); + } + + protected static function saveConfigCache(FactoriesCache $factoriesCache): void + { + $factoriesCache->save('config'); + } + + protected static function initializeConsole(): Console + { + $console = new Console(); + + // Show basic information before we do anything else. + // @phpstan-ignore-next-line + if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) { + unset($_SERVER['argv'][$suppress]); // @phpstan-ignore-line + $suppress = true; + } + + $console->showHeader($suppress); + + return $console; + } + + protected static function runCommand(Console $console): int + { + $exit = $console->run(); + + return is_int($exit) ? $exit : EXIT_SUCCESS; + } } diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index e5f3989148b2..2f8df2a35420 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -15,6 +15,7 @@ use CodeIgniter\Autoloader\Autoloader; use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\Autoloader\FileLocatorInterface; use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Cache\ResponseCache; @@ -71,6 +72,7 @@ use Config\Images; use Config\Migrations; use Config\Modules; +use Config\Optimize; use Config\Pager as ConfigPager; use Config\Services as AppServices; use Config\Toolbar as ConfigToolbar; @@ -281,7 +283,14 @@ public static function locator(bool $getShared = true) { if ($getShared) { if (empty(static::$instances['locator'])) { - static::$instances['locator'] = new FileLocator(static::autoloader()); + $cacheEnabled = class_exists(Optimize::class) + && (new Optimize())->locatorCacheEnabled; + + if ($cacheEnabled) { + static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader())); + } else { + static::$instances['locator'] = new FileLocator(static::autoloader()); + } } return static::$mocks['locator'] ?? static::$instances['locator']; diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 7328dd00c0aa..26ce27a673be 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -190,6 +190,10 @@ Or simply delete the **writable/cache/FileLocatorCache** file. How to Enable FileLocator Caching ================================= -Add the following code in **app/Config/Services.php**: +Set the following property to ``true`` in **app/Config/Optimize.php**:: -.. literalinclude:: autoloader/004.php + public bool $locatorCacheEnabled = true; + +.. note:: + This property cannot be overridden by + :ref:`environment variables `. diff --git a/user_guide_src/source/concepts/autoloader/004.php b/user_guide_src/source/concepts/autoloader/004.php deleted file mode 100644 index 090149e6486c..000000000000 --- a/user_guide_src/source/concepts/autoloader/004.php +++ /dev/null @@ -1,25 +0,0 @@ -load('config'); - +$factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); - +$factoriesCache->load('config'); - // ^^^ Uncomment these lines if you want to use Config Caching. - - /* - @@ -79,7 +79,7 @@ $app->setContext($context); - $app->run(); - - // Save Config Cache - -// $factoriesCache->save('config'); - +$factoriesCache->save('config'); - // ^^^ Uncomment this line if you want to use Config Caching. - - // Exits the application, setting the exit code for CLI-based applications +.. versionadded:: 4.5.0 + +Set the following property to ``true`` in **app/Config/Optimize.php**:: + + public bool $configCacheEnabled = true; + +.. note:: + This property cannot be overridden by + :ref:`environment variables `. + +.. note:: + Prior to v4.5.0, uncomment the following code in **public/index.php**:: + + --- a/public/index.php + +++ b/public/index.php + @@ -49,8 +49,8 @@ if (! defined('ENVIRONMENT')) { + } + + // Load Config Cache + -// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); + -// $factoriesCache->load('config'); + +$factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); + +$factoriesCache->load('config'); + // ^^^ Uncomment these lines if you want to use Config Caching. + + /* + @@ -79,7 +79,7 @@ $app->setContext($context); + $app->run(); + + // Save Config Cache + -// $factoriesCache->save('config'); + +$factoriesCache->save('config'); + // ^^^ Uncomment this line if you want to use Config Caching. + + // Exits the application, setting the exit code for CLI-based applications