sitopapa/supenv

A powerful, zero-dependency PHP Env Manager & Encrypter.

Maintainers

Package info

github.com/Sitopapa/supenv

pkg:composer/sitopapa/supenv

Statistics

Installs: 7

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v2.0.0 2025-11-25 21:20 UTC

This package is auto-updated.

Last update: 2026-03-25 21:59:51 UTC


README

Tests Code Quality PHP Version License

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 .env to .env.enc using Sodium for safe repository storage.
  • ๐Ÿ›ก๏ธ Type Safety: Retrieve values as bool, int, or string easily in your code.
  • โœ… Validation: Compare .env against .env.example to find missing keys (Perfect for CI/CD).
  • ๐Ÿ”„ Key Rotation: Safely rotate your encryption keys without data loss.
  • ๐Ÿ“ฆ Auto-Backup: Automatically creates .env.bak before any modification.
  • ๐Ÿ™ˆ Masking: list command masks sensitive data like API_KEY or PASSWORD.
  • ๐Ÿงช 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 found
  • EncryptionException - When encryption fails
  • DecryptionException - When decryption fails
  • ValidationException - When validation fails (has getMissingKeys() 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.key ONLY 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:

  1. Commit .env.enc to repository
  2. Upload .env.key to server manually (via SSH, secure file transfer, or secrets manager)
  3. Run supenv decrypt on 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