Skip to content

Commit

Permalink
🔧 enable LoadEnvironmentVariables (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
QWp6t authored Jan 6, 2024
1 parent ab517ba commit 56cb650
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/Roots/Acorn/Bootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
*/
Expand Down
28 changes: 21 additions & 7 deletions src/Roots/Acorn/Console/Commands/KeyGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,44 @@ 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.');

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,
$input = file_get_contents($envFile)
);

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;
}
}
1 change: 1 addition & 0 deletions src/Roots/Acorn/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Roots/Acorn/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 56cb650

Please sign in to comment.