From a9335c8be612a1b2dce9cf432618ea81c0d941cc Mon Sep 17 00:00:00 2001 From: Arif Hoque Date: Sat, 14 Mar 2026 15:54:01 +0600 Subject: [PATCH] fix: load .env before config cache to prevent null env values on startup --- src/Phaseolies/Application.php | 20 +++++++++-- src/Phaseolies/Config/Config.php | 27 ++++++++------- .../Providers/EnvServiceProvider.php | 33 ------------------- tests/Application/ApplicationTest.php | 1 - 4 files changed, 34 insertions(+), 47 deletions(-) delete mode 100644 src/Phaseolies/Providers/EnvServiceProvider.php diff --git a/src/Phaseolies/Application.php b/src/Phaseolies/Application.php index 73a8c924..e767fb45 100644 --- a/src/Phaseolies/Application.php +++ b/src/Phaseolies/Application.php @@ -11,6 +11,7 @@ use Phaseolies\DI\Container; use Phaseolies\Config\Config; use Phaseolies\ApplicationBuilder; +use Dotenv\Dotenv; class Application extends Container { @@ -163,6 +164,7 @@ class Application extends Container public function __construct() { parent::setInstance($this); + $this->loadEnvironmentVariables(); $this->withExceptionHandler(); $this->withConfiguration(); $this->bindSingletonClasses(); @@ -220,6 +222,21 @@ public function withExceptionHandler(): self return $this; } + /** + * Load environment variables from .env file + * + * @return void + */ + protected function loadEnvironmentVariables(): void + { + if (isset($_ENV['APP_ENV'])) { + return; + } + + $dotenv = Dotenv::createImmutable(base_path()); + $dotenv->safeLoad(); + } + /** * Registers the application configuration * @@ -232,7 +249,7 @@ public function withConfiguration(): self $this->cachedConfig = true; } - $this->environment = $this->cachedConfig['app.env'] ?? env('APP_ENV'); + $this->environment = config('app.env') ?? env('APP_ENV'); return $this; } @@ -628,7 +645,6 @@ protected function bindSingletonClasses(): void protected function loadCoreProviders(): array { return [ - \Phaseolies\Providers\EnvServiceProvider::class, \Phaseolies\Providers\FacadeServiceProvider::class, \Phaseolies\Providers\LanguageServiceProvider::class, \Phaseolies\Providers\SessionServiceProvider::class, diff --git a/src/Phaseolies/Config/Config.php b/src/Phaseolies/Config/Config.php index 0b524d10..49620e2c 100644 --- a/src/Phaseolies/Config/Config.php +++ b/src/Phaseolies/Config/Config.php @@ -62,6 +62,12 @@ protected static function getCacheKey(): string foreach ($files as $file) { $hashes[] = md5_file($file) . '|' . filemtime($file); } + + $envFile = base_path('.env'); + if (file_exists($envFile)) { + $hashes[] = md5_file($envFile) . '|' . filemtime($envFile); + } + $cacheKey = 'config_' . md5(implode('', $hashes)); } @@ -111,6 +117,10 @@ public static function loadFromCache(): void */ public static function cacheConfig(): void { + if (!isset($_ENV['APP_ENV'])) { + return; + } + if (self::$loadedFromCache && !self::configWasModified()) { return; } @@ -262,18 +272,13 @@ public static function clearCache(): void */ public static function isCacheValid(): bool { - static $cacheValid = null; - - if ($cacheValid === null) { - if (!file_exists(self::$cacheFile)) { - $cacheValid = false; - } else { - $cached = include self::$cacheFile; - $cacheValid = isset($cached['_meta']['cache_key']) && - $cached['_meta']['cache_key'] === self::getCacheKey(); - } + if (!file_exists(self::$cacheFile)) { + return false; } - return $cacheValid; + $cached = include self::$cacheFile; + + return isset($cached['_meta']['cache_key']) && + $cached['_meta']['cache_key'] === self::getCacheKey(); } } diff --git a/src/Phaseolies/Providers/EnvServiceProvider.php b/src/Phaseolies/Providers/EnvServiceProvider.php deleted file mode 100644 index 03345f67..00000000 --- a/src/Phaseolies/Providers/EnvServiceProvider.php +++ /dev/null @@ -1,33 +0,0 @@ -app->basePath()); - $dotenv->load(); - } - - /** - * Bootstrap any application services. - * - * @return void - */ - public function boot() - { - // - } -} diff --git a/tests/Application/ApplicationTest.php b/tests/Application/ApplicationTest.php index f0c4a1b0..93e9c2fc 100644 --- a/tests/Application/ApplicationTest.php +++ b/tests/Application/ApplicationTest.php @@ -204,7 +204,6 @@ public function testCoreProvidersAreLoaded(): void $providers = $this->callProtectedMethod($this->app, 'loadCoreProviders'); $this->assertIsArray($providers); - $this->assertContains(\Phaseolies\Providers\EnvServiceProvider::class, $providers); $this->assertContains(\Phaseolies\Providers\RouteServiceProvider::class, $providers); $this->assertContains(\Phaseolies\Providers\LanguageServiceProvider::class, $providers); }