From 16718829d14e6ac7cc5b2a4f8c4c7eb96d3bb870 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:40:13 +0900 Subject: [PATCH 01/17] refactor: extract Boot class --- public/index.php | 3 +- spark | 3 +- system/Boot.php | 164 +++++++++++++++++++++++++++++++++++++++++++ system/bootstrap.php | 7 ++ 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 system/Boot.php diff --git a/public/index.php b/public/index.php index 0bfbce2053fd..b8072f8e7b46 100644 --- a/public/index.php +++ b/public/index.php @@ -70,7 +70,8 @@ } // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +CodeIgniter\Boot::BootWeb($paths); // Load Config Cache // $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); diff --git a/spark b/spark index 6b4034131925..1be5328728c0 100755 --- a/spark +++ b/spark @@ -98,7 +98,8 @@ if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { } // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +CodeIgniter\Boot::BootSpark($paths); /* * --------------------------------------------------------------- diff --git a/system/Boot.php b/system/Boot.php new file mode 100644 index 000000000000..7ae52f0aabb8 --- /dev/null +++ b/system/Boot.php @@ -0,0 +1,164 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter; + +use CodeIgniter\Exceptions\FrameworkException; +use Config\Autoload; +use Config\Modules; +use Config\Paths; +use Config\Services; + +class Boot +{ + /** + * @used-by public/index.php + * + * Context + * web: Invoked by HTTP request + * php-cli: Invoked by CLI via `php public/index.php` + */ + public static function bootWeb(Paths $paths): void + { + static::definePathConstant($paths); + static::loadConstants(); + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * @used-by spark + */ + public static function bootSpark(Paths $paths): void + { + static::definePathConstant($paths); + static::loadConstants(); + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * The path constants provide convenient access to the folders throughout + * the application. We have to set them up here, so they are available in + * the config files that are loaded. + */ + protected static function definePathConstant(Paths $paths): void + { + // The path to the application directory. + if (! defined('APPPATH')) { + define('APPPATH', realpath(rtrim($paths->appDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); + } + + // The path to the project root directory. Just above APPPATH. + if (! defined('ROOTPATH')) { + define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR); + } + + // The path to the system directory. + if (! defined('SYSTEMPATH')) { + define('SYSTEMPATH', realpath(rtrim($paths->systemDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); + } + + // The path to the writable directory. + if (! defined('WRITEPATH')) { + define('WRITEPATH', realpath(rtrim($paths->writableDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); + } + + // The path to the tests directory + if (! defined('TESTPATH')) { + define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); + } + } + + protected static function loadConstants(): void + { + if (! defined('APP_NAMESPACE')) { + require_once APPPATH . 'Config/Constants.php'; + } + } + + protected static function loadCommonFunctions(): void + { + // Require app/Common.php file if exists. + if (is_file(APPPATH . 'Common.php')) { + require_once APPPATH . 'Common.php'; + } + + // Require system/Common.php + require_once SYSTEMPATH . 'Common.php'; + } + + /** + * The autoloader allows all the pieces to work together in the framework. + * We have to load it here, though, so that the config files can use the + * path constants. + */ + protected static function loadAutoloader(): void + { + if (! class_exists(Autoload::class, false)) { + require_once SYSTEMPATH . 'Config/AutoloadConfig.php'; + require_once APPPATH . 'Config/Autoload.php'; + require_once SYSTEMPATH . 'Modules/Modules.php'; + require_once APPPATH . 'Config/Modules.php'; + } + + require_once SYSTEMPATH . 'Autoloader/Autoloader.php'; + require_once SYSTEMPATH . 'Config/BaseService.php'; + require_once SYSTEMPATH . 'Config/Services.php'; + require_once APPPATH . 'Config/Services.php'; + + // Initialize and register the loader with the SPL autoloader stack. + Services::autoloader()->initialize(new Autoload(), new Modules())->register(); + Services::autoloader()->loadHelpers(); + } + + protected static function setExceptionHandler(): void + { + Services::exceptions()->initialize(); + } + + protected static function checkMissingExtensions(): void + { + // Run this check for manual installations + if (! is_file(COMPOSER_PATH)) { + $missingExtensions = []; + + foreach ([ + 'intl', + 'json', + 'mbstring', + ] as $extension) { + if (! extension_loaded($extension)) { + $missingExtensions[] = $extension; + } + } + + if ($missingExtensions !== []) { + throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); + } + + unset($missingExtensions); + } + } + + protected static function initializeKint(): void + { + Services::autoloader()->initializeKint(CI_DEBUG); + } +} diff --git a/system/bootstrap.php b/system/bootstrap.php index 84d52cef5cbc..f091aa306527 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -11,6 +11,13 @@ * the LICENSE file that was distributed with this source code. */ +/** + * --------------------------------------------------------------- + * + * @deprecated 4.5.0 This file is no longer used. Moved to Boot.php. + * --------------------------------------------------------------- + */ + use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; From 5338b3dbd01781853f6c5afad53538893b0b61f6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:45:40 +0900 Subject: [PATCH 02/17] refactor: move code to Boot class --- public/index.php | 22 ---------------------- spark | 22 ---------------------- system/Boot.php | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 44 deletions(-) diff --git a/public/index.php b/public/index.php index b8072f8e7b46..a93c7d30d80e 100644 --- a/public/index.php +++ b/public/index.php @@ -47,28 +47,6 @@ $paths = new Config\Paths(); -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); - -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - -// LOAD ENVIRONMENT BOOTSTRAP -if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { - require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; -} else { - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo 'The application environment is not set correctly.'; - - exit(EXIT_ERROR); // EXIT_ERROR -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; CodeIgniter\Boot::BootWeb($paths); diff --git a/spark b/spark index 1be5328728c0..172bacc8c500 100755 --- a/spark +++ b/spark @@ -75,28 +75,6 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); - -// DEFINE ENVIRONMENT -if (! defined('ENVIRONMENT')) { - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); -} - -// LOAD ENVIRONMENT BOOTSTRAP -if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { - require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; -} else { - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo 'The application environment is not set correctly.'; - - exit(EXIT_ERROR); // EXIT_ERROR -} - // LOAD THE FRAMEWORK BOOTSTRAP FILE require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; CodeIgniter\Boot::BootSpark($paths); diff --git a/system/Boot.php b/system/Boot.php index 7ae52f0aabb8..00ae0f61de25 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -13,6 +13,7 @@ namespace CodeIgniter; +use CodeIgniter\Config\DotEnv; use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; @@ -30,6 +31,9 @@ class Boot */ public static function bootWeb(Paths $paths): void { + static::loadDotEnv($paths); + static::defineEnvironment(); + static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); static::loadConstants(); static::loadCommonFunctions(); @@ -44,6 +48,9 @@ public static function bootWeb(Paths $paths): void */ public static function bootSpark(Paths $paths): void { + static::loadDotEnv($paths); + static::defineEnvironment(); + static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); static::loadConstants(); static::loadCommonFunctions(); @@ -53,6 +60,36 @@ public static function bootSpark(Paths $paths): void static::initializeKint(); } + /** + * Load environment settings from .env files into $_SERVER and $_ENV + */ + protected static function loadDotEnv(Paths $paths): void + { + require_once $paths->systemDirectory . '/Config/DotEnv.php'; + (new DotEnv($paths->appDirectory . '/../'))->load(); + } + + protected static function defineEnvironment(): void + { + if (! defined('ENVIRONMENT')) { + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); + } + } + + protected static function loadEnvironmentBootstrap(Paths $paths): void + { + if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { + require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; + } else { + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR + } + } + /** * The path constants provide convenient access to the folders throughout * the application. We have to set them up here, so they are available in From 529eb954d8ce354dae7149dd9e2c47f9676d3128 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:19:00 +0900 Subject: [PATCH 03/17] refactor: use Boot class in Test/bootstrap.php --- system/Boot.php | 40 ++++++++++++++---- system/Test/bootstrap.php | 86 ++------------------------------------- 2 files changed, 37 insertions(+), 89 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 00ae0f61de25..6902684ca612 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -20,6 +20,9 @@ use Config\Paths; use Config\Services; +/** + * Bootstrap for the application + */ class Boot { /** @@ -35,7 +38,9 @@ public static function bootWeb(Paths $paths): void static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); - static::loadConstants(); + if (! defined('APP_NAMESPACE')) { + static::loadConstants(); + } static::loadCommonFunctions(); static::loadAutoloader(); static::setExceptionHandler(); @@ -52,6 +57,23 @@ public static function bootSpark(Paths $paths): void static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); static::definePathConstant($paths); + if (! defined('APP_NAMESPACE')) { + static::loadConstants(); + } + static::loadCommonFunctions(); + static::loadAutoloader(); + static::setExceptionHandler(); + static::checkMissingExtensions(); + static::initializeKint(); + } + + /** + * @used-by system/Test/bootstrap.php + */ + public static function bootTest(Paths $paths): void + { + static::loadDotEnv($paths); + static::loadEnvironmentBootstrap($paths, false); static::loadConstants(); static::loadCommonFunctions(); static::loadAutoloader(); @@ -72,21 +94,27 @@ protected static function loadDotEnv(Paths $paths): void protected static function defineEnvironment(): void { if (! defined('ENVIRONMENT')) { + // @phpstan-ignore-next-line $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); unset($env); } } - protected static function loadEnvironmentBootstrap(Paths $paths): void + protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = true): void { if (is_file($paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php')) { require_once $paths->appDirectory . '/Config/Boot/' . ENVIRONMENT . '.php'; - } else { + + return; + } + + if ($exit) { header('HTTP/1.1 503 Service Unavailable.', true, 503); echo 'The application environment is not set correctly.'; - exit(EXIT_ERROR); // EXIT_ERROR + exit(EXIT_ERROR); } } @@ -125,9 +153,7 @@ protected static function definePathConstant(Paths $paths): void protected static function loadConstants(): void { - if (! defined('APP_NAMESPACE')) { - require_once APPPATH . 'Config/Constants.php'; - } + require_once APPPATH . 'Config/Constants.php'; } protected static function loadCommonFunctions(): void diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index 776250abfac3..fc113693a69e 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -11,9 +11,7 @@ * the LICENSE file that was distributed with this source code. */ -use CodeIgniter\Config\DotEnv; -use Config\Autoload; -use Config\Modules; +use CodeIgniter\Boot; use Config\Paths; use Config\Services; @@ -80,85 +78,9 @@ * and fires up an environment-specific bootstrapping. */ -// LOAD DOTENV FILE -// Load environment settings from .env files into $_SERVER and $_ENV -require_once $paths->systemDirectory . '/Config/DotEnv.php'; -(new DotEnv($paths->appDirectory . '/../'))->load(); - -/* - * --------------------------------------------------------------- - * LOAD ENVIRONMENT BOOTSTRAP - * --------------------------------------------------------------- - * - * Load any custom boot files based upon the current environment. - * If no boot file exists, we shouldn't continue because something - * is wrong. At the very least, they should have error reporting setup. - */ - -if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { - require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; -} - -/* - * --------------------------------------------------------------- - * GRAB OUR CONSTANTS - * --------------------------------------------------------------- - */ - -require_once APPPATH . 'Config/Constants.php'; - -/* - * --------------------------------------------------------------- - * LOAD COMMON FUNCTIONS - * --------------------------------------------------------------- - */ - -// Load Common.php from App then System -if (is_file(APPPATH . 'Common.php')) { - require_once APPPATH . 'Common.php'; -} - -require_once SYSTEMPATH . 'Common.php'; - -/* - * --------------------------------------------------------------- - * LOAD OUR AUTOLOADER - * --------------------------------------------------------------- - * - * The autoloader allows all of the pieces to work together in the - * framework. We have to load it here, though, so that the config - * files can use the path constants. - */ - -require_once SYSTEMPATH . 'Config/AutoloadConfig.php'; -require_once APPPATH . 'Config/Autoload.php'; -require_once SYSTEMPATH . 'Modules/Modules.php'; -require_once APPPATH . 'Config/Modules.php'; - -require_once SYSTEMPATH . 'Autoloader/Autoloader.php'; -require_once SYSTEMPATH . 'Config/BaseService.php'; -require_once SYSTEMPATH . 'Config/Services.php'; -require_once APPPATH . 'Config/Services.php'; - -// Initialize and register the loader with the SPL autoloader stack. -Services::autoloader()->initialize(new Autoload(), new Modules())->register(); -Services::autoloader()->loadHelpers(); - -/* - * --------------------------------------------------------------- - * SET EXCEPTION AND ERROR HANDLERS - * --------------------------------------------------------------- - */ - -Services::exceptions()->initialize(); - -/* - * --------------------------------------------------------------- - * INITIALIZE KINT - * --------------------------------------------------------------- - */ - -Services::autoloader()->initializeKint(CI_DEBUG); +// LOAD THE FRAMEWORK BOOTSTRAP FILE +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +Boot::bootTest($paths); /* * --------------------------------------------------------------- From 56916891b976191dbeba7532e0900b096f187a6b Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:50:55 +0900 Subject: [PATCH 04/17] refactor: fix method name --- system/Boot.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 6902684ca612..a1a9ca4a1168 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -37,7 +37,7 @@ public static function bootWeb(Paths $paths): void static::loadDotEnv($paths); static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); - static::definePathConstant($paths); + static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { static::loadConstants(); } @@ -56,7 +56,7 @@ public static function bootSpark(Paths $paths): void static::loadDotEnv($paths); static::defineEnvironment(); static::loadEnvironmentBootstrap($paths); - static::definePathConstant($paths); + static::definePathConstants($paths); if (! defined('APP_NAMESPACE')) { static::loadConstants(); } @@ -123,7 +123,7 @@ protected static function loadEnvironmentBootstrap(Paths $paths, bool $exit = tr * the application. We have to set them up here, so they are available in * the config files that are loaded. */ - protected static function definePathConstant(Paths $paths): void + protected static function definePathConstants(Paths $paths): void { // The path to the application directory. if (! defined('APPPATH')) { From c5191a08930a1513d35cf13b0492bc3bd6ce1694 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:55:41 +0900 Subject: [PATCH 05/17] refactor: remove rtrim() --- public/index.php | 2 +- spark | 2 +- system/Test/bootstrap.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/public/index.php b/public/index.php index a93c7d30d80e..057ec8a111c3 100644 --- a/public/index.php +++ b/public/index.php @@ -48,7 +48,7 @@ $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; CodeIgniter\Boot::BootWeb($paths); // Load Config Cache diff --git a/spark b/spark index 172bacc8c500..8588352a7e50 100755 --- a/spark +++ b/spark @@ -76,7 +76,7 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; CodeIgniter\Boot::BootSpark($paths); /* diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index fc113693a69e..4a068a4c97b1 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -79,7 +79,7 @@ */ // LOAD THE FRAMEWORK BOOTSTRAP FILE -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'Boot.php'; +require $paths->systemDirectory . '/Boot.php'; Boot::bootTest($paths); /* From 41db7666757bb6b6a88ba7ecc7b1859b0b9f37f8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 12:52:25 +0900 Subject: [PATCH 06/17] docs: update comments --- spark | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spark b/spark index 8588352a7e50..a1b42e35b0b7 100755 --- a/spark +++ b/spark @@ -16,9 +16,12 @@ * -------------------------------------------------------------------- * The main entry point into the CLI system and allows you to run * commands and perform maintenance on your application. - * - * Because CodeIgniter can handle CLI requests as just another web request - * this class mainly acts as a passthru to the framework itself. + */ + +/* + *--------------------------------------------------------------- + * CHECK SERVER API + *--------------------------------------------------------------- */ // Refuse to run when called from php-cgi From 7b81bb056b26890e22240f7c6d7d220ed770bb44 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 13:35:17 +0900 Subject: [PATCH 07/17] docs: add user guide --- user_guide_src/source/changelogs/v4.5.0.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index d4219c7b0333..b311358e7993 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -185,6 +185,8 @@ Others - **Web Page Caching:** ``ResponseCache`` has been improved to include the request HTTP method in the cache key. This means that the same URI will be cached separately if the HTTP method is different. +- **Bootstrap:** The ``CodeIgniter\Boot`` class has been introduced, replacing + **system/bootstrap.php**. ******** BREAKING @@ -469,6 +471,8 @@ Deprecations been moved to the ``Autoloader``. - The ``configureKint()`` method has been deprecated. No longer used. It has been moved to the ``Autoloader``. +- **system/bootstrap.php:** This file has been deprecated. No longer used. + The code has been moved to the new ``CodeIgniter\Boot`` class. - **Response:** The constructor parameter ``$config`` has been deprecated. No longer used. - **Filters:** From c33573142925dd0f66577defbeda01324342b910 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 13:52:24 +0900 Subject: [PATCH 08/17] docs: add @codeCoverageIgnore --- system/Boot.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/system/Boot.php b/system/Boot.php index a1a9ca4a1168..0001db6ba051 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -22,6 +22,8 @@ /** * Bootstrap for the application + * + * @codeCoverageIgnore */ class Boot { From 4a430eae98f3acff34bc9f81f9583ac5bdf8263c Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 16 Mar 2024 10:11:22 +0900 Subject: [PATCH 09/17] docs: remove space --- system/Boot.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Boot.php b/system/Boot.php index 0001db6ba051..856b7da1be87 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -30,7 +30,7 @@ class Boot /** * @used-by public/index.php * - * Context + * Context * web: Invoked by HTTP request * php-cli: Invoked by CLI via `php public/index.php` */ From 4cd1a70eaf984a8f22f161dfd38be8965bb70174 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 08:43:48 +0900 Subject: [PATCH 10/17] docs: remove @used-by We should write FQSEN, but a PHP file is not a FQSEN. @uses [FQSEN] [] https://docs.phpdoc.org/guide/references/phpdoc/tags/uses.html#uses-used-by --- system/Boot.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 856b7da1be87..839394549d88 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -28,7 +28,7 @@ class Boot { /** - * @used-by public/index.php + * Used by `public/index.php` * * Context * web: Invoked by HTTP request @@ -51,7 +51,7 @@ public static function bootWeb(Paths $paths): void } /** - * @used-by spark + * Used by `spark` */ public static function bootSpark(Paths $paths): void { @@ -70,7 +70,7 @@ public static function bootSpark(Paths $paths): void } /** - * @used-by system/Test/bootstrap.php + * Used by `system/Test/bootstrap.php` */ public static function bootTest(Paths $paths): void { From a95c6f180238be58bdd0a5d9df1531ab3f6cba9b Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 08:46:51 +0900 Subject: [PATCH 11/17] refactor: extract boot() method --- system/Boot.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 839394549d88..abe0d3fa9484 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -36,24 +36,18 @@ class Boot */ public static function bootWeb(Paths $paths): void { - static::loadDotEnv($paths); - static::defineEnvironment(); - static::loadEnvironmentBootstrap($paths); - static::definePathConstants($paths); - if (! defined('APP_NAMESPACE')) { - static::loadConstants(); - } - static::loadCommonFunctions(); - static::loadAutoloader(); - static::setExceptionHandler(); - static::checkMissingExtensions(); - static::initializeKint(); + static::boot($paths); } /** * Used by `spark` */ public static function bootSpark(Paths $paths): void + { + static::boot($paths); + } + + protected static function boot(Paths $paths): void { static::loadDotEnv($paths); static::defineEnvironment(); From 1022a335d5f72116918fbf7db814a7512c47ba74 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 08:50:47 +0900 Subject: [PATCH 12/17] refactor: improve readability --- system/Boot.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index abe0d3fa9484..ae48feac5879 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -91,10 +91,11 @@ protected static function defineEnvironment(): void { if (! defined('ENVIRONMENT')) { // @phpstan-ignore-next-line - $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] + ?? getenv('CI_ENVIRONMENT') + ?: 'production'; - define('ENVIRONMENT', ($env !== false) ? $env : 'production'); - unset($env); + define('ENVIRONMENT', $env); } } From 61bf440c2471df9e0cb1afc1edd246c02725c9fb Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 08:51:29 +0900 Subject: [PATCH 13/17] refactor: remove unneeded unset() --- system/Boot.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index ae48feac5879..370610c02fa5 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -212,8 +212,6 @@ protected static function checkMissingExtensions(): void if ($missingExtensions !== []) { throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); } - - unset($missingExtensions); } } From dc9caaebef0c42f5970471aa03df7175aea78c18 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 09:26:54 +0900 Subject: [PATCH 14/17] refactor: move checkMissingExtensions() up The translated error message is no longer provided. This is because the checking must be done at a very early stage. --- system/Boot.php | 27 +++++++++++++++++------- system/Exceptions/FrameworkException.php | 2 ++ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 370610c02fa5..b601d0f5fe77 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -14,7 +14,6 @@ namespace CodeIgniter; use CodeIgniter\Config\DotEnv; -use CodeIgniter\Exceptions\FrameworkException; use Config\Autoload; use Config\Modules; use Config\Paths; @@ -49,17 +48,19 @@ public static function bootSpark(Paths $paths): void protected static function boot(Paths $paths): void { - static::loadDotEnv($paths); - static::defineEnvironment(); - static::loadEnvironmentBootstrap($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::checkMissingExtensions(); static::initializeKint(); } @@ -68,13 +69,15 @@ protected static function boot(Paths $paths): void */ public static function bootTest(Paths $paths): void { + static::loadConstants(); + static::checkMissingExtensions(); + static::loadDotEnv($paths); static::loadEnvironmentBootstrap($paths, false); - static::loadConstants(); + static::loadCommonFunctions(); static::loadAutoloader(); static::setExceptionHandler(); - static::checkMissingExtensions(); static::initializeKint(); } @@ -210,7 +213,15 @@ protected static function checkMissingExtensions(): void } if ($missingExtensions !== []) { - throw FrameworkException::forMissingExtension(implode(', ', $missingExtensions)); + $message = sprintf( + 'The framework needs the following extension(s) installed and loaded: %s.', + implode(', ', $missingExtensions) + ); + + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo $message; + + exit(EXIT_ERROR); } } } diff --git a/system/Exceptions/FrameworkException.php b/system/Exceptions/FrameworkException.php index fb371bc49c8d..a223f8ec927a 100644 --- a/system/Exceptions/FrameworkException.php +++ b/system/Exceptions/FrameworkException.php @@ -59,6 +59,8 @@ public static function forCopyError(string $path) /** * @return static + * + * @deprecated 4.5.0 No longer used. */ public static function forMissingExtension(string $extension) { From a5ee34096c060b92a1bb97a898d548046515167b Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 09:40:19 +0900 Subject: [PATCH 15/17] refactor: early return --- system/Boot.php | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index b601d0f5fe77..ba94cce6b910 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -198,32 +198,36 @@ protected static function setExceptionHandler(): void protected static function checkMissingExtensions(): void { + if (is_file(COMPOSER_PATH)) { + return; + } + // Run this check for manual installations - if (! is_file(COMPOSER_PATH)) { - $missingExtensions = []; - - foreach ([ - 'intl', - 'json', - 'mbstring', - ] as $extension) { - if (! extension_loaded($extension)) { - $missingExtensions[] = $extension; - } + $missingExtensions = []; + + foreach ([ + 'intl', + 'json', + 'mbstring', + ] as $extension) { + if (! extension_loaded($extension)) { + $missingExtensions[] = $extension; } + } - if ($missingExtensions !== []) { - $message = sprintf( - 'The framework needs the following extension(s) installed and loaded: %s.', - implode(', ', $missingExtensions) - ); + if ($missingExtensions === []) { + return; + } - header('HTTP/1.1 503 Service Unavailable.', true, 503); - echo $message; + $message = sprintf( + 'The framework needs the following extension(s) installed and loaded: %s.', + implode(', ', $missingExtensions) + ); - exit(EXIT_ERROR); - } - } + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo $message; + + exit(EXIT_ERROR); } protected static function initializeKint(): void From 44583a412e41177b93e2f51fffc88d6c6ae7f4ea Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 09:42:29 +0900 Subject: [PATCH 16/17] fix: change HTTP status code from 200 to 503 --- public/index.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/public/index.php b/public/index.php index 057ec8a111c3..f384dd5416cf 100644 --- a/public/index.php +++ b/public/index.php @@ -14,7 +14,10 @@ PHP_VERSION ); - exit($message); + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo $message; + + exit(1); } /* From 842fd6eae9d8ddad0177d47da587c38e6f721e25 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 17 Mar 2024 09:59:08 +0900 Subject: [PATCH 17/17] feat: "system/bootstrap.php" shows upgrade error message --- system/bootstrap.php | 12 ++++++++++-- user_guide_src/source/changelogs/v4.5.0.rst | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/system/bootstrap.php b/system/bootstrap.php index f091aa306527..c0b021494737 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -13,8 +13,7 @@ /** * --------------------------------------------------------------- - * - * @deprecated 4.5.0 This file is no longer used. Moved to Boot.php. + * This file cannot be used. The code has moved to Boot.php. * --------------------------------------------------------------- */ @@ -24,6 +23,13 @@ use Config\Paths; use Config\Services; +header('HTTP/1.1 503 Service Unavailable.', true, 503); + +$message = 'This "system/bootstrap.php" is no longer used. If you are seeing this error message, +the upgrade is not complete. Please refer to the upgrade guide and complete the upgrade. +See https://codeigniter4.github.io/userguide/installation/upgrade_450.html' . PHP_EOL; +echo $message; + /* * --------------------------------------------------------------- * SETUP OUR PATH CONSTANTS @@ -153,3 +159,5 @@ */ Services::autoloader()->initializeKint(CI_DEBUG); + +exit(1); diff --git a/user_guide_src/source/changelogs/v4.5.0.rst b/user_guide_src/source/changelogs/v4.5.0.rst index b311358e7993..7d1a6c1023d4 100644 --- a/user_guide_src/source/changelogs/v4.5.0.rst +++ b/user_guide_src/source/changelogs/v4.5.0.rst @@ -251,6 +251,8 @@ Others - **BaseModel:** The ``getIdValue()`` method has been changed to ``abstract``. - **Routing:** The :ref:`404-override` feature does change the Response status code to 404 by default. See :ref:`Upgrading Guide `. +- **system/bootstrap.php:** This file cannot be used. The code has been moved to + the new ``CodeIgniter\Boot`` class. Interface Changes ================= @@ -471,8 +473,6 @@ Deprecations been moved to the ``Autoloader``. - The ``configureKint()`` method has been deprecated. No longer used. It has been moved to the ``Autoloader``. -- **system/bootstrap.php:** This file has been deprecated. No longer used. - The code has been moved to the new ``CodeIgniter\Boot`` class. - **Response:** The constructor parameter ``$config`` has been deprecated. No longer used. - **Filters:**