From 72138c7fb5494b988c5245ce96ac96ba6a6216ca Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 11:19:00 +0900 Subject: [PATCH] refactor: use Boot class in Test/bootstrap.php --- system/Boot.php | 35 ++++++++++++---- system/Test/bootstrap.php | 85 ++------------------------------------- 2 files changed, 31 insertions(+), 89 deletions(-) diff --git a/system/Boot.php b/system/Boot.php index 00ae0f61de25..16e1ad54ef15 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -35,7 +35,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 +54,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(); @@ -78,15 +97,19 @@ protected static function defineEnvironment(): void } } - 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 +148,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 08c6b2e52519..5ed9284d838b 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -11,9 +11,6 @@ * the LICENSE file that was distributed with this source code. */ -use CodeIgniter\Config\DotEnv; -use Config\Autoload; -use Config\Modules; use Config\Paths; use Config\Services; @@ -80,85 +77,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'; +CodeIgniter\Boot::BootTest($paths); /* * ---------------------------------------------------------------