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 08c6b2e52519..cb70a435831b 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); /* * ---------------------------------------------------------------