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 system, but offers a more structured API for accessing variables. It could complement Laravel’s config()/env() helpers without redundancy, especially in monolithic apps or microservices where consistency across non-Laravel components (e.g., CLI scripts, queues) is needed.env() lacks type coercion (e.g., bool, int), forcing manual validation. This package’s typed helpers (envBool(), envInt()) reduce runtime errors and improve IDE support (e.g., PHPStan, Psalm), making it ideal for projects adopting stricter type systems..env files are project-specific. This package could standardize variable access across a multi-repo ecosystem (e.g., shared libraries, services) where .env files aren’t feasible, by enforcing a single source of truth (e.g., Docker/K8s secrets, CI/CD variables).getenv()/$_ENV in non-Laravel contexts (e.g., Artisan commands, jobs, console apps). For Laravel-specific code, it can coexist with config() or env() via a facade wrapper.env() already handles defaults and casting (via phpdotenv). This package’s value lies in consistency across non-Laravel code or projects where Laravel’s helpers aren’t available (e.g., legacy PHP scripts).envBool()) may silently fail on invalid strings (e.g., "yes" vs. "true"). Requires explicit validation rules or custom parsers.env() calls in Laravel is already optimized. This package’s overhead is negligible for most use cases but could matter in high-frequency CLI tools (e.g., cron jobs).config() may resist switching to a lower-level API, especially if the package lacks Laravel-specific features (e.g., caching, merging).getenv() entirely) or legacy Laravel apps (where it’d supplement env())?"false" for booleans)?null defaults, empty strings) be handled?env() helper for consistency, or remain separate?phpdotenv entirely, or coexist with it?env() is unavailable or overkill. For web requests, Laravel’s config() caching remains superior..env files aren’t practical. Example:
// Shared library (no Laravel)
$dbHost = envString('DB_HOST', 'localhost');
getenv() with typed access and defaults.| Phase | Action | Tools/Examples |
|---|---|---|
| Assessment | Audit getenv()/$_ENV usage in non-Laravel code (e.g., Artisan, jobs). |
git grep 'getenv|$_ENV' |
| Pilot | Replace getenv() in a single module (e.g., a queue worker). |
envString('QUEUE_CONNECTION', 'redis') |
| Framework Bridge | Create a facade to wrap Laravel’s env() for consistency: |
Env::string('APP_DEBUG', env('APP_DEBUG')) |
| Full Adoption | Deprecate getenv() in favor of the package’s API. |
IDE refactoring (PHPStorm), ESLint rules. |
| Testing | Update test suites to use the package’s mocking capabilities. | envString('TEST_MODE', true) in PHPUnit. |
config() and env(); no framework modifications needed..env file loading (e.g., vlucas/phpdotenv) if not using Laravel’s loader.envInt() won’t reject "abc").getenv() in Artisan commands, queues, and scheduled tasks (low-risk, high-impact areas).config() for performance-critical paths).getenv() via linters (e.g., PHPStan rules) and IDE warnings.getenv() calls with defaults.SNAKE_CASE (or custom) 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).envString('REGION_DB_HOST'))."false" → true) require explicit validation.getenv() in a real-world Artisan command.How can I help you explore Laravel packages today?