Beautified the search and replace function

This commit is contained in:
Miguel Nogueira 2020-07-09 08:46:45 +01:00
parent 4456d19c1f
commit fb4d832489
1 changed files with 12 additions and 5 deletions

View File

@ -32,20 +32,27 @@ class SetEnv extends Command
/** /**
* Execute the console command. * Execute the console command.
* @see https://stackoverflow.com/a/50965881/11540218
* @return mixed * @return mixed
*/ */
public function handle() public function handle()
{ {
$path = base_path('/.env'); $path = base_path('/.env');
$name = $this->argument('key'); $key = $this->argument('key');
$value = $this->argument('value'); $value = $this->argument('value');
$originalValue = env($key);
if (file_exists($path)) if (file_exists($path))
{ {
file_put_contents($path, str_replace( $file = file_get_contents($path);
$name . '=' . env($name), $name . '=' . $value, file_get_contents($path) $newConfig = str_replace($key . '=' . $originalValue, $key . '=' . $value, $file);
));
file_put_contents(
$path,
$newConfig
);
} }
$this->info('>> Changed value! It may now be accessed via env() or config() if there\'s a file for it.'); $this->info('>> Changed value! It may now be accessed via env() or config() if there\'s a file for it.');