Total Complexity | 2 |
Total Lines | 16 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
11 | class GenerateController extends Controller |
||
12 | { |
||
13 | public function actionKey(string $filename = '.env') |
||
14 | { |
||
15 | $environmentFilePath = Yii::getAlias('@app/' . $filename); |
||
16 | Loader::load('', $filename, true); |
||
17 | foreach (['COOKIE_VALIDATION_KEY', 'JWT_SECRET'] as $item) { |
||
18 | $escaped = preg_quote('=' . env($item), '/'); |
||
|
|||
19 | $keyReplacementPattern = "/^{$item}{$escaped}/m"; |
||
20 | $key = Security::generateRealUniqId(32); |
||
21 | file_put_contents($environmentFilePath, preg_replace( |
||
22 | $keyReplacementPattern, |
||
23 | "{$item}={$key}", |
||
24 | file_get_contents($environmentFilePath) |
||
25 | )); |
||
26 | $this->stdout("{$item} key [{$key}] set successfully.\n", Console::FG_GREEN); |
||
27 | } |
||
30 |