Symfony - Терминальная команда для генерации нового APP_SECRET - PullRequest
0 голосов
/ 24 марта 2020

Я пишу приложение c Symfony. Поэтому мне нужно выставить это на Packagist. Как запустить post-install-cmd для автоматической установки нового случайного значения APP_SECRET?

Это было бы приятно узнать, и я думаю, что это важно. Я не нашел ничего об этом в inte rnet.

1 Ответ

1 голос
/ 25 марта 2020

Вы можете использовать symfony / console,

Создать, например:

php bin/console make:command regenerate-app-secret

in src/Command/RegenerateAppSecretCommand.php

<?php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class RegenerateAppSecretCommand extends Command
{
    protected static $defaultName = 'regenerate-app-secret';

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        $a = '0123456789abcdef';
        $secret = '';
        for ($i = 0; $i < 32; $i++) {
            $secret .= $a[rand(0, 15)];
        }

        $r = shell_exec('sed -i -E "s/^APP_SECRET=.{32}$/APP_SECRET=' . $secret . '/" .env');

        $io->success('New APP_SECRET was generated: ' . $secret);

        return 0;
    }
}

Run:

php bin/console regenerate-app-secret
...