-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
UserDataProvider.php
132 lines (107 loc) · 3.8 KB
/
UserDataProvider.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Install\Console;
use Flarum\Install\AdminUser;
use Flarum\Install\BaseUrl;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Installation;
use Illuminate\Support\Str;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class UserDataProvider implements DataProviderInterface
{
protected BaseUrl $baseUrl;
public function __construct(
protected InputInterface $input,
protected OutputInterface $output,
protected QuestionHelper $questionHelper
) {
}
public function configure(Installation $installation): Installation
{
return $installation
->debugMode(false)
->baseUrl($this->getBaseUrl())
->databaseConfig($this->getDatabaseConfiguration())
->adminUser($this->getAdminUser())
->settings($this->getSettings());
}
private function getDatabaseConfiguration(): DatabaseConfig
{
$host = $this->ask('Database host (required):');
$port = 3306;
if (Str::contains($host, ':')) {
list($host, $port) = explode(':', $host, 2);
}
return new DatabaseConfig(
'mysql',
$host,
intval($port),
$this->ask('Database name (required):'),
$this->ask('Database user (required):'),
$this->secret('Database password:'),
$this->ask('Prefix:')
);
}
private function getBaseUrl(): BaseUrl
{
$baseUrl = $this->ask('Base URL (Default: http://flarum.localhost):', 'http://flarum.localhost');
return $this->baseUrl = BaseUrl::fromString($baseUrl);
}
private function getAdminUser(): AdminUser
{
return new AdminUser(
$this->ask('Admin username (Default: admin):', 'admin'),
$this->askForAdminPassword(),
$this->ask('Admin email address (required):')
);
}
private function askForAdminPassword(): string
{
while (true) {
$password = $this->secret('Admin password (required >= 8 characters):');
if (strlen($password) < 8) {
$this->validationError('Password must be at least 8 characters.');
continue;
}
$confirmation = $this->secret('Admin password (confirmation):');
if ($password !== $confirmation) {
$this->validationError('The password did not match its confirmation.');
continue;
}
return $password;
}
}
private function getSettings(): array
{
$title = $this->ask('Forum title:');
return [
'forum_title' => $title,
'mail_from' => $this->baseUrl->toEmail('noreply'),
'welcome_title' => 'Welcome to '.$title,
];
}
private function ask(string $question, string $default = null): mixed
{
$question = new Question("<question>$question</question> ", $default);
return $this->questionHelper->ask($this->input, $this->output, $question);
}
private function secret(string $question): mixed
{
$question = new Question("<question>$question</question> ");
$question->setHidden(true)->setHiddenFallback(true);
return $this->questionHelper->ask($this->input, $this->output, $question);
}
private function validationError(string $message): void
{
$this->output->writeln("<error>$message</error>");
$this->output->writeln('Please try again.');
}
}