joe1992w / envsemble
A Laravel package for merging multiple .env files with patch support and advanced features.
Requires
- php: ^8.2.0
- illuminate/console: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
Requires (Dev)
- laravel/pint: ^1.13.7
- pestphp/pest: ^2.28.1
- phpstan/phpstan: ^1.10.50
- rector/rector: ^0.18.13
- symfony/var-dumper: ^6.4.0|^7.0.0
This package is not auto-updated.
Last update: 2025-06-04 07:32:29 UTC
README

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
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Made with β€οΈ by Joe Ware