From 88d4e6ea21db596a90d583fa656f91408bd7affa Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Feb 2024 19:34:27 +0900 Subject: [PATCH] refactor: move CodeIgniter::bootstrapEnvironment() to bootstrap.php --- public/index.php | 14 ++++++++------ spark | 14 ++++++++------ system/CodeIgniter.php | 5 ++--- system/Test/bootstrap.php | 9 +++++++++ system/bootstrap.php | 20 ++++++++++++++++++++ 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/public/index.php b/public/index.php index 8cf5ce347c82..6d78241411ba 100644 --- a/public/index.php +++ b/public/index.php @@ -36,18 +36,20 @@ $paths = new Config\Paths(); -// Location of the framework bootstrap file. -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - // Load environment settings from .env files into $_SERVER and $_ENV -require_once SYSTEMPATH . 'Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); // Define ENVIRONMENT if (! defined('ENVIRONMENT')) { - define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production')); + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); } +// Location of the framework bootstrap file. +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; + // Load Config Cache // $factoriesCache = new \CodeIgniter\Cache\FactoriesCache(); // $factoriesCache->load('config'); diff --git a/spark b/spark index 7029eee1f775..7428b8a392a5 100755 --- a/spark +++ b/spark @@ -71,18 +71,20 @@ require FCPATH . '../app/Config/Paths.php'; $paths = new Config\Paths(); -// Location of the framework bootstrap file. -require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; - // Load environment settings from .env files into $_SERVER and $_ENV -require_once SYSTEMPATH . 'Config/DotEnv.php'; -(new CodeIgniter\Config\DotEnv(ROOTPATH))->load(); +require_once $paths->systemDirectory . '/Config/DotEnv.php'; +(new CodeIgniter\Config\DotEnv($paths->appDirectory . '/../'))->load(); // Define ENVIRONMENT if (! defined('ENVIRONMENT')) { - define('ENVIRONMENT', env('CI_ENVIRONMENT', 'production')); + $env = $_ENV['CI_ENVIRONMENT'] ?? $_SERVER['CI_ENVIRONMENT'] ?? getenv('CI_ENVIRONMENT'); + define('ENVIRONMENT', ($env !== false) ? $env : 'production'); + unset($env); } +// Location of the framework bootstrap file. +require rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php'; + // Grab our CodeIgniter $app = Config\Services::codeigniter(); $app->initialize(); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 85776368ed24..2bd5965bb22a 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -186,9 +186,6 @@ public function __construct(App $config) */ public function initialize() { - // Define environment variables - $this->bootstrapEnvironment(); - // Setup Exception Handling Services::exceptions()->initialize(); @@ -584,6 +581,8 @@ protected function detectEnvironment() * is wrong. At the very least, they should have error reporting setup. * * @return void + * + * @deprecated 4.5.0 Moved to system/bootstrap.php. */ protected function bootstrapEnvironment() { diff --git a/system/Test/bootstrap.php b/system/Test/bootstrap.php index f7f8b83bcfd2..baa0c7f34b5f 100644 --- a/system/Test/bootstrap.php +++ b/system/Test/bootstrap.php @@ -51,6 +51,14 @@ defined('COMPOSER_PATH') || define('COMPOSER_PATH', (string) realpath(HOMEPATH . 'vendor/autoload.php')); defined('VENDORPATH') || define('VENDORPATH', realpath(HOMEPATH . 'vendor') . DIRECTORY_SEPARATOR); +if (is_file(APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php')) { + require_once APPPATH . 'Config/Boot/' . ENVIRONMENT . '.php'; +} else { + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR +} + // Load Common.php from App then System if (is_file(APPPATH . 'Common.php')) { require_once APPPATH . 'Common.php'; @@ -78,6 +86,7 @@ // Initialize and register the loader with the SPL autoloader stack. Services::autoloader()->initialize(new Autoload(), new Modules())->register(); Services::autoloader()->loadHelpers(); +Services::autoloader()->initializeKint(); // Now load Composer's if it's available if (is_file(COMPOSER_PATH)) { diff --git a/system/bootstrap.php b/system/bootstrap.php index e5ddf9d0aa32..5d24137a5a57 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -53,6 +53,26 @@ define('TESTPATH', realpath(rtrim($paths->testsDirectory, '\\/ ')) . DIRECTORY_SEPARATOR); } +/** + * --------------------------------------------------------------- + * 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'; +} else { + // @codeCoverageIgnoreStart + header('HTTP/1.1 503 Service Unavailable.', true, 503); + echo 'The application environment is not set correctly.'; + + exit(EXIT_ERROR); // EXIT_ERROR + // @codeCoverageIgnoreEnd +} + /* * --------------------------------------------------------------- * GRAB OUR CONSTANTS & COMMON