From ecb93d8cfa4e5e8e404883f724331450ebafa1f6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Mon, 19 Feb 2024 09:45:40 +0900 Subject: [PATCH] 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 0c67f2e0894a..721fc9e4aa56 100755 --- a/spark +++ b/spark @@ -82,28 +82,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