Skip to content

Instantly share code, notes, and snippets.

@GregoireHebert
Last active November 6, 2018 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GregoireHebert/3e6feeade4f1eb4035aa25c09285fb01 to your computer and use it in GitHub Desktop.
Save GregoireHebert/3e6feeade4f1eb4035aa25c09285fb01 to your computer and use it in GitHub Desktop.
DotenvUpdater
#!/usr/bin/env php
<?php
declare(strict_types=1);
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
if (file_exists($file)) {
require $file; break;
}
}
$dotenv = new \Symfony\Component\Dotenv\Dotenv();
$text = 'This will add new entries to your Dotenv file, but won\'t update existing ones...' . PHP_EOL;
echo sprintf("\033[%sm%s\033[%sm", implode(';', ['34']), $text, implode(';', ['39']));
$environment = array_merge(
$dotenv->parse(file_get_contents(__DIR__ . '/../.env.dist'), __DIR__ . '/../.env.dist'),
$dotenv->parse(file_get_contents(__DIR__ . '/../.env'), __DIR__ . '/../.env')
);
file_put_contents(__DIR__ . '/../.env', implode(PHP_EOL, array_map(function ($v, $k) {
return sprintf("%s='%s'", $k, $v);
},
$environment,
array_keys($environment)
)));
$text = 'Dotenv file successfully updated!' . PHP_EOL;
echo sprintf("\033[%sm%s\033[%sm", implode(';', ['32']), $text, implode(';', ['39']));
exit(0);
@GregoireHebert
Copy link
Author

Put this on you bin directory and set it executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment