Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 enable LoadEnvironmentVariables #328

Merged
merged 4 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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. No APP_KEY variable was found in the .env file.');
QWp6t marked this conversation as resolved.
Show resolved Hide resolved

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
Loading