petstack / lite-env
Minimal and lightweight .env parser for PHP with no dependencies.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
pkg:composer/petstack/lite-env
Requires
- php: >=8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.56
- phpunit/phpunit: ^9.5
This package is not auto-updated.
Last update: 2025-12-21 00:33:24 UTC
README
A minimal and lightweight .env file parser for PHP with no dependencies.
Description
Lite Env is a simple library for loading environment variables from .env files in PHP applications. It's designed to be lightweight and dependency-free, making it ideal for use in other libraries where minimizing dependencies is important.
Note: This is a lightweight library primarily intended for developing other libraries. For production applications, consider using more powerful and feature-rich alternatives like vlucas/phpdotenv or symfony/dotenv.
Features
- Zero dependencies
- Simple and intuitive API
- Automatic loading of
.envand.env.localfiles - Support for variable interpolation (
${VAR}or$VARsyntax) - Type conversion (strings, integers, floats, booleans, null)
- Multiline values
- Quoted values (both single and double quotes)
- Inline comments support with proper whitespace handling
- Multiple file loading with single method call
- Comprehensive error handling and validation
Installation
composer require petstack/lite-env
Usage
Basic Usage
<?php use PetStack\LiteEnv\Env; // Load variables from .env and .env.local files automatically Env::load(); // Load custom files along with default ones Env::load('/path/to/custom.env'); // Load multiple custom files Env::load('/path/to/.env.production', '/path/to/.env.secrets'); // Disable automatic loading of default files Env::$disableDefaultPaths = true; Env::load('/path/to/custom.env'); // Only loads custom.env // Get an environment variable $dbName = Env::get('DATABASE_NAME'); // Get with a default value if the variable doesn't exist $port = Env::get('PORT', 3000); // Check if a variable exists if (Env::has('DEBUG')) { // Do something } // Get all loaded environment variable keys $keys = Env::getAllKeys();
New in v2.0: Automatic File Loading
By default, Env::load() will automatically load both .env and .env.local files if they exist in the current directory. The .env.local file takes precedence over .env for overlapping variables.
// This will load .env, .env.local, and custom.env (in that order) Env::load('custom.env'); // To load only specific files without defaults: Env::$disableDefaultPaths = true; Env::load('production.env', 'secrets.env');
Example .env File
# Database configuration
DATABASE_NAME=myapp_db
DATABASE_USER=dbuser
DATABASE_PASS=secret
# Application settings
APP_ENV=development
DEBUG=true
PORT=3000
# Path configuration with variable interpolation
BASE_PATH=/var/www/app
LOG_PATH=${BASE_PATH}/logs
CACHE_PATH=$BASE_PATH/cache
# Values with inline comments (v2.0 improvement)
API_URL=https://api.example.com # Production API endpoint
TIMEOUT=30 # Connection timeout in seconds
# Multiline value
MULTILINE_VALUE="line1
line2
line3"
# Quoted values with inner quotes
QUOTED_VALUE='He said "Hello World"'
JSON_CONFIG={"key": "value", "debug": true}
Type Conversion
Lite Env automatically converts values to appropriate PHP types:
true,(true),TRUE→true(boolean)false,(false),FALSE→false(boolean)null,(null)→nullempty,(empty),"",''→''(empty string)- Numeric values → integers or floats
Version 2.0 Breaking Changes
If you're upgrading from v1.x, please note these breaking changes:
- Class is now final - You cannot extend the
Envclass anymore - Removed
loadMultiple()method - Useload(string ...$paths)instead:// Old (v1.x) Env::loadMultiple('/path/to/.env', '/path/to/another.env'); // New (v2.0) Env::load('/path/to/.env', '/path/to/another.env');
- Automatic default file loading -
load()now loads.envand.env.localby default
See CHANGELOG.md for detailed migration guide.
Requirements
- PHP 8.0 or higher
License
This library is licensed under the MIT License - see the LICENSE file for details.