-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDatabaseSettingsCommand.php
More file actions
113 lines (91 loc) · 4.45 KB
/
DatabaseSettingsCommand.php
File metadata and controls
113 lines (91 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Jexactyl\Console\Commands\Environment;
use Illuminate\Console\Command;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\DatabaseManager;
use Jexactyl\Traits\Commands\EnvironmentWriterTrait;
class DatabaseSettingsCommand extends Command
{
use EnvironmentWriterTrait;
protected $description = 'Defina as configurações do database para o painel.';
protected $signature = 'p:environment:database
{--host= : O endereço de conexão para o servidor MySQL.}
{--port= : A porta de conexão para o servidor MySQL.}
{--database= : O database a ser usado.}
{--username= : Nome de usuário para usar ao conectar.}
{--password= : Senha a ser usada para este database.}';
protected array $variables = [];
/**
* DatabaseSettingsCommand constructor.
*/
public function __construct(private DatabaseManager $database, private Kernel $console)
{
parent::__construct();
}
/**
* Handle command execution.
*
* @throws \Jexactyl\Exceptions\JexactylException
*/
public function handle(): int
{
$this->output->note(' É altamente recomendável não usar "localhost" como seu host de database, pois temos visto problemas frequentes de conexão de soquete. Se você quiser usar uma conexão local, você deve estar usando "127.0.0.1".');
$this->variables['DB_HOST'] = $this->option('host') ?? $this->ask(
'Host do Database ',
config('database.connections.mysql.host', '127.0.0.1')
);
$this->variables['DB_PORT'] = $this->option('port') ?? $this->ask(
'Porta do Database ',
config('database.connections.mysql.port', 3306)
);
$this->variables['DB_DATABASE'] = $this->option('database') ?? $this->ask(
'Nome do Database ',
config('database.connections.mysql.database', 'panel')
);
$this->output->note('Usar a conta "root" para conexões MySQL não é apenas altamente desaprovado, mas também não é permitido por este aplicativo. Você precisa ter criado um usuário MySQL para este software.');
$this->variables['DB_USERNAME'] = $this->option('username') ?? $this->ask(
'Usuário do Database',
config('database.connections.mysql.username', 'Jexactyl')
);
$askForMySQLPassword = true;
if (!empty(config('database.connections.mysql.password')) && $this->input->isInteractive()) {
$this->variables['DB_PASSWORD'] = config('database.connections.mysql.password');
$askForMySQLPassword = $this->confirm('Parece que você já tem uma senha de conexão MySQL definida, você gostaria de alterá-la?');
}
if ($askForMySQLPassword) {
$this->variables['DB_PASSWORD'] = $this->option('password') ?? $this->secret(' Senha do Database');
}
try {
$this->testMySQLConnection();
} catch (\PDOException $exception) {
$this->output->error(sprintf('Incapaz de conectar-se ao servidor MySQL usando as credenciais fornecidas. O erro retornado foi "%s".', $exception->getMessage()));
$this->output->error('Suas credenciais de conexão NÃO foram salvas. Você precisará fornecer informações válidas de conexão antes de prosseguir.');
if ($this->confirm('Voltar e tentar novamente?')) {
$this->database->disconnect('_Jexactyl_command_test');
return $this->handle();
}
return 1;
}
$this->writeToEnvironment($this->variables);
$this->info($this->console->output());
return 0;
}
/**
* Test that we can connect to the provided MySQL instance and perform a selection.
*/
private function testMySQLConnection()
{
config()->set('database.connections._Jexactyl_command_test', [
'driver' => 'mysql',
'host' => $this->variables['DB_HOST'],
'port' => $this->variables['DB_PORT'],
'database' => $this->variables['DB_DATABASE'],
'username' => $this->variables['DB_USERNAME'],
'password' => $this->variables['DB_PASSWORD'],
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'strict' => true,
]);
$this->database->connection('_Jexactyl_command_test')->getPdo();
}
}