Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from mallgroup/feature-array
Browse files Browse the repository at this point in the history
Added array feature into adapter
  • Loading branch information
bckp authored Oct 26, 2021
2 parents f384a6c + aeddd23 commit 0a1f9b7
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 269 deletions.
52 changes: 51 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,54 @@ $ composer require mallgroup/environment-adapter
This package is currently maintaining by these authors.

<a href="https://github.com/bckp"><img width="80" height="80" src="https://avatars.githubusercontent.com/u/179652?v=4&s=80"></a>
<a href="https://github.com/mallgroup"><img width="80" height="80" src="https://avatars.githubusercontent.com/u/23184995?v=4&s=80"></a>
<a href="https://github.com/mallgroup"><img width="80" height="80" src="https://avatars.githubusercontent.com/u/23184995?v=4&s=80"></a>

## How to use

For use this adapter, you need to use Mallgroup/Configurator instead of Nette one. It will autoregister ENV extension support. After that, you can simply link `some-name.env` file and nette will inject env variables into %env%.
The expected syntax is
name_of_env_variable: ::{string|int|float|bool}(default: {string}, hidden: {true|false})
name_of_array_variable: ::array(separator: {string}, hidden: {true|false}, cast: {int|float|bool|string})

first entry is name of ENV variable, this will add `%env.name_of_env_variable%` to the parameters and get value using `getenv('NAME_OF_ENV_VARIABLE');`
next is `::` that will tell adapter, we are working with entity, this is just shortcut to force nette get arguments using internal mechanism.
after that, we have `cast` part, this tells adapter, what type of variable he should cast to, usefull as env know only strings, with this, you can have INTs, FLOATs, BOOLs and even ARRAY of INT, FLOAT, STRING, BOOL.

attributes inside
```text
default: that is fallback, if no value is found using getenv.
hidden: if variable must remain secret, or we can burn it into generated container file (if we have password, we do not want to have it stored in PHP file, but kept it in ENV only)
cast: only for Array, cast values to specific type
separator: only for Array, used for explode
```

if you keep order of arguments same as Environment class expect, you can omit their names.

### Example file
```text
service_user: ::string(secret_user)
service_password: ::string(secret_password, true)
service_port: ::int(1234)
service_nonstring: ::nonstring(1234)
service_active: ::bool(\'false\')
service_array: ::array(cast: int)
```
with .env
```text
SERVICE_USER=someuser
SERVICE_PASSWORD=supersecret
SERVICE_ARRAY=1|2|3|4
```

will be translated to if none ENV

```php
[
'service_user' => 'someuser',
'service_password' => Mallgroup\Environment::string('SERVICE_PASSWORD', 'secret_password'),
'service_port' => 1234,
'service_nonstring' => '1234',
'service_active' => false # notice string false is translated to the boolean correctly
'service_array' => [1,2,3,4],
]
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"nette/di": "^3.0",
"nette/utils": "^3.0",
"nette/neon": "^3.0",
"mallgroup/environment": "^1.0"
"mallgroup/environment": "^1.3"
}
}
Loading

0 comments on commit 0a1f9b7

Please sign in to comment.