sitopapa / supenv
A powerful, zero-dependency PHP Env Manager & Encrypter.
Requires
- php: ^8.1
- ext-sodium: *
Requires (Dev)
- phpunit/phpunit: ^10.5
README
Supenv is a zero-dependency, production-ready tool to manage, secure, and validate your .env files. Use it as a robust CLI Tool or integrate it directly into your PHP application as a Library.
It uses Sodium encryption (industry standard) to keep your secrets safe.
๐ Features
- Zero Dependency: No external packages required. Lightweight and fast.
- ๐ Encryption: Encrypt
.envto.env.encusing Sodium for safe repository storage. - ๐ก๏ธ Type Safety: Retrieve values as
bool,int, orstringeasily in your code. - โ
Validation: Compare
.envagainst.env.exampleto find missing keys (Perfect for CI/CD). - ๐ Key Rotation: Safely rotate your encryption keys without data loss.
- ๐ฆ Auto-Backup: Automatically creates
.env.bakbefore any modification. - ๐ Masking:
listcommand masks sensitive data likeAPI_KEYorPASSWORD. - ๐งช Fully Tested: 41 tests with ~95% code coverage.
- ๐ CI/CD Ready: Automated testing with GitHub Actions.
- โ ๏ธ Custom Exceptions: Type-safe error handling for better debugging.
๐ฆ Installation
composer require sitopapa/supenv
Requirements:
- PHP 8.1 or higher
- ext-sodium extension (usually enabled by default)
๐ป CLI Usage
Supenv comes with a built-in terminal command.
1. Direct Usage
# Linux / Mac vendor/bin/supenv list vendor/bin/supenv help # Windows vendor\bin\supenv list vendor\bin\supenv help
2. Composer Script (Recommended)
Add this to your composer.json:
"scripts": { "supenv": "supenv" }
Now run:
composer supenv list
composer supenv help
CLI Commands
| Command | Description | Example |
|---|---|---|
| encrypt | Encrypts .env โ .env.enc & generates key |
composer supenv encrypt |
| decrypt | Decrypts .env.enc โ .env |
composer supenv decrypt |
| list | Lists all variables (masks sensitive data) | composer supenv list |
| get | Gets a specific value (unmasked) | composer supenv get DB_PASSWORD |
| set | Sets or updates a value | composer supenv set APP_DEBUG true |
| unset | Removes a variable | composer supenv unset APP_DEBUG |
| rotate | Rotates encryption key safely | composer supenv rotate |
| validate | Checks missing keys vs .env.example |
composer supenv validate |
| example | Generates .env.example from .env |
composer supenv example |
๐ Library Usage (PHP)
use Sitopapa\Supenv\Supenv; // Initialize $env = new Supenv(__DIR__ . '/.env'); $env->load(); // --- Reading Data --- $debug = $env->getBool('APP_DEBUG'); // true/false $port = $env->getInt('DB_PORT'); // int 3306 $key = $env->get('API_KEY'); // string // --- Writing Data --- $env->set('DB_HOST', '127.0.0.1'); $env->setMany([ 'APP_ENV' => 'production', 'CACHE_DRIVER' => 'redis' ]); // Save changes $env->save(); // --- Encryption --- $env->encrypt(); // creates .env.enc $env->decrypt('.env.enc'); // restores .env // --- Validation --- $env->require(['DB_HOST', 'DB_PASSWORD', 'API_KEY']); // Throws ValidationException if missing
โ ๏ธ Exception Handling
Supenv uses custom exceptions for better error handling:
use Sitopapa\Supenv\Supenv; use Sitopapa\Supenv\Exceptions\ValidationException; use Sitopapa\Supenv\Exceptions\FileNotFoundException; use Sitopapa\Supenv\Exceptions\DecryptionException; try { $env = new Supenv('.env'); $env->load(); $env->require(['DB_HOST', 'DB_PASSWORD']); } catch (ValidationException $e) { // Get missing keys $missing = $e->getMissingKeys(); echo "Missing keys: " . implode(', ', $missing); } catch (FileNotFoundException $e) { echo "File not found: " . $e->getMessage(); } catch (DecryptionException $e) { echo "Decryption failed: " . $e->getMessage(); }
Available Exceptions:
SupenvException- Base exception (all Supenv exceptions extend this)FileNotFoundException- When a required file is not foundEncryptionException- When encryption failsDecryptionException- When decryption failsValidationException- When validation fails (hasgetMissingKeys()method)SecurityException- When a security violation is detected
๐งช Testing
Supenv comes with a comprehensive test suite:
# Run all tests composer test # Run tests with coverage report composer test:coverage # Run specific test composer test:filter testEncryptionWorks
Test Stats:
- โ 41 tests
- โ 92 assertions
- โ ~95% code coverage
๐ Security Best Practices
What to commit:
- โ
.env.enc(encrypted environment file) - โ
.env.example(template without values) - โ
.env.keyONLY on local development
What to NEVER commit:
- โ
.env(actual environment file with secrets) - โ
.env.bak(backup file) - โ
.env.key(encryption key in production)
Production Deployment:
- Commit
.env.encto repository - Upload
.env.keyto server manually (via SSH, secure file transfer, or secrets manager) - Run
supenv decrypton server to restore.env
Recommended .gitignore
.env .env.bak .env.key
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development:
# Clone the repository git clone https://github.com/sitopapa/supenv.git cd supenv # Install dependencies composer install # Run tests composer test # Run tests with coverage composer test:coverage
๐ CI/CD
Supenv uses GitHub Actions for automated testing:
- โ Tests run on PHP 8.1, 8.2, 8.3
- โ Cross-platform testing (Ubuntu, Windows, macOS)
- โ Automatic code quality checks
- โ Composer validation
๐ License
MIT License - see LICENSE file for details.
๐ Credits
Created and maintained by Veli GEรGEL
๐ก Support
If you find this package useful, please consider:
- โญ Starring the repository
- ๐ Reporting bugs
- ๐ก Suggesting new features
- ๐ค Contributing to the code
Made with โค๏ธ in Turkey