forecho /
yii2-rest-api
| 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
Bug
introduced
by
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 |