| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Zero-dependency PHP class that loads .env files in a layered, mode-aware fashion.
Auto-detects dev vs prod from the hostname — no manual configuration needed.
Namespace: uniko · PHP: 8.0+ · Version: 1.0.0
Loading happens in two layers:
Values in the overlay always win over the base .env.
The mode is never set manually — it is determined automatically at construction time from $_SERVER['HTTP_HOST']:
| Hostname | Mode | Overlay |
|---|---|---|
| localhost | dev | dev.env |
| 127.0.0.1 / ::1 | dev | dev.env |
| *.local / *.test / *.dev | dev | dev.env |
| Any other hostname | prod | prod.env |
| CLI (no HTTP_HOST) | dev* | dev.env |
* In CLI context (cron, migrations), falls back to getenv('APP_MODE'), defaulting to dev if not set.
project/
├── .env ← shared base values
├── dev.env ← dev overrides ← add to .gitignore
├── prod.env ← prod overrides ← add to .gitignore
└── src/
└── EnvLoader.php
⚠️ Never commit dev.env or prod.env — they typically contain credentials.
No Composer required. Just drop the file in and require it:
require_once __DIR__ . '/src/EnvLoader.php';$loader = new \uniko\src\EnvLoader(__DIR__);
$result = $loader->load();
// $result['mode'] → 'dev' or 'prod'
// $result['env'] → $_ENV array (merged)$loader->isDev(); // true on localhost
$loader->isProd(); // true on remote server
$loader->getMode(); // 'dev' | 'prod'Global constants are also defined automatically after load():
MODE // 'DEV' or 'PROD' (uppercased string)
ISDEV // true | false
ISPROD // true | false// Via $_ENV
$host = $_ENV['DB_HOST'];
// Via getenv()
$host = getenv('DB_HOST');
// Via constant (key uppercased)
echo DB_HOST;Pass true to remove all loaded keys from $_ENV and $_SERVER after constants are defined:
$result = $loader->load(unsetENV: true);Use setReplace() to inject runtime values into .env placeholders before constants are defined:
# .env
APP_URL=https://{{DOMAIN}}/app$loader->setReplace(['{{DOMAIN}}' => 'example.com'])
->load();
// APP_URL → 'https://example.com/app'No external library. Supports:
| Syntax | Behaviour |
|---|---|
| KEY=value | Plain value |
| KEY="hello world" | Double-quoted — quotes stripped |
| KEY='hello world' | Single-quoted — quotes stripped |
| KEY=value # comment | Inline comment stripped (outside quotes) |
| export KEY=value | export prefix ignored |
| # comment | Line skipped |
| blank line | Skipped |
| KEY= | Stored as empty string |
After load(), constants are defined globally from the merged environment:
| Constant | Value |
|---|---|
| MODE | 'dev' or 'prod' |
| ISDEV | true or false |
| ISPROD | true or false |
| <KEY> for each env var | String value (key uppercased) |
Constants are only defined if not already defined — pre-existing constants are never overwritten.
| Method | Description |
|---|---|
| __construct(string $path) | Sets config directory, auto-detects mode |
| load(bool $unsetENV = false): array | Loads .env + overlay, defines constants, returns [mode, env] |
| getMode(): string | Returns 'dev' or 'prod' |
| isDev(): bool | true when mode is dev |
| isProd(): bool | true when mode is prod |
| setReplace(array $replace): static | Sets placeholder → value map, chainable |
| replaceENVValues(): array|string | Applies replacements on $_ENV (called internally by load()) |
If the base .env is missing, a RuntimeException is thrown.
Overlay files (dev.env / prod.env) are optional and silently skipped if absent.
try {
$result = $loader->load();
} catch (\RuntimeException $e) {
error_log($e->getMessage());
}Copyright © 2026 uniko.ca - MIT
| Back | FazBrowse Home | New Git URL |