Issues (18)

commands/GenerateController.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\console\Controller;
7
use yii\helpers\Console;
8
use yiier\helpers\Security;
9
use yiithings\dotenv\Loader;
10
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), '/');
0 ignored issues
show
Are you sure env($item) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
            $escaped = preg_quote('=' . /** @scrutinizer ignore-type */ env($item), '/');
Loading history...
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
        }
28
    }
29
}
30