composer require envless/env installs one public namespace, Envless\Env, and the PHP SDK, envless/sdk, alongside. It needs PHP 8.2 or newer with the curl, json and openssl extensions.
Envless\Env\Env
| Method | Signature |
|---|---|
load | (bool $revalidate = false, ?HttpClient $httpClient = null): array<string, string>. Loads once per process and returns every value |
get | (string $name): string|int|float|bool, coerced to the declared type. Throws MissingVariableException |
optional | (string $name): string|int|float|bool|null, null for a name that is not set |
has | (string $name): bool. Never throws, not even before a load |
string | (string $name): string |
number | (string $name): int|float |
boolean | (string $name): bool |
server | (): Values, every variable |
client | (): ClientValues, the client variables only |
override | (array<string, string> $values): Override |
fetch | (?string $token = null, ?string $project = null, ?string $environment = null, ?string $product = null, ?string $passphrase = null, ?string $key = null, ?string $workspaceId = null, ?string $endpoint = null, ?HttpClient $httpClient = null): array<string, string> |
isConfigured | (): bool, true when a machine key is available or envless run injected the values |
VERSION | The package version |
HttpClient is Envless\Http\HttpClient from envless/sdk. Pass one to route the requests through Guzzle or a test double, or leave it out for the bundled cURL client.
Envless\Env\Values and Envless\Env\ClientValues
| Member | Behaviour |
|---|---|
get(), optional(), has(), string(), number(), boolean() | As on Env, through this view |
names() | list<string>, the names this view can read |
$view->NAME, $view['NAME'] | get() |
isset($view->NAME), isset($view['NAME']) | has() |
foreach ($view as $name => $value) | Every readable variable, coerced |
count($view) | The number of readable variables |
| Assigning or unsetting a name | ReadOnlyException |
serialize() | SerialisationException |
json_encode(Env::server()) | SerialisationException |
json_encode(Env::client()), Env::client()->snapshot() | array<string, string|int|float|bool> of the client variables |
Values reads every variable. ClientValues reads only the variables whose visibility is client and throws ServerOnlyException for the rest.
Envless\Env\Override
| Method | Behaviour |
|---|---|
restore() | Removes this layer. Calling it twice does nothing |
isRestored() | Whether restore() has run |
vendor/envless/env/register.php
Includes Composer's autoloader and calls Env::load(). Use it as PHP's auto_prepend_file, see load at boot.
vendor/bin/envless-env types
Writes a class with one typed static method per variable. Flags: --output, --class, --namespace, --project, --product, --env and --check. See typed variables.
Envless\Env\Symfony\EnvVarLoader
Implements Symfony's EnvVarLoaderInterface: loadEnvVars() returns Env::load() when Env::isConfigured() is true and an empty array otherwise. Tag it container.env_var_loader, see Symfony.
Exceptions
Every exception extends Envless\Env\Exception\EnvException, a RuntimeException that implements Envless\Exception\EnvlessException, the interface every envless/sdk exception implements too.
| Exception | Thrown when |
|---|---|
LoadException | A load or a fetch fails. See when a load fails |
NotLoadedException | A read before the first load, with no override in place |
MissingVariableException | A read of a name that is not set. Carries variable |
ServerOnlyException | A read of a server variable through Env::client(). Carries variable |
ReadOnlyException | Assigning or unsetting a name on a view |
SerialisationException | Serialising a view, or JSON encoding Env::server() |
Env::override() with a value that is not a string throws Envless\Exception\InvalidArgumentException from envless/sdk.