Skip to content

Commit

Permalink
feat: add key:generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Aug 23, 2023
1 parent feaa40d commit ac73b86
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/Command/KeyGenerateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Aloe\Command;

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 function configure()
{
$this
->setHelp('Run your frontend dev command')
->setDescription('Run your frontend dev server');
}

protected function generateKey()
{
return 'base64:' . base64_encode(random_bytes(32));
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$directory = getcwd();
$env = file_get_contents("$directory/.env");

if (strpos($env, 'APP_KEY') !== false) {
$output->writeln("<info>APP_KEY already exists. Regenerating APP_KEY</info>");
$env = preg_replace('/APP_KEY=(.*)/', "APP_KEY={$this->generateKey()}", $env);
} else {
$env = "APP_KEY={$this->generateKey()}\n$env";
}


file_put_contents("$directory/.env", $env);

$output->writeln("<info>APP_KEY generated successfully.</info>");

return 0;
}
}
1 change: 1 addition & 0 deletions src/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function commands()

// Aloe Commands
\Aloe\Command\DevToolsCommand::class,
\Aloe\Command\KeyGenerateCommand::class,

// auth Commands
\Aloe\Command\AuthScaffoldCommand::class,
Expand Down

0 comments on commit ac73b86

Please sign in to comment.