|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\installer\commands; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\core\helpers\UpdateHelper; |
|
6
|
|
|
use app\modules\installer\models\AdminUser; |
|
7
|
|
|
use app\modules\installer\models\DbConfig; |
|
8
|
|
|
use app\modules\installer\components\InstallerFilter; |
|
9
|
|
|
use app\modules\installer\components\InstallerHelper; |
|
10
|
|
|
use app\modules\installer\models\FinalStep; |
|
11
|
|
|
use app\modules\installer\models\MigrateModel; |
|
12
|
|
|
use Yii; |
|
13
|
|
|
use yii\base\DynamicModel; |
|
14
|
|
|
use yii\console\Controller; |
|
15
|
|
|
use yii\helpers\Console; |
|
16
|
|
|
|
|
17
|
|
|
class InstallController extends Controller |
|
18
|
|
|
{ |
|
19
|
|
|
private $db = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function behaviors() |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
'installer' => [ |
|
28
|
|
|
'class' => InstallerFilter::className(), |
|
29
|
|
|
], |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function actionIndex() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->stdout("Checking permissions\n", Console::FG_YELLOW); |
|
36
|
|
|
$permissions = InstallerHelper::checkPermissions(); |
|
37
|
|
|
$ok = true; |
|
38
|
|
|
foreach ($permissions as $file => $result) { |
|
39
|
|
|
|
|
40
|
|
|
if ($result) { |
|
41
|
|
|
$this->stdout('[ OK ] ', Console::FG_GREEN); |
|
42
|
|
|
} else { |
|
43
|
|
|
$this->stdout('[ Error ] ', Console::FG_RED); |
|
44
|
|
|
} |
|
45
|
|
|
$this->stdout($file); |
|
46
|
|
|
$this->stdout("\n"); |
|
47
|
|
|
$ok = $ok && $result; |
|
48
|
|
|
} |
|
49
|
|
|
if (!$ok) { |
|
50
|
|
|
if ($this->confirm("\nSome of your files are not accessible.\nContinue at your own risk?", true)) { |
|
51
|
|
|
return $this->language(); |
|
52
|
|
|
} else { |
|
53
|
|
|
return 1; |
|
54
|
|
|
} |
|
55
|
|
|
} else { |
|
56
|
|
|
return $this->language(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function language() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
Yii::$app->session->set('language', $this->prompt('Enter language(ie. ru, zh-CN, en)',[ |
|
63
|
|
|
'required' => true, |
|
64
|
|
|
'default' => 'en', |
|
65
|
|
|
])); |
|
66
|
|
|
return $this->dbConfig(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function dbConfig() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$config = $this->getDbConfigFromSession(); |
|
72
|
|
|
|
|
73
|
|
|
$model = new DbConfig(); |
|
74
|
|
|
$model->setAttributes($config); |
|
75
|
|
|
|
|
76
|
|
|
$this->stdout("Enter your database configuration:\n", Console::FG_YELLOW); |
|
77
|
|
|
foreach ($model->attributes() as $attribute) { |
|
78
|
|
|
if ($attribute !== 'enableSchemaCache') { |
|
79
|
|
|
$model->setAttributes( |
|
80
|
|
|
[ |
|
81
|
|
|
$attribute => $this->prompt("-> $attribute", [ |
|
82
|
|
|
'required' => in_array($attribute, ['db_host', 'db_name', 'username']), |
|
83
|
|
|
'default' => $model->$attribute, |
|
84
|
|
|
]), |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (!$this->interactive) { |
|
91
|
|
|
if (getenv('DB_USER')) { |
|
92
|
|
|
$model->username = getenv('DB_USER'); |
|
93
|
|
|
} |
|
94
|
|
|
if (getenv('DB_PASS')) { |
|
95
|
|
|
$model->password = getenv('DB_PASS'); |
|
96
|
|
|
} |
|
97
|
|
|
if (getenv('DB_NAME')) { |
|
98
|
|
|
$model->db_name = getenv('DB_NAME'); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
$config = $model->getAttributes(); |
|
102
|
|
|
$config['connectionOk'] = false; |
|
103
|
|
|
|
|
104
|
|
|
if ($model->testConnection() === false) { |
|
105
|
|
|
$config['connectionOk'] = true; |
|
106
|
|
|
$this->stderr("Could not connect to databse!\n", Console::FG_RED); |
|
107
|
|
|
$this->dbConfig(); |
|
108
|
|
|
} |
|
109
|
|
|
Yii::$app->session->set('db-config', $config); |
|
110
|
|
|
return $this->migration(); |
|
111
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function migration() |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$config = $this->getDbConfigFromSession(); |
|
117
|
|
|
|
|
118
|
|
|
$dbConfigModel = new DbConfig(); |
|
119
|
|
|
$dbConfigModel->setAttributes($config); |
|
120
|
|
|
$config = InstallerHelper::createDatabaseConfig($dbConfigModel->getAttributes()); |
|
121
|
|
|
if (InstallerHelper::createDatabaseConfigFile($config) === false) { |
|
122
|
|
|
$this->stderr(Yii::t('app', 'Unable to create db-local config'), Console::FG_RED); |
|
123
|
|
|
return false; |
|
124
|
|
|
} |
|
125
|
|
|
$this->stdout("Running migrations...\n", Console::FG_YELLOW); |
|
126
|
|
|
/** @var UpdateHelper $helper */ |
|
127
|
|
|
$helper = Yii::createObject([ |
|
128
|
|
|
'class' => UpdateHelper::className(), |
|
129
|
|
|
]); |
|
130
|
|
|
$process = $helper->applyAppMigrations( |
|
131
|
|
|
false |
|
132
|
|
|
); |
|
133
|
|
|
|
|
134
|
|
|
$process->run(); |
|
135
|
|
|
|
|
136
|
|
|
if (!$this->interactive && getenv("DP2_SKIP_ADMIN")) { |
|
137
|
|
|
return $this->finalStep(); |
|
138
|
|
|
} else { |
|
139
|
|
|
return $this->adminUser(); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function adminUser() |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$model = new AdminUser(); |
|
146
|
|
|
foreach ($model->attributes() as $attribute) { |
|
147
|
|
|
|
|
148
|
|
|
$model->setAttributes( |
|
149
|
|
|
[ |
|
150
|
|
|
$attribute => $this->prompt("-> $attribute", [ |
|
151
|
|
|
'required' => true, |
|
152
|
|
|
'default' => $model->$attribute, |
|
153
|
|
|
]), |
|
154
|
|
|
] |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
if (!$this->interactive) { |
|
159
|
|
|
$model->password = 'password'; |
|
160
|
|
|
} |
|
161
|
|
|
if ($model->validate()) { |
|
162
|
|
|
InstallerHelper::createAdminUser($model, $this->db()); |
|
163
|
|
|
return $this->finalStep(); |
|
164
|
|
|
} else { |
|
165
|
|
|
$this->stderr("Error in input data: ".var_export($model->errors, true), Console::FG_RED); |
|
166
|
|
|
return $this->adminUser(); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
private function finalStep() |
|
171
|
|
|
{ |
|
172
|
|
|
$model = new FinalStep(); |
|
173
|
|
|
Yii::setAlias('@webroot', Yii::getAlias('@app/web/')); |
|
174
|
|
|
foreach ($model->attributes() as $attribute) { |
|
175
|
|
|
if ($attribute !== 'useMemcached') { |
|
176
|
|
|
$model->setAttributes( |
|
177
|
|
|
[ |
|
178
|
|
|
$attribute => $this->prompt("-> $attribute", [ |
|
179
|
|
|
'required' => true, |
|
180
|
|
|
'default' => $model->$attribute, |
|
181
|
|
|
]), |
|
182
|
|
|
] |
|
183
|
|
|
); |
|
184
|
|
|
} else { |
|
185
|
|
|
$model->useMemcached = $this->confirm("Use memcached extension?", false); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
if (getenv('DP2_SERVER_NAME')) { |
|
190
|
|
|
$model->serverName = getenv('DP2_SERVER_NAME'); |
|
191
|
|
|
} |
|
192
|
|
|
if (getenv('DP2_SERVER_PORT')) { |
|
193
|
|
|
$model->serverPort = getenv('DP2_SERVER_PORT'); |
|
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
if (InstallerHelper::writeCommonConfig($model) && InstallerHelper::updateConfigurables()) { |
|
196
|
|
|
file_put_contents(Yii::getAlias('@app/installed.mark'), '1'); |
|
197
|
|
|
$this->stdout("Installation complete!\n", Console::FG_GREEN); |
|
198
|
|
|
} else { |
|
199
|
|
|
$this->stderr("Unable to write configs!\n", Console::FG_RED); |
|
200
|
|
|
} |
|
201
|
|
|
return 0; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @return \yii\db\Connection |
|
206
|
|
|
* @throws \yii\base\InvalidConfigException |
|
207
|
|
|
*/ |
|
208
|
|
View Code Duplication |
private function db() |
|
209
|
|
|
{ |
|
210
|
|
|
if ($this->db === null) { |
|
211
|
|
|
$config = InstallerHelper::createDatabaseConfig($this->getDbConfigFromSession()); |
|
212
|
|
|
$dbComponent = Yii::createObject( |
|
213
|
|
|
$config |
|
214
|
|
|
); |
|
215
|
|
|
$dbComponent->open(); |
|
216
|
|
|
$this->db = $dbComponent; |
|
217
|
|
|
} |
|
218
|
|
|
return $this->db; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
View Code Duplication |
private function getDbConfigFromSession() |
|
|
|
|
|
|
222
|
|
|
{ |
|
223
|
|
|
return Yii::$app->session->get('db-config', [ |
|
224
|
|
|
'db_host' => 'localhost', |
|
225
|
|
|
'db_name' => 'dotplant2', |
|
226
|
|
|
'username' => 'root', |
|
227
|
|
|
'password' => '', |
|
228
|
|
|
'enableSchemaCache' => true, |
|
229
|
|
|
'schemaCacheDuration' => 86400, |
|
230
|
|
|
'schemaCache' => 'cache', |
|
231
|
|
|
'connectionOk' => false, |
|
232
|
|
|
]); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.