Version: 1.0 Date: 2024-03-01 Applies to: KaririCode\Dotenv 4.4+
KaririCode\Dotenv ships a CLI tool at vendor/bin/kariricode-dotenv (registered in composer.json bin field). It provides 9 commands for managing .env files without writing PHP code.
The CLI is automatically available after composer install/require. No additional setup needed.
vendor/bin/kariricode-dotenv help| Option | Default | Description |
|---|---|---|
--dir=<path> |
Current working directory | Project root directory containing .env files. |
Lists all loaded variables with their detected types, source files, and override status.
vendor/bin/kariricode-dotenv debug [--dir=.]Output format:
Variable Source Type Overridden Value
──────────────────────────────────────────────────────────
DB_HOST .env.local String yes localhost
DB_PORT .env Integer no 5432
APP_DEBUG .env Boolean no true
APP_ENV .env.staging String yes staging
Process: Instantiates Dotenv, calls bootEnv(), then formats debug() output as a table.
Validates the current .env configuration against a .env.schema file.
vendor/bin/kariricode-dotenv validate [--dir=.] [--schema=.env.schema]| Option | Default | Description |
|---|---|---|
--schema=<path> |
.env.schema (relative to --dir) |
Path to the schema file. |
Exit codes:
0— All validations passed.1— One or more validation failures. Errors printed to STDERR.
Output on failure:
✗ Environment validation failed:
- DB_HOST is required but not defined.
- DB_PORT must be an integer.
- APP_ENV must be one of: local, staging, production.
Encrypts all plaintext values in a .env file using AES-256-GCM.
vendor/bin/kariricode-dotenv encrypt <file> --key=<hex64> [--output=<path>]| Option | Required | Description |
|---|---|---|
<file> |
Yes | Path to the .env file to encrypt. |
--key=<hex64> |
Yes | 64-character hex encryption key. |
--output=<path> |
No | Output path. If omitted, overwrites the input file. |
Behavior:
- Parses the input file line by line.
- For each
KEY=VALUEline where the value does not already start withencrypted:, encrypts the value. - Preserves comments, empty lines, and formatting.
- Already-encrypted values are left unchanged (idempotent).
Output:
Encrypted 5 values in .env
DB_PASSWORD: encrypted
API_SECRET: encrypted
SMTP_PASSWORD: encrypted
JWT_KEY: encrypted
REDIS_PASSWORD: encrypted
Skipped 8 plaintext values (not encrypted)
Skipped 2 already-encrypted values
Decrypts all encrypted values in a .env file, producing a plaintext version.
vendor/bin/kariricode-dotenv decrypt <file> --key=<hex64> [--output=<path>]Same options and behavior as encrypt, but in reverse. Plaintext values pass through unchanged.
Generates an OPcache-friendly PHP cache file from the current .env configuration.
vendor/bin/kariricode-dotenv cache:dump [--dir=.] [--output=.env.cache.php]| Option | Default | Description |
|---|---|---|
--output=<path> |
.env.cache.php (relative to --dir) |
Cache file path. |
Process: Loads .env via bootEnv(), then calls dumpCache().
Removes the PHP cache file.
vendor/bin/kariricode-dotenv cache:clear [--dir=.] [--file=.env.cache.php]Compares two .env files, showing added, removed, and changed variables.
vendor/bin/kariricode-dotenv diff <file1> <file2>Output:
Comparing .env vs .env.production:
+ REDIS_HOST=redis.internal (added in .env.production)
- DEV_TOOLBAR=true (removed in .env.production)
~ DB_HOST: localhost → prod-db.internal
~ APP_DEBUG: true → false
Summary: 2 changed, 1 added, 1 removed, 8 unchanged
Generates a .env.example file from an existing .env by stripping all values.
vendor/bin/kariricode-dotenv example:generate [--dir=.] [--output=.env.example]Output file:
# Auto-generated from .env — do not edit manually.
# Copy to .env and fill in the values.
DB_HOST=
DB_PORT=
DB_NAME=
DB_USER=
DB_PASSWORD=
APP_ENV=
APP_DEBUG=
APP_URL=Comments from the source file are preserved. Values are stripped to empty strings.
Generates a new encryption key pair.
vendor/bin/kariricode-dotenv keygenOutput:
KaririCode\Dotenv — Key Generation
Private key: a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2
Public ID: f7e8d9c0
Store the private key in DOTENV_PRIVATE_KEY environment variable.
Never commit the private key to version control.
Usage:
export DOTENV_PRIVATE_KEY=a1b2c3d4...
vendor/bin/kariricode-dotenv encrypt .env --key=a1b2c3d4...
| Code | Meaning |
|---|---|
0 |
Success |
1 |
Validation failure or expected error (missing file, wrong key) |
2 |
Usage error (unknown command, missing required option) |
All errors are written to STDERR. Normal output goes to STDOUT. This allows piping:
vendor/bin/kariricode-dotenv debug 2>/dev/null | grep DB_The CLI resolves vendor/autoload.php by checking two paths:
__DIR__ . '/../vendor/autoload.php'— when installed as a root dependency.__DIR__ . '/../../../autoload.php'— when installed as a dependency of another package.
If neither path exists, the CLI prints an error to STDERR and exits with code 1.
Options are parsed from $argv with a simple --key=value convention. No external option parser is used (zero dependencies). Boolean flags use --flag (no value).
function parseOptions(array $args): array
{
$options = [];
foreach ($args as $arg) {
if (str_starts_with($arg, '--')) {
$eqPos = strpos($arg, '=');
if ($eqPos !== false) {
$options[substr($arg, 2, $eqPos - 2)] = substr($arg, $eqPos + 1);
} else {
$options[substr($arg, 2)] = true;
}
}
}
return $options;
}