Skip to content

Commit 23c6881

Browse files
feat: add environment directory to load .env file
1 parent b8a036d commit 23c6881

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

app/Config/Paths.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,14 @@ class Paths
7575
* is used when no value is provided to `Services::renderer()`.
7676
*/
7777
public string $viewDirectory = __DIR__ . '/../Views';
78+
79+
/**
80+
* ---------------------------------------------------------------
81+
* ENVIRONMENT DIRECTORY NAME
82+
* ---------------------------------------------------------------
83+
*
84+
* This variable must contain the name of the directory for
85+
* environment files.
86+
*/
87+
public string $environmentDirectory = __DIR__ . '/../../';
7888
}

system/Boot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static function preload(Paths $paths): void
170170
protected static function loadDotEnv(Paths $paths): void
171171
{
172172
require_once $paths->systemDirectory . '/Config/DotEnv.php';
173-
(new DotEnv($paths->appDirectory . '/../'))->load();
173+
(new DotEnv($paths->environmentDirectory))->load();
174174
}
175175

176176
protected static function defineEnvironment(): void

system/Commands/Encryption/GenerateKey.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Config\DotEnv;
1919
use CodeIgniter\Encryption\Encryption;
20+
use Config\Paths;
2021

2122
/**
2223
* Generates a new encryption key.
@@ -101,7 +102,7 @@ public function run(array $params)
101102
// force DotEnv to reload the new env vars
102103
putenv('encryption.key');
103104
unset($_ENV['encryption.key'], $_SERVER['encryption.key']);
104-
$dotenv = new DotEnv(ROOTPATH);
105+
$dotenv = new DotEnv((new Paths())->environmentDirectory);
105106
$dotenv->load();
106107

107108
CLI::write('Application\'s new encryption key was successfully set.', 'green');
@@ -155,7 +156,7 @@ protected function confirmOverwrite(array $params): bool
155156
protected function writeNewEncryptionKeyToFile(string $oldKey, string $newKey): bool
156157
{
157158
$baseEnv = ROOTPATH . 'env';
158-
$envFile = ROOTPATH . '.env';
159+
$envFile = (new Paths())->environmentDirectory . '.env';
159160

160161
if (! is_file($envFile)) {
161162
if (! is_file($baseEnv)) {

system/Commands/Utilities/Environment.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use CodeIgniter\CLI\BaseCommand;
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Config\DotEnv;
19+
use Config\Paths;
1920

2021
/**
2122
* Command to display the current environment,
@@ -119,7 +120,7 @@ public function run(array $params)
119120
// however we cannot redefine the ENVIRONMENT constant
120121
putenv('CI_ENVIRONMENT');
121122
unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']);
122-
(new DotEnv(ROOTPATH))->load();
123+
(new DotEnv((new Paths())->environmentDirectory))->load();
123124

124125
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
125126
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
@@ -134,7 +135,7 @@ public function run(array $params)
134135
private function writeNewEnvironmentToEnvFile(string $newEnv): bool
135136
{
136137
$baseEnv = ROOTPATH . 'env';
137-
$envFile = ROOTPATH . '.env';
138+
$envFile = (new Paths())->environmentDirectory . '.env';
138139

139140
if (! is_file($envFile)) {
140141
if (! is_file($baseEnv)) {

0 commit comments

Comments
 (0)