Files
2026-09-01 10:46:29 -03:00

42 lines
1.4 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class KeyGenerateCommand extends Command
{
protected static $defaultName = 'key:generate';
protected static $defaultDescription = 'Gera uma nova chave de aplicação (APP_KEY)';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$key = bin2hex(random_bytes(32));
$envFile = dirname(__DIR__, 3) . '/.env';
if (!file_exists($envFile)) {
$output->writeln("<error>Arquivo .env não encontrado.</error>");
return Command::FAILURE;
}
$content = file_get_contents($envFile);
if (strpos($content, 'APP_KEY=') !== false) {
$content = preg_replace('/APP_KEY=.*/', 'APP_KEY=' . $key, $content);
} else {
$content .= "\nAPP_KEY=" . $key;
}
if (file_put_contents($envFile, $content)) {
$output->writeln("<info>APP_KEY gerada com sucesso!</info>");
$output->writeln("<comment>Chave:</comment> {$key}");
return Command::SUCCESS;
} else {
$output->writeln("<error>Falha ao escrever no arquivo .env.</error>");
return Command::FAILURE;
}
}
}