Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Created August 16, 2016 10:10
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 nasrulhazim/dba6d25d58b13739d1cbaeebaf31f7ee to your computer and use it in GitHub Desktop.
Save nasrulhazim/dba6d25d58b13739d1cbaeebaf31f7ee to your computer and use it in GitHub Desktop.
A sample of custom Artisan command
<?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();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment