Skip to content

Commit

Permalink
use regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 21, 2024
1 parent 3a0e8c9 commit 9e1d4f5
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,14 @@ protected function configureDefaultDatabaseConnection(string $directory, string
$database = 'mysql';
}

$this->replaceInFile(
'DB_CONNECTION=sqlite',
$this->pregReplaceInFile(
'DB_CONNECTION=.*',
'DB_CONNECTION='.$database,
$directory.'/.env'
);

$this->replaceInFile(
'DB_CONNECTION=sqlite',
$this->pregReplaceInFile(
'DB_CONNECTION=.*',
'DB_CONNECTION='.$database,
$directory.'/.env.example'
);
Expand Down Expand Up @@ -823,4 +823,20 @@ protected function replaceInFile(string|array $search, string|array $replace, st
str_replace($search, $replace, file_get_contents($file))
);
}

/**
* Replace the given string in the given file using regular expressions.
*
* @param string|array $search
* @param string|array $replace
* @param string $file
* @return void
*/
protected function pregReplaceInFile(string $pattern, string $replace, string $file)
{
file_put_contents(
$file,
preg_replace($pattern, $replace, file_get_contents($file))
);
}
}

0 comments on commit 9e1d4f5

Please sign in to comment.