diff --git a/CHANGELOG.md b/CHANGELOG.md index 39c8a884..2faf41b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -* Ability to load a Context from a dotenv file +* Add support for loading the context's environment from a dotenv file ## 0.5.1 (2023-06-22) diff --git a/doc/05-context.md b/doc/05-context.md index 38766769..f891d8dc 100644 --- a/doc/05-context.md +++ b/doc/05-context.md @@ -118,3 +118,24 @@ function foo(Context $context): void run('pwd'); // will print /tmp } ``` + +## Load a context from dotenv file + +You can also load your context from a dotenv file: + +```php +use Castor\Attribute\AsContext; +use Castor\Context; + +#[AsContext] +function my_context(): Context +{ + return new Context::fromDotEnv(); +} +``` + +By default, it loads the `.env` file on your project root (where castor file or +folder was found), but you can overload this by passing your dotenv file +path as an argument. +You can find more about how dotenv file loading and overloading works on +[related Symfony documentation](https://symfony.com/doc/current/configuration.html#configuring-environment-variables-in-env-files).