FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

unikoca/simpleEnvLoader: just another simple env file loader... · GitHub

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnvLoader

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


How It Works

Loading happens in two layers:

  1. .env — base/shared config, always loaded first (required)
  2. dev.env — loaded on top when on localhost prod.env — loaded on top when on a remote server

Values in the overlay always win over the base .env.


Mode Detection

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.


File Structure

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.


Installation

No Composer required. Just drop the file in and require it:

require_once __DIR__ . '/src/EnvLoader.php';

Usage

Basic

$loader = new \uniko\src\EnvLoader(__DIR__);
$result = $loader->load();

// $result['mode'] → 'dev' or 'prod'
// $result['env']  → $_ENV array (merged)

Checking the Mode

$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

Accessing Values

// Via $_ENV
$host = $_ENV['DB_HOST'];

// Via getenv()
$host = getenv('DB_HOST');

// Via constant (key uppercased)
echo DB_HOST;

Cleaning Up Superglobals

Pass true to remove all loaded keys from $_ENV and $_SERVER after constants are defined:

$result = $loader->load(unsetENV: true);

Value Replacement

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'

.env Parser

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

Auto-Defined Constants

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.


API Reference

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())

Error Handling

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());
}

Requirements

  • PHP 8.0+
  • No external dependencies

License

Copyright © 2026 uniko.ca - MIT

About

just another simple env file loader...

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages


Back | FazBrowse Home | New Git URL