66 lines
1.6 KiB
PHP
66 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
class SetupDatabase extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
* @var string
|
|
*/
|
|
protected $signature = 'db:setup {host} {user} {password} {database}';
|
|
/**
|
|
* The console command description.
|
|
* @var string
|
|
*/
|
|
protected $description = 'Setup database credentials';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
echo "zpracovavam";
|
|
$host = $this->argument('host');
|
|
$user = $this->argument('user');
|
|
$password = $this->argument('password');
|
|
$database = $this->argument('database');
|
|
|
|
// try to connect with this settings
|
|
// @todo add credentials verification
|
|
|
|
// if pass, proceed to creating .env file
|
|
$parameters = [
|
|
'DB_HOST' => $host,
|
|
'DB_USERNAME' => $user,
|
|
'DB_PASSWORD' => $password,
|
|
'DB_DATABASE' => $database
|
|
];
|
|
|
|
$envPath = base_path('.env');
|
|
$env = file_get_contents($envPath);
|
|
if (!file_exists($envPath))
|
|
$env = file_get_contents($envPath.'.example');
|
|
|
|
foreach ($parameters as $parameter => $value) {
|
|
$env = preg_replace('/'.$parameter.'=(.*)/i', $parameter.'='.$value, $env);
|
|
}
|
|
|
|
file_put_contents($envPath, $env);
|
|
|
|
|
|
}
|
|
}
|