Immutability and Repository Customization
masterImmutability determines whether Dotenv is allowed to overwrite existing environment variables.
- Immutable (Default): Use
createImmutable(). It will not overwrite variables that are already set in the environment. - Mutable: Use
createMutable()if you want Dotenv to overwrite existing environment variables.
For advanced configurations, you can use the RepositoryBuilder to define specific adapters (like EnvConstAdapter or PutenvAdapter) and set immutability or an allow-list of variables.
// Example: Custom repository with specific adapters and immutability
$repository = Dotenv\Repository\RepositoryBuilder::createWithNoAdapters()
->addAdapter(Dotenv\Repository\Adapter\EnvConstAdapter::class)
->addWriter(Dotenv\Repository\Adapter\PutenvAdapter::class)
->immutable()
->make();
$dotenv = Dotenv\Dotenv::create($repository, __DIR__);
$dotenv->load();
// Example: Using an allow-list
$repository = Dotenv\Repository\RepositoryBuilder::createWithDefaultAdapters()
->allowList(['FOO', 'BAR'])
->make();
$dotenv = Dotenv\Dotenv::create($repository, __DIR__);
$dotenv->load();