php-standard-library/env
Tiny PHP utility for reading environment variables with sensible defaults and type casting. Helps centralize access to config via env(), supports required keys, fallback values, and safe handling when variables are missing or empty.
env() but fills gaps in type safety and consistency across non-web layers (e.g., queues, CLI tools). Ideal for multi-repo ecosystems where Laravel’s helpers aren’t available (e.g., shared libraries, microservices).Illuminate\Support\Facades\Env via a facade wrapper..env loading)..env loading (e.g., vlucas/phpdotenv) if not using Laravel’s loader.envBool() may accept "yes" or "1" as true, leading to subtle bugs. Requires custom validation rules or strict parsing.config() (cached) to env() (uncached) for web layers.getenv() replacements may break hardcoded logic (e.g., "true" vs. true).envString('KEY', 'value', true) for tests).env() for consistency, or remain separate?phpdotenv entirely, or coexist with it?"false" for booleans)?null defaults, empty strings) be handled?getenv()/$_ENV usage?config() isn’t needed.// Instead of:
$debug = env('APP_DEBUG', false);
// Use:
$debug = envBool('APP_DEBUG', false); // Type-safe boolean
getenv() with type safety and defaults.// In a queue job:
$timeout = envInt('JOB_TIMEOUT', 60); // Always an integer
// Shared library (no Laravel):
$apiKey = envString('API_KEY');
# GitHub Actions:
env:
APP_ENV: production
// In a deploy script:
$env = envString('APP_ENV', 'development');
| Phase | Action | Tools/Examples |
|---|---|---|
| Audit | Identify getenv()/$_ENV usage in non-web layers (queues, CLI, jobs). |
`git grep -E 'getenv |
| Pilot | Replace getenv() in a single module (e.g., a queue worker). |
envInt('QUEUE_TIMEOUT', 30) |
| Facade Wrapper | Create a Laravel facade to bridge env() and the package’s API. |
Env::string('APP_DEBUG') → envString('APP_DEBUG') |
| Type Migration | Update type hints and defaults in services/controllers. | public function __construct(public bool $debug) |
| Deprecation | Add linter rules (PHPStan) to flag getenv() usage. |
rules/phpstan.php |
| Testing | Update test suites to use the package’s mocking utilities. | envString('TEST_MODE', true, true) (override) |
config() and env(); no framework modifications..env loading (e.g., vlucas/phpdotenv).envInt() won’t reject "abc").getenv() in Artisan commands, queues, and scheduled tasks (low-risk, high-impact).config() for performance-critical paths).getenv() via linters (e.g., PHPStan rules) and IDE warnings.getenv() calls with defaults and type safety.SNAKE_CASE conventions across teams.composer.json, requiring version updates.env() that require package updates.envInt() fails fast on invalid input).getenv() call if not wrapped properly.env* syntax; minimal learning curve.config() caching.env() vs. envString().getenv(); no significant impact on memory/CPU.config(), this package doesn’t cache by default (avoids stale data but requires manual caching in high-frequency loops).How can I help you explore Laravel packages today?