Symfony - 生成新 APP_SECRET 的终端命令

Slo*_*wie 2 php symfony

我正在编写一个公共 Symfony 应用程序。所以我需要在Packagist上公开它。如何运行 post-install-cmd 来自动设置一个新的 random APP_SECRET

知道这一点会很高兴,我认为这很重要。我没有在互联网上找到任何关于它的信息。

ink*_*rot 7

您可以使用 symfony/控制台,

创建例如:

php bin/console make:command regenerate-app-secret
Run Code Online (Sandbox Code Playgroud)

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 Code Online (Sandbox Code Playgroud)

跑:

php bin/console regenerate-app-secret
Run Code Online (Sandbox Code Playgroud)