Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Filesystem\Filesystem;
- class DatabaseEnvironment extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'env:db';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Configure database settings';
- /**
- * @var File
- */
- private $file;
- public function __construct(Filesystem $file)
- {
- $this->file = $file;
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- $this->doAsks();
- }
- private function doAsks()
- {
- $database = $this->ask('What is your database name?');
- $username = $this->ask('What is your database username?');
- $password = $this->secret('What is your database username\'s password?');
- $this->info('PLEASE CONFIRM YOUR DATABASE DETAILS');
- $this->line('');
- $this->info('Database Name: '.$database);
- $this->info('Username: '.$username);
- $this->info('Password: ********'); // purposely hide it
- if ($this->confirm('Are you confirm all the details are correct? [y|N]')) {
- $env = $this->laravel->app->basePath() . '/' . $this->laravel->app->environmentFile();
- $content = $this->file->get($env);
- $content = preg_replace("/^(DB_DATABASE=){1}\w+/im", "DB_DATABASE=".$database, $content, -1);
- $content = preg_replace("/^(DB_USERNAME=){1}\w+/im", "DB_USERNAME=".$username, $content, -1);
- $content = preg_replace("/^(DB_PASSWORD=){1}\w+/im", "DB_PASSWORD=".$password, $content, -1);
- $this->file->put($this->laravel->app->basePath() . '/.env', $content );
- } else {
- if ($this->confirm('Do you want to reconfigure? [y|N]')) {
- $this->doAsks();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement