1. Go to this page and download the library: Download bacarndiaye/envmap-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bacarndiaye / envmap-php example snippets
use Gaindetech\EnvMap\EnvMap;
// Create an instance
$envmap = EnvMap::create()
->withEnv('dev')
->withCache(true);
// Get a secret
$dbUrl = $envmap->get('DATABASE_URL');
// Get with default value
$debug = $envmap->getOrDefault('DEBUG', 'false');
// Check if a secret exists
if ($envmap->has('API_KEY')) {
$apiKey = $envmap->get('API_KEY');
}
// Get all secrets
$secrets = $envmap->getAll();
// Set a secret
$envmap->set('NEW_SECRET', 'secret-value');
// Delete a secret
$envmap->delete('OLD_SECRET');
t a secret
$dbUrl = envmap_get('DATABASE_URL');
// With default value
$debug = envmap_get('DEBUG', 'false');
// Check existence
if (envmap_has('API_KEY')) {
// ...
}
// Load into $_ENV and $_SERVER
envmap_load();
// Now you can use getenv()
$dbUrl = getenv('DATABASE_URL');
use Gaindetech\EnvMap\EnvMapFactory;
// For development
$envmap = EnvMapFactory::forDev();
// For staging
$envmap = EnvMapFactory::forStaging();
// For production
$envmap = EnvMapFactory::forProduction();
// Auto-detect based on APP_ENV or ENV
$envmap = EnvMapFactory::fromEnvironment();
use Gaindetech\EnvMap\EnvMap;
$envmap = EnvMap::create()->withEnv('dev');
// Sync to a .env file
$envmap->sync('.env');
// With options
$envmap->sync(
outputPath: '.env',
merge: true, // Keep local-only keys
keepLocal: false, // Provider wins on conflict
force: true, // Ignore git warnings
backup: true // Create .env.bak
);
// Check for differences without writing
$drift = $envmap->checkDrift('.env');
if ($drift['has_drift']) {
foreach ($drift['differences'] as $key => $diff) {
echo "$key: file={$diff['file']} provider={$diff['provider']}\n";
}
}
$envmap = EnvMap::create()->withEnv('dev');
// Import secrets from a .env file
$envmap->import('.env.local');
// Import and delete source file
$envmap->import('.env.local', deleteAfter: true);
use Gaindetech\EnvMap\Laravel\Facades\EnvMap;
// Via Facade
$dbUrl = EnvMap::get('DATABASE_URL');
$all = EnvMap::getAll();
EnvMap::set('KEY', 'value');
// Via dependency injection
use Gaindetech\EnvMap\EnvMap;
class MyController
{
public function __construct(private EnvMap $envmap) {}
public function index()
{
$secret = $this->envmap->get('API_KEY');
}
}
use Gaindetech\EnvMap\EnvMap;
class MyService
{
public function __construct(private EnvMap $envmap) {}
public function doSomething(): void
{
$apiKey = $this->envmap->get('API_KEY');
}
}
use Gaindetech\EnvMap\EnvMap;
use Gaindetech\EnvMap\Exception\SecretNotFoundException;
use Gaindetech\EnvMap\Exception\EnvMapException;
use Gaindetech\EnvMap\Exception\ConfigurationException;
$envmap = EnvMap::create()->withEnv('dev');
try {
$secret = $envmap->get('NON_EXISTENT_KEY');
} catch (SecretNotFoundException $e) {
// Secret does not exist
echo "Secret not found: " . $e->getMessage();
} catch (ConfigurationException $e) {
// envmap configuration problem
echo "Configuration error: " . $e->getMessage();
} catch (EnvMapException $e) {
// Other envmap error
echo "Error: " . $e->getMessage();
}
use Gaindetech\EnvMap\EnvMap;
// Custom path to envmap
$envmap = EnvMap::create('/usr/local/bin/envmap');
// Or via environment variable
putenv('ENVMAP_BINARY_PATH=/custom/path/envmap');
use Gaindetech\EnvMap\EnvMap;
// Enable cache (recommended in production)
$envmap = EnvMap::create()
->withEnv('prod')
->withCache(true);
// Repeated calls to get() use the cache
$value1 = $envmap->get('KEY'); // Calls envmap
$value2 = $envmap->get('KEY'); // From cache
// Clear cache manually
$envmap->clearCache();