Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Illuminate\Filesystem\Filesystem;
  7.  
  8. class DatabaseEnvironment extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'env:db';
  16.  
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Configure database settings';
  23.  
  24. /**
  25. * @var File
  26. */
  27. private $file;
  28.  
  29. public function __construct(Filesystem $file)
  30. {
  31. $this->file = $file;
  32.  
  33. parent::__construct();
  34. }
  35.  
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $this->doAsks();
  44. }
  45.  
  46. private function doAsks()
  47. {
  48. $database = $this->ask('What is your database name?');
  49. $username = $this->ask('What is your database username?');
  50. $password = $this->secret('What is your database username\'s password?');
  51.  
  52. $this->info('PLEASE CONFIRM YOUR DATABASE DETAILS');
  53. $this->line('');
  54. $this->info('Database Name: '.$database);
  55. $this->info('Username: '.$username);
  56. $this->info('Password: ********'); // purposely hide it
  57.  
  58. if ($this->confirm('Are you confirm all the details are correct? [y|N]')) {
  59.  
  60. $env = $this->laravel->app->basePath() . '/' . $this->laravel->app->environmentFile();
  61. $content = $this->file->get($env);
  62.  
  63. $content = preg_replace("/^(DB_DATABASE=){1}\w+/im", "DB_DATABASE=".$database, $content, -1);
  64. $content = preg_replace("/^(DB_USERNAME=){1}\w+/im", "DB_USERNAME=".$username, $content, -1);
  65. $content = preg_replace("/^(DB_PASSWORD=){1}\w+/im", "DB_PASSWORD=".$password, $content, -1);
  66.  
  67. $this->file->put($this->laravel->app->basePath() . '/.env', $content );
  68.  
  69. } else {
  70. if ($this->confirm('Do you want to reconfigure? [y|N]')) {
  71. $this->doAsks();
  72. }
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement