diff --git a/composer.json b/composer.json index 204747f..2d397c7 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "vimeo/psalm": "3.10.1", "sylius-labs/coding-standard": "^3.0", "symfony/browser-kit": "^4.4|^5.0", + "symfony/dotenv": "^4.4|^5.0", "symfony/framework-bundle": "^4.4|^5.0", "symfony/process": "^4.4|^5.0", "symfony/yaml": "^4.4|^5.0" diff --git a/src/ServiceContainer/SymfonyExtension.php b/src/ServiceContainer/SymfonyExtension.php index e27a8e1..7b0ae25 100644 --- a/src/ServiceContainer/SymfonyExtension.php +++ b/src/ServiceContainer/SymfonyExtension.php @@ -22,6 +22,8 @@ use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Parameter; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\Dotenv\Dotenv; +use Symfony\Component\Dotenv\Exception\PathException; final class SymfonyExtension implements Extension { @@ -75,6 +77,10 @@ public function load(ContainerBuilder $container, array $config): void $this->loadBootstrap($this->autodiscoverBootstrap($config['bootstrap'])); + if (empty($config['bootstrap'])) { + $this->loadEnv($config); + } + $this->loadKernel($container, $this->autodiscoverKernelConfiguration($config['kernel'])); $this->loadDriverKernel($container); @@ -182,6 +188,17 @@ private function loadBootstrap(?string $bootstrap): void require_once $bootstrap; } + private function loadEnv(array $config) + { + $env = $config['environment'] ?? $_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? 'test'; + + try { + (new Dotenv())->bootEnv(basename(dirname(__DIR__)) . '/../.env', $env); + } catch (PathException $exception) { + return; + } + } + private function fallbackToTestEnvironment(): void { // If there's no defined server / environment variable with an environment, default to test