PHP

PHP runtime reference

Every class and method of envless/env: Env and its readers, the server and client views, overrides, the explicit fetch, register.php, the types command, the Symfony env var loader and every exception.

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

MethodSignature
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
VERSIONThe 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

MemberBehaviour
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 nameReadOnlyException
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

MethodBehaviour
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.

ExceptionThrown when
LoadExceptionA load or a fetch fails. See when a load fails
NotLoadedExceptionA read before the first load, with no override in place
MissingVariableExceptionA read of a name that is not set. Carries variable
ServerOnlyExceptionA read of a server variable through Env::client(). Carries variable
ReadOnlyExceptionAssigning or unsetting a name on a view
SerialisationExceptionSerialising a view, or JSON encoding Env::server()

Env::override() with a value that is not a string throws Envless\Exception\InvalidArgumentException from envless/sdk.