Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
162 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace MOLiBot\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class DotEnvSet extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'dotenv:set {key} {value?}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Set and save an environment variable in the .env file'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
try { | ||
list($key, $value) = $this->getKeyValue(); | ||
} catch (\InvalidArgumentException $e) { | ||
return $this->error($e->getMessage()); | ||
} | ||
|
||
$envFilePath = app()->environmentFilePath(); | ||
|
||
$contents = file_get_contents($envFilePath); | ||
|
||
if ($oldValue = $this->getOldValue($contents, $key)) { | ||
$contents = str_replace("{$key}={$oldValue}", "{$key}={$value}", $contents); | ||
|
||
$this->writeFile($envFilePath, $contents); | ||
|
||
return $this->info("Environment variable with key '{$key}' has been changed from '{$oldValue}' to '{$value}'"); | ||
} | ||
|
||
$contents = $contents . "\n{$key}={$value}\n"; | ||
|
||
$this->writeFile($envFilePath, $contents); | ||
|
||
return $this->info("A new environment variable with key '{$key}' has been set to '{$value}'"); | ||
} | ||
|
||
/** | ||
* Overwrite the contents of a file. | ||
* | ||
* @param string $path | ||
* @param string $contents | ||
* @return boolean | ||
*/ | ||
protected function writeFile(string $path, string $contents): bool | ||
{ | ||
$file = fopen($path, 'w'); | ||
|
||
fwrite($file, $contents); | ||
|
||
return fclose($file); | ||
} | ||
|
||
/** | ||
* Get the old value of a given key from an environment file. | ||
* | ||
* @param string $envFile | ||
* @param string $key | ||
* @return string | ||
*/ | ||
protected function getOldValue(string $envFile, string $key): string | ||
{ | ||
// Match the given key at the beginning of a line | ||
preg_match("/^{$key}=[^\r\n]*/m", $envFile, $matches); | ||
|
||
if (count($matches)) { | ||
return substr($matches[0], strlen($key) + 1); | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
/** | ||
* Determine what the supplied key and value is from the current command. | ||
* | ||
* @return array | ||
*/ | ||
protected function getKeyValue(): array | ||
{ | ||
$key = $this->argument('key'); | ||
|
||
$value = $this->argument('value'); | ||
|
||
if (! $value) { | ||
$parts = explode('=', $key, 2); | ||
|
||
if (count($parts) !== 2) { | ||
throw new \InvalidArgumentException('No value was set'); | ||
} | ||
|
||
$key = $parts[0]; | ||
|
||
$value = $parts[1]; | ||
} | ||
|
||
if (! $this->isValidKey($key)) { | ||
throw new \InvalidArgumentException('Invalid argument key'); | ||
} | ||
|
||
if (! is_bool(strpos($value, ' '))) { | ||
$value = '"' . $value . '"'; | ||
} | ||
|
||
return [strtoupper($key), $value]; | ||
} | ||
|
||
/** | ||
* Check if a given string is valid as an environment variable key. | ||
* | ||
* @param string $key | ||
* @return boolean | ||
*/ | ||
protected function isValidKey(string $key): bool | ||
{ | ||
if (str_contains($key, '=')) { | ||
throw new \InvalidArgumentException("Environment key should not contain '='"); | ||
} | ||
|
||
if (!preg_match('/^[a-zA-Z_]+$/', $key)) { | ||
throw new \InvalidArgumentException('Invalid environment key. Only use letters and underscores'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters