diff --git a/app/Config/Paths.php b/app/Config/Paths.php index 3dc9c5d93951..16dc37068660 100644 --- a/app/Config/Paths.php +++ b/app/Config/Paths.php @@ -75,4 +75,14 @@ class Paths * is used when no value is provided to `Services::renderer()`. */ public string $viewDirectory = __DIR__ . '/../Views'; + + /** + * --------------------------------------------------------------- + * ENVIRONMENT DIRECTORY NAME + * --------------------------------------------------------------- + * + * This variable must contain the name of the directory for + * environment files. + */ + public string $environmentDirectory = __DIR__ . '/../../'; } diff --git a/system/Boot.php b/system/Boot.php index ba3675516b16..283a61bd4769 100644 --- a/system/Boot.php +++ b/system/Boot.php @@ -170,7 +170,7 @@ public static function preload(Paths $paths): void protected static function loadDotEnv(Paths $paths): void { require_once $paths->systemDirectory . '/Config/DotEnv.php'; - (new DotEnv($paths->appDirectory . '/../'))->load(); + (new DotEnv($paths->environmentDirectory))->load(); } protected static function defineEnvironment(): void diff --git a/system/Commands/Encryption/GenerateKey.php b/system/Commands/Encryption/GenerateKey.php index a3fdbd4393a9..3f90203c7d9c 100644 --- a/system/Commands/Encryption/GenerateKey.php +++ b/system/Commands/Encryption/GenerateKey.php @@ -17,6 +17,7 @@ use CodeIgniter\CLI\CLI; use CodeIgniter\Config\DotEnv; use CodeIgniter\Encryption\Encryption; +use Config\Paths; /** * Generates a new encryption key. @@ -101,7 +102,7 @@ public function run(array $params) // force DotEnv to reload the new env vars putenv('encryption.key'); unset($_ENV['encryption.key'], $_SERVER['encryption.key']); - $dotenv = new DotEnv(ROOTPATH); + $dotenv = new DotEnv((new Paths())->environmentDirectory); $dotenv->load(); CLI::write('Application\'s new encryption key was successfully set.', 'green'); @@ -155,7 +156,7 @@ protected function confirmOverwrite(array $params): bool protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): bool { $baseEnv = ROOTPATH . 'env'; - $envFile = ROOTPATH . '.env'; + $envFile = (new Paths())->environmentDirectory . '.env'; if (! is_file($envFile)) { if (! is_file($baseEnv)) { diff --git a/system/Commands/Utilities/Environment.php b/system/Commands/Utilities/Environment.php index 22794fe9d51d..f5caad4ba3f4 100644 --- a/system/Commands/Utilities/Environment.php +++ b/system/Commands/Utilities/Environment.php @@ -16,6 +16,7 @@ use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\CLI; use CodeIgniter\Config\DotEnv; +use Config\Paths; /** * Command to display the current environment, @@ -119,7 +120,7 @@ public function run(array $params) // however we cannot redefine the ENVIRONMENT constant putenv('CI_ENVIRONMENT'); unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']); - (new DotEnv(ROOTPATH))->load(); + (new DotEnv((new Paths())->environmentDirectory))->load(); CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green'); CLI::write('The ENVIRONMENT constant will be changed in the next script execution.'); @@ -134,7 +135,7 @@ public function run(array $params) private function writeNewEnvironmentToEnvFile(string $newEnv): bool { $baseEnv = ROOTPATH . 'env'; - $envFile = ROOTPATH . '.env'; + $envFile = (new Paths())->environmentDirectory . '.env'; if (! is_file($envFile)) { if (! is_file($baseEnv)) {