Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Phaseolies/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Phaseolies\DI\Container;
use Phaseolies\Config\Config;
use Phaseolies\ApplicationBuilder;
use Dotenv\Dotenv;

class Application extends Container
{
Expand Down Expand Up @@ -163,6 +164,7 @@ class Application extends Container
public function __construct()
{
parent::setInstance($this);
$this->loadEnvironmentVariables();
$this->withExceptionHandler();
$this->withConfiguration();
$this->bindSingletonClasses();
Expand Down Expand Up @@ -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
*
Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
27 changes: 16 additions & 11 deletions src/Phaseolies/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
}
}
33 changes: 0 additions & 33 deletions src/Phaseolies/Providers/EnvServiceProvider.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading