diff --git a/src/Roots/Acorn/Bootloader.php b/src/Roots/Acorn/Bootloader.php index 15fa12b2..bd23b9de 100644 --- a/src/Roots/Acorn/Bootloader.php +++ b/src/Roots/Acorn/Bootloader.php @@ -266,6 +266,8 @@ public function getApplication(): ApplicationContract { $this->app ??= new Application($this->basePath(), $this->usePaths()); + $this->app->useEnvironmentPath($this->environmentPath()); + $this->app->singleton( \Illuminate\Contracts\Http\Kernel::class, \Roots\Acorn\Http\Kernel::class @@ -315,6 +317,16 @@ protected function basePath(): string }; } + /** + * Get the environment file path. + */ + protected function environmentPath(): string + { + return is_file($envPath = $this->files->closest($this->basePath(), '.env')) + ? dirname($envPath) + : $this->basePath(); + } + /** * Use paths that are configurable by the developer. */ diff --git a/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php b/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php index f6f52a81..4843dc72 100644 --- a/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php +++ b/src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php @@ -15,9 +15,7 @@ class KeyGenerateCommand extends FoundationKeyGenerateCommand */ protected function writeNewEnvironmentFileWith($key) { - $envFile = file_exists($this->laravel->environmentFilePath()) - ? $this->laravel->environmentFilePath() - : File::closest($this->laravel->basePath(), '.env'); + $envFile = $this->laravel->environmentFilePath(); if (! $envFile) { $this->error('Unable to set application key. Create a .env file.'); @@ -25,6 +23,21 @@ protected function writeNewEnvironmentFileWith($key) return false; } + if ($this->replace($envFile, $key)) { + return true; + } + + if ($this->prepend($envFile, $key)) { + return true; + } + + $this->error('Unable to set application key.'); + + return false; + } + + protected function replace($envFile, $key): bool + { $replaced = preg_replace( $this->keyReplacementPattern(), 'APP_KEY='.$key, @@ -32,13 +45,14 @@ protected function writeNewEnvironmentFileWith($key) ); if ($replaced === $input || $replaced === null) { - $this->error('Unable to set application key. No APP_KEY variable was found in the .env file.'); - return false; } - file_put_contents($envFile, $replaced); + return file_put_contents($envFile, $replaced) !== false; + } - return true; + protected function prepend($envFile, $key): bool + { + return File::prepend($envFile, 'APP_KEY='.$key.PHP_EOL) !== false; } } diff --git a/src/Roots/Acorn/Console/Kernel.php b/src/Roots/Acorn/Console/Kernel.php index 3bf0a093..f189bd15 100644 --- a/src/Roots/Acorn/Console/Kernel.php +++ b/src/Roots/Acorn/Console/Kernel.php @@ -50,6 +50,7 @@ class Kernel extends FoundationConsoleKernel * @var string[] */ protected $bootstrappers = [ + \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Roots\Acorn\Bootstrap\LoadConfiguration::class, \Roots\Acorn\Bootstrap\HandleExceptions::class, \Roots\Acorn\Bootstrap\RegisterFacades::class, diff --git a/src/Roots/Acorn/Http/Kernel.php b/src/Roots/Acorn/Http/Kernel.php index 983962de..3c815aa8 100644 --- a/src/Roots/Acorn/Http/Kernel.php +++ b/src/Roots/Acorn/Http/Kernel.php @@ -12,6 +12,7 @@ class Kernel extends HttpKernel * @var string[] */ protected $bootstrappers = [ + \Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class, \Roots\Acorn\Bootstrap\LoadConfiguration::class, \Roots\Acorn\Bootstrap\HandleExceptions::class, \Roots\Acorn\Bootstrap\RegisterFacades::class,