|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\commands; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\InvalidConfigException; |
|
7
|
|
|
use yii\console\{Controller, ExitCode}; |
|
8
|
|
|
use yii\helpers\{Console, ArrayHelper}; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Command for create local config |
|
12
|
|
|
*/ |
|
13
|
|
|
class CreateLocalConfigController extends Controller |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string MySQL host |
|
17
|
|
|
*/ |
|
18
|
|
|
public $MYSQL_HOST; |
|
19
|
|
|
/** |
|
20
|
1 |
|
* @var string MySQL database |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public $MYSQL_DATABASE; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string MySQL user |
|
25
|
2 |
|
*/ |
|
26
|
|
|
public $MYSQL_USER; |
|
27
|
2 |
|
/** |
|
28
|
2 |
|
* @var string MySQL password |
|
29
|
1 |
|
*/ |
|
30
|
|
|
public $MYSQL_PASSWORD; |
|
31
|
|
|
/** |
|
32
|
1 |
|
* @var string Config file path |
|
33
|
|
|
*/ |
|
34
|
|
|
public $path; |
|
35
|
1 |
|
|
|
36
|
|
|
public function options($actionId = '') |
|
37
|
1 |
|
{ |
|
38
|
1 |
|
return [ |
|
39
|
|
|
'path', |
|
40
|
1 |
|
'MYSQL_HOST', |
|
41
|
1 |
|
'MYSQL_DATABASE', |
|
42
|
1 |
|
'MYSQL_USER', |
|
43
|
|
|
'MYSQL_PASSWORD', |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
1 |
|
|
|
47
|
|
|
public function beforeAction($action) |
|
48
|
|
|
{ |
|
49
|
|
|
if (parent::beforeAction($action)) { |
|
50
|
|
|
if (empty($this->path)) { |
|
51
|
|
|
throw new InvalidConfigException('`path` should be specified'); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function actionIndex() |
|
58
|
|
|
{ |
|
59
|
|
|
$source = Yii::getAlias('@app/config/local/main.dist'); |
|
60
|
|
|
$dist = Yii::getAlias($this->path); |
|
61
|
|
|
|
|
62
|
|
|
if (!file_exists($dist)) { |
|
63
|
|
|
copy($source, $dist); |
|
64
|
|
|
$this->specifySettings($dist); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$this->stdout("Created successfully!\n", Console::FG_GREEN); |
|
67
|
|
|
} else { |
|
68
|
|
|
$this->stdout("Config file is exist!\n", Console::FG_RED); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return ExitCode::OK; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private function specifySettings(string $file) |
|
75
|
|
|
{ |
|
76
|
|
|
$contents = file_get_contents($file); |
|
77
|
|
|
$settings = $this->getSettings(); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($settings as $var => $value) { |
|
80
|
|
|
$contents = str_replace('%' . $var . '%', $value, $contents); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
file_put_contents($file, $contents); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function getSettings() |
|
87
|
|
|
{ |
|
88
|
|
|
$envFile = Yii::getAlias('@app/.env'); |
|
89
|
|
|
if (file_exists($envFile)) { |
|
90
|
|
|
return parse_ini_file($envFile); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->getOptionValues(''); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.