joe1992w/envsemble

A Laravel package for merging multiple .env files with patch support and advanced features.

v0.1.0 2025-06-03 09:02 UTC

This package is not auto-updated.

Last update: 2025-06-04 07:32:29 UTC


README

Envsemble Logo

A powerful Laravel package for merging multiple .env files with patch support and advanced features.

Envsemble allows you to maintain a base environment configuration and apply targeted patches for different environments, features, or deployment scenarios. Perfect for managing complex Laravel applications with multiple deployment targets.

✨ Features

  • πŸ”§ Base + Patch Architecture: Start with a base .env file and apply patches
  • πŸ—‘οΈ Key Deletion: Use KEY=__DELETE__ to remove keys during merge
  • πŸ“ Source Tracking: Each line in the output shows which file it came from
  • πŸ” Dry Run Mode: Preview changes before applying them
  • πŸ“Š Detailed Reports: See what was added, modified, or deleted
  • πŸ—œοΈ Squash Mode: Combine all files into a new base and remove patches
  • 🎯 CLI Integration: Full Laravel Artisan command support
  • βœ… Comprehensive Tests: Full PHPUnit/Pest test coverage

πŸ“¦ Installation

Install via Composer:

composer require joe1992w/envsemble

For Laravel applications, the service provider will be auto-discovered. For manual registration:

// config/app.php
'providers' => [
    // ...
    JoeWare\Envsemble\EnvsembleServiceProvider::class,
],

πŸš€ Quick Start

1. Basic Usage

php artisan env:build --base=.env.base --patches=env-patches/ --out=.env.generated

2. Dry Run (Preview Changes)

php artisan env:build --base=.env.base --patches=env-patches/ --out=.env.generated --dry-run

3. Squash Mode (Combine All Files)

php artisan env:build --base=.env.base --patches=env-patches/ --out=.env.new --squash

πŸ“ File Structure Example

project/
β”œβ”€β”€ .env.base                 # Base environment file
β”œβ”€β”€ env-patches/              # Directory containing patch files
β”‚   β”œβ”€β”€ 01-development.env    # Development overrides
β”‚   β”œβ”€β”€ 02-local-testing.env  # Testing configuration
β”‚   └── 03-mail-debug.env     # Mail debugging settings
└── .env.generated            # Final merged output

πŸ“ Syntax Examples

Base File (.env.base)

APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
DB_HOST=127.0.0.1
DB_DATABASE=production_db
CACHE_DRIVER=redis
MAIL_MAILER=smtp

Patch File (01-development.env)

# Override for development
APP_ENV=local
APP_DEBUG=true

# Add new keys
LOG_LEVEL=debug
TELESCOPE_ENABLED=true

# Delete keys (removes from final output)
CACHE_DRIVER=__DELETE__

Generated Output (.env.generated)

# Generated by Envsemble on 2025-06-03 10:30:00
# Base: .env.base
# Patches: 01-development.env

APP_NAME="My Laravel App" # from: .env.base
APP_ENV=local # from: 01-development.env
APP_DEBUG=true # from: 01-development.env
DB_HOST=127.0.0.1 # from: .env.base
DB_DATABASE=production_db # from: .env.base
MAIL_MAILER=smtp # from: .env.base
LOG_LEVEL=debug # from: 01-development.env
TELESCOPE_ENABLED=true # from: 01-development.env

πŸ› οΈ Command Options

Option Description Required
--base Path to the base .env file βœ…
--patches Directory containing patch files βœ…
--out Output file path βœ…
--dry-run Preview changes without writing files ❌
--no-comments Exclude source comments from output ❌
--squash Combine all files and remove patches ❌

πŸ“Š Merge Reports

After each merge, Envsemble provides a detailed report:

πŸ“Š Merge Report:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Metric              β”‚ Count β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Base file keys      β”‚ 15    β”‚
β”‚ Patch files processedβ”‚ 3     β”‚
β”‚ Keys added          β”‚ 4     β”‚
β”‚ Keys modified       β”‚ 3     β”‚
β”‚ Keys deleted        β”‚ 2     β”‚
β”‚ Final output keys   β”‚ 20    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

βž• Added keys: LOG_LEVEL, TELESCOPE_ENABLED, DEBUGBAR_ENABLED, API_KEY
πŸ”„ Modified keys: APP_ENV, APP_DEBUG, MAIL_MAILER
πŸ—‘οΈ  Deleted keys: REDIS_HOST, CACHE_DRIVER

πŸ“ Files processed:
   Base: .env.base
   Patch: 01-development.env
   Patch: 02-testing.env
   Patch: 03-debug.env

πŸ“ˆ Efficiency: 133.3% keys retained/added from base

πŸ”§ Programmatic Usage

You can also use Envsemble programmatically:

use JoeWare\Envsemble\EnvMerger;

$merger = new EnvMerger();

// Basic merge
$result = $merger->merge('.env.base', 'env-patches/');
$output = $merger->generateOutput($result);
file_put_contents('.env.generated', $output);

// Get detailed report
$report = $result->getReport();
echo "Added {$result->getAddedKeysCount()} keys\n";
echo "Modified {$result->getModifiedKeysCount()} keys\n";

// Squash files
$result = $merger->squash('.env.base', 'env-patches/', '.env.new');

πŸ—οΈ Advanced Use Cases

Environment-Specific Builds

# Development
php artisan env:build --base=.env.base --patches=patches/dev/ --out=.env

# Staging  
php artisan env:build --base=.env.base --patches=patches/staging/ --out=.env

# Production
php artisan env:build --base=.env.base --patches=patches/prod/ --out=.env

Feature Flag Management

# Enable feature flags
php artisan env:build --base=.env.base --patches=features/feature-x/ --out=.env

# Disable feature (using __DELETE__ in patch)
php artisan env:build --base=.env.base --patches=features/disable-feature-x/ --out=.env

CI/CD Integration

# GitHub Actions example
- name: Build Environment
  run: |
    php artisan env:build \
      --base=.env.production \
      --patches=deploy/patches/ \
      --out=.env \
      --no-comments

πŸ§ͺ Testing

Run the test suite:

composer test

Individual test commands:

composer test:unit      # Unit tests
composer test:types     # Static analysis
composer test:lint      # Code style

🀝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

πŸ“„ License

This package is open-sourced software licensed under the MIT license.

πŸ™‹β€β™‚οΈ Support

Made with ❀️ by Joe Ware