Skip to content

[12.x] feat: add extending env to configure step #55933

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

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 17 additions & 1 deletion src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,38 @@ public function __construct($basePath = null)
* Begin configuring a new Laravel application instance.
*
* @param string|null $basePath
* @param array $environments
* @return \Illuminate\Foundation\Configuration\ApplicationBuilder
*/
public static function configure(?string $basePath = null)
public static function configure(?string $basePath = null, array $environments = [])
{
$basePath = match (true) {
is_string($basePath) => $basePath,
default => static::inferBasePath(),
};

static::extendEnvironment($environments);

return (new Configuration\ApplicationBuilder(new static($basePath)))
->withKernels()
->withEvents()
->withCommands()
->withProviders();
}

/**
* Extend environment with custom adapters.
*
* @param array $environments
* @return void
*/
protected static function extendEnvironment(array $environments)
{
foreach ($environments as $name => $callback) {
Env::extend($callback, is_string($name) ? $name : null);
}
}

/**
* Infer the application's base directory from the environment.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Foundation/FoundationApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Foundation;

use Dotenv\Repository\Adapter\ArrayAdapter;
use Illuminate\Config\Repository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Foundation\Application;
Expand All @@ -18,6 +19,21 @@ class FoundationApplicationTest extends TestCase
{
protected function tearDown(): void
{
// Clean up environment variables that might be set by tests
unset($_ENV['FOO'], $_SERVER['FOO']);
putenv('FOO');

// Reset the global Env state to prevent test pollution
$reflection = new \ReflectionClass(\Illuminate\Support\Env::class);

$customAdaptersProperty = $reflection->getProperty('customAdapters');
$customAdaptersProperty->setAccessible(true);
$customAdaptersProperty->setValue(null, []);

$repositoryProperty = $reflection->getProperty('repository');
$repositoryProperty->setAccessible(true);
$repositoryProperty->setValue(null, null);

m::close();
}

Expand Down Expand Up @@ -271,6 +287,30 @@ public function testMethodAfterLoadingEnvironmentAddsClosure()
$this->assertArrayHasKey(0, $app['events']->getListeners('bootstrapped: Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables'));
}

public function testExtendingEnvironmentWithCustomAdapter()
{
$adapter = ArrayAdapter::create()->get();

$adapter->write('FOO', 'BAR');

$app = Application::configure(
environments: [
fn () => $adapter,
]
)->create();
$closure = function () {
//
};
$app->afterLoadingEnvironment($closure);
$app->useConfigPath(__DIR__.'/fixtures/config');
$app->bootstrapWith([
\Illuminate\Foundation\Bootstrap\LoadEnvironmentVariables::class,
\Illuminate\Foundation\Bootstrap\LoadConfiguration::class,
]);
$this->assertSame('BAR', $app->make('config')->get('app.bar'));
$this->assertNotSame('foo', $app->make('config')->get('app.bar'));
}

public function testBeforeBootstrappingAddsClosure()
{
$app = new Application;
Expand Down
1 change: 1 addition & 0 deletions tests/Foundation/fixtures/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

return [
'foo' => 'bar',
'bar' => env('FOO', 'foo'),
];