1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Propaganistas\LaravelFakeId\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Jenssegers\Optimus\Energon; |
7
|
|
|
|
8
|
|
|
class FakeIdSetupCommand extends Command |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The name and signature of the console command. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $signature = 'fakeid:setup |
16
|
|
|
{--o|overwrite : Silently overwrite existing configuration} |
17
|
|
|
{--p|preserve : Silently preserves existing configuration}'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The console command description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = 'Configures FakeId for use'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Execute the console command. |
28
|
|
|
* |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function fire() |
32
|
|
|
{ |
33
|
|
|
// Write in environment file. |
34
|
|
|
$path = base_path('.env'); |
35
|
|
|
$env = file($path); |
36
|
|
|
|
37
|
|
|
// Detect existing configuration. |
38
|
|
|
if ($this->hasExistingConfiguration($env)) { |
39
|
|
|
|
40
|
|
|
if ($this->option('preserve')) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if (! $this->option('overwrite')) { |
45
|
|
|
if (! $this->confirm("Overwrite existing configuration?")) { |
46
|
|
|
return; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$this->removeExistingConfiguration($env); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->writeNewConfiguration($env, $path); |
54
|
|
|
$this->info("FakeId configured correctly."); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Checks if the given file array contains existing FakeId configuration. |
59
|
|
|
*/ |
60
|
|
|
protected function hasExistingConfiguration($file) |
61
|
|
|
{ |
62
|
|
|
return str_contains(implode(' ', $file), 'FAKEID_'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Removes existing FakeId configuration from the given file array. |
67
|
|
|
* |
68
|
|
|
* @param array $file |
69
|
|
|
*/ |
70
|
|
|
protected function removeExistingConfiguration(&$file) |
71
|
|
|
{ |
72
|
|
|
foreach ($file as $k => $line) { |
73
|
|
|
if (strpos($line, 'FAKEID_') === 0) { |
74
|
|
|
unset($file[$k]); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Writes new configuration using the provided file array to the given path. |
81
|
|
|
* |
82
|
|
|
* @param array $file |
83
|
|
|
* @param string $path |
84
|
|
|
*/ |
85
|
|
|
protected function writeNewConfiguration($file, $path) |
86
|
|
|
{ |
87
|
|
|
list($prime, $inverse, $rand) = Energon::generate(); |
88
|
|
|
|
89
|
|
|
$file[] = "\nFAKEID_PRIME=" . $prime; |
90
|
|
|
$file[] = "\nFAKEID_INVERSE=" . $inverse; |
91
|
|
|
$file[] = "\nFAKEID_RANDOM=" . $rand; |
92
|
|
|
|
93
|
|
|
file_put_contents($path, implode('', $file), LOCK_EX); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|