Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions v3.0/packages/config/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ public function getInt(string $key, ?int $default = null): int;
- `getInt()` returns the number for a whole number or whole-number text such as `587`, and throws `ConfigException::invalidValue()` for any other value, such as `abc` or `1.5`
- `get()` is not affected and keeps returning the raw value

The `.env` readers and the config readers differ on purpose. A `.env` file is outside input and often incomplete, so `env_bool()` and `env_int()` are forgiving: a missing or empty value returns the default, or `false` and `0`. Config files belong to the application, so `config()->getBool()` and `config()->getInt()` without a default state a required key, and throw `ConfigException::missingKey()` when it is not set. See the [`.env` readers](../environment/contracts.md#typed-reader-contract).

`Config` and `ConfigSection` both have these methods. `ConfigInterface` does not declare them, so code that depends on the interface alone cannot call them.

## Section contract
Expand Down
15 changes: 8 additions & 7 deletions v3.0/packages/environment/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Environment::load()

## How `Environment` is organised

`Environment` implements `EnvironmentInterface` and keeps only its state and `load()`. The rest comes from three traits in `Quantum\Environment\Traits`:
`Environment` implements `EnvironmentInterface` and loads the `.env` file itself: it holds the state, `setMutable()`, `load()` and the private methods that pick and parse the file. Reading and writing each have their own trait in `Quantum\Environment\Traits`:

- `AppEnvTrait`: `getAppEnv()` and the `is...()` checks
- `ValueTrait`: the readers `getValue()`, `getBool()`, `getInt()`, `hasKey()` and `getRow()`
- `EnvFileTrait`: `updateRow()` and the file lookup and parsing
- `ReadTrait`: the readers `getValue()`, `getBool()`, `getInt()`, `hasKey()` and `getRow()`
- `WriteTrait`: `updateRow()`. It needs the path and name of the file, so it declares `getEnvFilePath()` and `getEnvFileName()` abstract, and `Environment` provides them

## `Environment` lifecycle

Expand Down Expand Up @@ -64,12 +64,13 @@ There is no validation that `app_env` matches one of the `Env` constants.
When the key already exists, the method:

1. reads the whole environment file
2. replaces the exact `KEY=currentValue` line with `KEY=newValue`
3. writes the full file back
2. finds the key and its current value as the file writes them: `KEY="value"` (also over several lines), `KEY='value'`, `KEY=value` up to a space or `#`, or `KEY` and `KEY=` without a value
3. replaces that part with `KEY=newValue`, so a comment after the value stays
4. writes the full file back

When the key does not exist, it appends a new `KEY=value` line.
When the key does not exist, it appends a new `KEY=value` line. In both cases the new value is written as it is given, without quotes. A `$` or `\` in it stays as typed, but a space makes the file fail to load on the next request, and text after a `#` is read as a comment, so pass such values only after checking them.

Because the replacement pattern is built from the current cached value, the method is designed for values that still match the file content exactly.
The search starts from the value as the `.env` parser read it. When the file writes that value in another form, the line is not found, the file stays unchanged and only the cached value changes. That happens for `export KEY=value`, a space after `=`, an escape such as `\"` or `\n` inside double quotes, and `${VAR}`. If the replacement itself fails, `updateRow()` throws `EnvException::fileNotFound()` and does not write the file.

## `Server` lifecycle

Expand Down
12 changes: 8 additions & 4 deletions v3.0/packages/environment/contracts.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ public function getBool(string $key, bool $default = false): bool;
public function getInt(string $key, int $default = 0): int;
```

- a missing key returns the default
- `getBool()` returns `true` for `true`, `1`, `on` and `yes`, ignoring letter case, and `false` for every other value, including `false`, `0`, `off`, `no` and an empty value
- `getInt()` returns the number for whole-number text such as `587`, and throws `EnvException::invalidValue()` for any other text, such as `abc`, `1.5` or an empty value
- a key that is missing, empty (`KEY=`) or written without a value (`KEY` with no `=`) is not set and returns the default
- `getBool()` returns `true` for `true`, `1`, `on` and `yes`, ignoring letter case, and `false` for every other value, including `false`, `0`, `off` and `no`
- `getInt()` returns the number for whole-number text such as `587`, and throws `EnvException::invalidValue()` for any other text, such as `abc` or `1.5`
- `getValue()` and `env()` are not affected and keep returning the raw value

The `.env` readers and the config readers differ on purpose. A `.env` file is outside input and often incomplete, so `env_bool()` and `env_int()` are forgiving: a missing or empty value returns the default, or `false` and `0`. Config files belong to the application, so `config()->getBool()` and `config()->getInt()` without a default state a required key, and throw `ConfigException::missingKey()` when it is not set. See the [config readers](../config/contracts.md#typed-reader-contract).

## One-shot load contract

`Environment::load()` returns immediately when `$loaded` is already `true`.
Expand All @@ -57,6 +59,8 @@ If mutability is off, the method throws `EnvException::environmentImmutable()`.

If loading has not happened yet, it throws `EnvException::environmentNotLoaded()`.

For a key that exists, `updateRow()` replaces only the key and its value, keeps a comment after the value, and writes the new value as it is given. A line it cannot find in the form the value was read is left unchanged; the forms it finds are listed under [Mutation model](architecture.md#mutation-model). It never writes an empty file: when the replacement fails, it throws `EnvException::fileNotFound()`.

## Environment-name contract

The environment predicate helpers are exact string comparisons:
Expand Down Expand Up @@ -129,4 +133,4 @@ The package defines three local exception factories in `EnvException`:
- `environmentNotLoaded()`
- `invalidValue()`, raised by `getInt()` and `env_int()` for text that is not a whole number

It also calls `fileNotFound(...)`, inherited from the shared base exception layer, when the selected `.env` file is absent.
It also calls `fileNotFound(...)`, inherited from the shared base exception layer, when the selected `.env` file is absent, and from `updateRow()` when the file cannot be read or the replacement fails.
4 changes: 2 additions & 2 deletions v3.0/packages/environment/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Behavior:
1. resolves the shared `Environment` instance with `environment()`
2. returns `getBool($var, $default)`

Use it for flags such as `DEBUG`, where the raw text `'false'` would otherwise count as true. See [Reading booleans and numbers](usage.md#reading-booleans-and-numbers).
Use it for flags such as `DEBUG`, where the raw text `'false'` would otherwise count as true. A missing or empty value returns the default. See [Reading booleans and numbers](usage.md#reading-booleans-and-numbers).

## `env_int()`

Expand All @@ -53,7 +53,7 @@ Behavior:
1. resolves the shared `Environment` instance with `environment()`
2. returns `getInt($var, $default)`

It throws `EnvException::invalidValue()` when the value is not a whole number. See [Reading booleans and numbers](usage.md#reading-booleans-and-numbers).
A missing or empty value returns the default. It throws `EnvException::invalidValue()` when the value is not a whole number. See [Reading booleans and numbers](usage.md#reading-booleans-and-numbers).

## `server()`

Expand Down
2 changes: 1 addition & 1 deletion v3.0/packages/environment/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The package is built from these parts:

- `Quantum\Environment\Environment` selects and loads one `.env` file
- `Quantum\Environment\Contracts\EnvironmentInterface` declares the public methods of `Environment`
- `Quantum\Environment\Traits` holds the code `Environment` is built from: `AppEnvTrait`, `ValueTrait` and `EnvFileTrait`
- `Quantum\Environment\Traits` holds the code `Environment` is built from: `AppEnvTrait`, `ReadTrait` and `WriteTrait`
- `Quantum\Environment\Server` wraps a snapshot of `$_SERVER`
- `Quantum\Environment\Helpers\env.php` exposes `environment()`, `env()`, `env_bool()` and `env_int()`
- `Quantum\Environment\Helpers\server.php` exposes `server()`, `get_user_ip()`, and a `getallheaders()` fallback
Expand Down
5 changes: 4 additions & 1 deletion v3.0/packages/environment/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ $debug = env_bool('DEBUG');
$port = env_int('MAIL_PORT', 25);
```

`env_bool()` returns `true` for `true`, `1`, `on` and `yes`, in any letter case, and `false` for everything else, including an empty value. `env_int()` returns the number, and throws an `EnvException` when the text is not a whole number, so a typo in `.env` shows up at once instead of silently becoming `0`. When the key is missing, both return the default: `false` and `0` unless you pass another. `env()` keeps returning the raw text.
`env_bool()` returns `true` for `true`, `1`, `on` and `yes`, in any letter case, and `false` for any other text. `env_int()` returns the number, and throws an `EnvException` when the text is not a whole number, so a typo in `.env` shows up at once instead of silently becoming `0`.

A key that is missing or empty counts as not set, and both return the default: `false` and `0` unless you pass another. A `.env` file often lists keys with no value yet, such as `TWO_FA=`, so with `env_bool('TWO_FA', true)` that line keeps two-factor authentication on. Write `TWO_FA=false` to turn it off. `env()` keeps returning the raw text, the empty string included.

The same readers are methods of the shared instance, `environment()->getBool('DEBUG')` and `environment()->getInt('MAIL_PORT')`. Like `env()`, they work only after the environment has been loaded. They are available since 3.1.2; the exact rules are on the [contracts page](contracts.md#typed-reader-contract).

Expand Down Expand Up @@ -49,6 +51,7 @@ Use this carefully:
- mutability is off by default
- the target file must already have been selected and loaded
- updates modify the real `.env` file under `App::getBaseDir()`
- an existing line is found in the forms listed under [Mutation model](architecture.md#mutation-model), and a comment after the value stays

## Reading request metadata

Expand Down
25 changes: 23 additions & 2 deletions v3.0/upgrade-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,25 @@ Ports, TTLs and lifetimes are read as whole numbers, so a typo such as `abc` now
- A log adapter that uses `LoggerTrait` declares `initialize(ConfigSection $config)`.
- An auth adapter that uses `AuthTrait` implements `readConfig(ConfigSection $config)`, see [When the settings are read](packages/auth/adapters.md#when-the-settings-are-read).

### 11. Check the upgrade
### 11. Empty `.env` values and the Environment traits

**An empty `.env` value gives the default.** `env_bool()` and `env_int()`, and `environment()->getBool()` and `getInt()`, treat `KEY=` and a `KEY` line without `=` as not set, the same as a missing key. `env_bool()` gave `false` and `env_int()` threw an `EnvException`. So a setting whose default is on, and whose line is empty, is now on. The auth config of the `DemoWeb` and `DemoApi` module templates reads `env_bool('TWO_FA', true)`, so an empty `TWO_FA=` leaves two-factor authentication on. Write the value you mean:

```bash
# before: turned two-factor authentication off
TWO_FA=

# after
TWO_FA=false
```

`env()` still returns the raw value. The config readers stay strict on purpose, see the [`.env` readers](packages/environment/contracts.md#typed-reader-contract).

**`updateRow()` finds the line as the file writes it,** quoted or without a value, and keeps a comment after the value, so `php qt core:key` now also replaces a quoted `APP_KEY="..."`. The forms it finds are listed under [Mutation model](packages/environment/architecture.md#mutation-model).

**Code of your own that uses the Environment traits:** `Quantum\Environment\Traits\ValueTrait` is renamed `ReadTrait`, `EnvFileTrait` is removed, and `updateRow()` is in the new `WriteTrait`. A class that uses them declares what they use, as `Environment` does: the properties `$loaded`, `$envContent`, `$isMutable` and `$appEnv`, and the methods `getEnvFilePath()` and `getEnvFileName()`.

### 12. Check the upgrade

Update the dependencies and run the checks you already use:

Expand All @@ -173,11 +191,14 @@ composer phpstan
Then look for the names this part replaced. Each hit is a section above:

```bash
grep -rnF -e 'Generator::scan(' -e '@dataProvider' -e 'verbose=' -e 'setHierarchy(' -e 'getHierarchy(' -e 'Config\Traits\ValueTrait' -e 'ConfigFileTrait' -e '->all()->' shared modules helpers tests phpunit.xml
grep -rnF -e 'Generator::scan(' -e '@dataProvider' -e 'verbose=' -e 'setHierarchy(' -e 'getHierarchy(' -e 'Config\Traits\ValueTrait' -e 'ConfigFileTrait' -e '->all()->' -e 'Environment\Traits\ValueTrait' -e 'EnvFileTrait' shared modules helpers tests phpunit.xml
grep -rnE "new Setup\([^,]+,[^,]+, *(true|false)" shared modules helpers tests
grep -rnE -e "->(getBool|getInt)\('[^']+'\)" shared modules helpers tests
grep -nE '^[A-Z0-9_]+= *$' .env
```

The last command lists the empty values in `.env`. A hit matters only when the key is read with `env_bool()` or `env_int()` and a default.

Finally, open a page with `app.debug` on to check the toolbar, sign in once if you use JWT, and run one `php qt` command.

## Upgrading from 3.0 to 3.1
Expand Down
Loading