forked from doppar/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyGenerateCommand.php
More file actions
57 lines (47 loc) · 1.47 KB
/
KeyGenerateCommand.php
File metadata and controls
57 lines (47 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Phaseolies\Console\Commands;
use Phaseolies\Console\Schedule\Command;
use RuntimeException;
class KeyGenerateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'key:generate';
/**
* The description of the console command.
*
* @var string
*/
protected $description = 'Generate a new application key and set it in the .env file';
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
return $this->executeWithTiming(function() {
$randomKey = base64_encode(random_bytes(32));
$envPath = base_path() . '/.env';
if (!file_exists($envPath)) {
throw new RuntimeException('.env file not found!');
}
$envContent = file_get_contents($envPath);
$newEnvContent = preg_replace(
'/^APP_KEY=.*$/m',
"APP_KEY=base64:$randomKey",
$envContent
);
if (file_put_contents($envPath, $newEnvContent) === false) {
throw new RuntimeException('Failed to update .env file.');
}
$this->displaySuccess('Application key set successfully');
$this->line("<fg=yellow>🔑 Key:</> <fg=white>base64:$randomKey</>");
return Command::SUCCESS;
});
return Command::SUCCESS;
}
}