From c1ca7b720691e91a53470d6fe4c1fc35d6e37617 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 07:20:09 +0900 Subject: [PATCH 1/9] feat: add config Cache::$configCacheEnabled and move code from index.php to Boot class. --- app/Config/Cache.php | 9 +++++ public/index.php | 39 +--------------------- system/Boot.php | 79 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 82 insertions(+), 45 deletions(-) diff --git a/app/Config/Cache.php b/app/Config/Cache.php index b29c13a9ea71..fb63945a2a90 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -168,4 +168,13 @@ class Cache extends BaseConfig 'redis' => RedisHandler::class, 'wincache' => WincacheHandler::class, ]; + + /** + * -------------------------------------------------------------------------- + * Config Caching + * -------------------------------------------------------------------------- + * + * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching + */ + public bool $configCacheEnabled = false; } diff --git a/public/index.php b/public/index.php index f384dd5416cf..0f9da5e48fb8 100644 --- a/public/index.php +++ b/public/index.php @@ -52,42 +52,5 @@ // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->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/system/Boot.php b/system/Boot.php index ba94cce6b910..9682ceba7376 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -13,8 +13,10 @@ namespace CodeIgniter; +use CodeIgniter\Cache\FactoriesCache; use CodeIgniter\Config\DotEnv; use Config\Autoload; +use Config\Cache; use Config\Modules; use Config\Paths; use Config\Services; @@ -32,21 +34,47 @@ 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 = (new Cache())->configCacheEnabled ?? false; + if ($configCacheEnabled) { + $factoriesCache = static::loadConfigCache(); + } + + $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` */ public static function bootSpark(Paths $paths): void - { - static::boot($paths); - } - - protected static function boot(Paths $paths): void { static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { @@ -234,4 +262,41 @@ 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'); + } } From 97de8bb3ebe8b61204178af9d5895241ec0446d8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 07:55:26 +0900 Subject: [PATCH 2/9] refactor: move code from spark to Boot class --- spark | 38 +------------------------------------- system/Boot.php | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/spark b/spark index a1b42e35b0b7..623b6df1093b 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 9682ceba7376..06c900b7c4e2 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -14,6 +14,7 @@ namespace CodeIgniter; use CodeIgniter\Cache\FactoriesCache; +use CodeIgniter\CLI\Console; use CodeIgniter\Config\DotEnv; use Config\Autoload; use Config\Cache; @@ -73,8 +74,10 @@ public static function bootWeb(Paths $paths): int /** * Used by `spark` + * + * @return int Exit code. */ - public static function bootSpark(Paths $paths): void + public static function bootSpark(Paths $paths): int { static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { @@ -90,6 +93,11 @@ public static function bootSpark(Paths $paths): void static::loadAutoloader(); static::setExceptionHandler(); static::initializeKint(); + + static::initializeCodeIgniter(); + $console = static::initializeConsole(); + + return static::runCommand($console); } /** @@ -299,4 +307,27 @@ 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; + } } From b61baa73904caddaccb39927fc67dc74370c2508 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 20 Feb 2024 08:08:21 +0900 Subject: [PATCH 3/9] docs: update "How to Enable Config Caching" --- user_guide_src/source/concepts/factories.rst | 55 +++++++++++--------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index 76dcc99eb7b3..415361e3d5e3 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -316,27 +316,34 @@ Or simply delete the **writable/cache/FactoriesCache_config** file. How to Enable Config Caching ============================ -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 +.. versionadded:: 4.5.0 + +Set the following property to ``true`` in **app/Config/Cache.php**:: + + public bool $configCacheEnabled = true; + +.. 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 From e32b962bf832aa0a00f8a7f60db6398364d313de Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 08:17:16 +0900 Subject: [PATCH 4/9] config: move $cacheQueryString down This setting is for Web Page Caching, not for Cache service. --- app/Config/Cache.php | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/app/Config/Cache.php b/app/Config/Cache.php index fb63945a2a90..6c61b0de1b56 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 @@ -169,6 +150,25 @@ class Cache extends BaseConfig '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; + /** * -------------------------------------------------------------------------- * Config Caching From 101870612e0f7ab1709b7c82a25bab6488055ef9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 09:12:23 +0900 Subject: [PATCH 5/9] fix: extract autoloadHelpers() --- system/Boot.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/system/Boot.php b/system/Boot.php index 06c900b7c4e2..1f840a5c8732 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -60,6 +60,8 @@ public static function bootWeb(Paths $paths): int $factoriesCache = static::loadConfigCache(); } + static::autoloadHelpers(); + $app = static::initializeCodeIgniter(); static::runCodeIgniter($app); @@ -93,6 +95,7 @@ public static function bootSpark(Paths $paths): int static::loadAutoloader(); static::setExceptionHandler(); static::initializeKint(); + static::autoloadHelpers(); static::initializeCodeIgniter(); $console = static::initializeConsole(); @@ -115,6 +118,7 @@ public static function bootTest(Paths $paths): void static::loadAutoloader(); static::setExceptionHandler(); static::initializeKint(); + static::autoloadHelpers(); } /** @@ -224,6 +228,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(); } From eba1e9ea3660b0390da3518e67392f0f9f03cfe3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 27 Feb 2024 11:06:08 +0900 Subject: [PATCH 6/9] fix: method name case --- public/index.php | 2 +- spark | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/index.php b/public/index.php index 0f9da5e48fb8..5ec58a7729c3 100644 --- a/public/index.php +++ b/public/index.php @@ -53,4 +53,4 @@ // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -exit(CodeIgniter\Boot::BootWeb($paths)); +exit(CodeIgniter\Boot::bootWeb($paths)); diff --git a/spark b/spark index 623b6df1093b..a56fbc1bd7b6 100755 --- a/spark +++ b/spark @@ -81,4 +81,4 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE require $paths->systemDirectory . '/Boot.php'; -exit(CodeIgniter\Boot::BootSpark($paths)); +exit(CodeIgniter\Boot::bootSpark($paths)); From fef194b399d688e1aa52a5f74ab297b3abae01bb Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 29 Feb 2024 09:04:23 +0900 Subject: [PATCH 7/9] perf: add config Optimize and move $configCacheEnabled The Cache Config class that extends BaseConfig is very slow to instantiate.. See https://github.com/codeigniter4/CodeIgniter4/pull/8558#discussion_r1506813270 --- app/Config/Cache.php | 9 -------- app/Config/Optimize.php | 23 ++++++++++++++++++++ system/Boot.php | 5 +++-- user_guide_src/source/concepts/factories.rst | 2 +- 4 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 app/Config/Optimize.php diff --git a/app/Config/Cache.php b/app/Config/Cache.php index 6c61b0de1b56..3fbade6840cc 100644 --- a/app/Config/Cache.php +++ b/app/Config/Cache.php @@ -168,13 +168,4 @@ class Cache extends BaseConfig * @var bool|list */ public $cacheQueryString = false; - - /** - * -------------------------------------------------------------------------- - * Config Caching - * -------------------------------------------------------------------------- - * - * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching - */ - public bool $configCacheEnabled = false; } diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php new file mode 100644 index 000000000000..7895f16b3093 --- /dev/null +++ b/app/Config/Optimize.php @@ -0,0 +1,23 @@ +configCacheEnabled ?? false; + $configCacheEnabled = class_exists(Optimize::class) + && (new Optimize())->configCacheEnabled; if ($configCacheEnabled) { $factoriesCache = static::loadConfigCache(); } diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index 415361e3d5e3..ca6142c7f5ed 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -318,7 +318,7 @@ How to Enable Config Caching .. versionadded:: 4.5.0 -Set the following property to ``true`` in **app/Config/Cache.php**:: +Set the following property to ``true`` in **app/Config/Optimize.php**:: public bool $configCacheEnabled = true; From 8f465638480c164443480c758e55c68f56a2fef9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 29 Feb 2024 09:29:05 +0900 Subject: [PATCH 8/9] feat: add Config\Optimize::$locatorCacheEnabled --- app/Config/Optimize.php | 9 +++++++ system/Config/BaseService.php | 11 +++++++- user_guide_src/source/concepts/autoloader.rst | 4 +-- .../source/concepts/autoloader/004.php | 25 ------------------- 4 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 user_guide_src/source/concepts/autoloader/004.php diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php index 7895f16b3093..6fb441fd2288 100644 --- a/app/Config/Optimize.php +++ b/app/Config/Optimize.php @@ -20,4 +20,13 @@ class Optimize * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching */ public bool $configCacheEnabled = false; + + /** + * -------------------------------------------------------------------------- + * Config Caching + * -------------------------------------------------------------------------- + * + * @see https://codeigniter.com/user_guide/concepts/autoloader.html#file-locator-caching + */ + public bool $locatorCacheEnabled = false; } 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..3bbd5f8d1fcd 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -190,6 +190,6 @@ 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; 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 @@ - Date: Thu, 29 Feb 2024 09:36:51 +0900 Subject: [PATCH 9/9] docs: add notes for environment variables --- user_guide_src/source/concepts/autoloader.rst | 4 ++++ user_guide_src/source/concepts/factories.rst | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 3bbd5f8d1fcd..26ce27a673be 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -193,3 +193,7 @@ How to Enable FileLocator Caching Set the following property to ``true`` in **app/Config/Optimize.php**:: public bool $locatorCacheEnabled = true; + +.. note:: + This property cannot be overridden by + :ref:`environment variables `. diff --git a/user_guide_src/source/concepts/factories.rst b/user_guide_src/source/concepts/factories.rst index ca6142c7f5ed..3f0849b5e512 100644 --- a/user_guide_src/source/concepts/factories.rst +++ b/user_guide_src/source/concepts/factories.rst @@ -322,6 +322,10 @@ 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**::