Skip to content

Commit

Permalink
refactor: move code to Boot class
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Feb 19, 2024
1 parent 5280d59 commit ecb93d8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
22 changes: 0 additions & 22 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 0 additions & 22 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 37 additions & 0 deletions system/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter;

use CodeIgniter\Config\DotEnv;
use CodeIgniter\Exceptions\FrameworkException;
use Config\Autoload;
use Config\Modules;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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
Expand Down

0 comments on commit ecb93d8

Please sign in to comment.