|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Laravel-Env-Sync |
|
4
|
|
|
* |
|
5
|
|
|
* @author Julien Tant - Craftyx <[email protected]> |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Jtant\LaravelEnvSync\Console; |
|
9
|
|
|
|
|
10
|
|
|
use Illuminate\Console\Command; |
|
11
|
|
|
use Jtant\LaravelEnvSync\SyncService; |
|
12
|
|
|
use Jtant\LaravelEnvSync\Writer\WriterInterface; |
|
13
|
|
|
|
|
14
|
|
|
class SyncCommand extends Command |
|
15
|
|
|
{ |
|
16
|
|
|
const YES = 'y'; |
|
17
|
|
|
const NO = 'n'; |
|
18
|
|
|
const CHANGE = 'c'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The name and signature of the console command. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $signature = 'env:sync {--reverse}'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The console command description. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $description = 'Synchronise the .env & .env.example files'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var SyncService |
|
36
|
|
|
*/ |
|
37
|
|
|
private $sync; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var WriterInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $writer; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new command instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @param SyncService $sync |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function __construct(SyncService $sync, WriterInterface $writer) |
|
50
|
|
|
{ |
|
51
|
3 |
|
parent::__construct(); |
|
52
|
3 |
|
$this->sync = $sync; |
|
53
|
3 |
|
$this->writer = $writer; |
|
54
|
3 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Execute the console command. |
|
58
|
|
|
* |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function handle() |
|
62
|
|
|
{ |
|
63
|
1 |
|
$first = base_path('.env.example'); |
|
64
|
1 |
|
$second = base_path('.env'); |
|
65
|
|
|
|
|
66
|
1 |
|
if ($this->option('reverse')) { |
|
67
|
|
|
$switch = $first; |
|
68
|
|
|
$first = $second; |
|
69
|
|
|
$second = $switch; |
|
70
|
|
|
unset($switch); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
$forceCopy = $this->option('no-interaction'); |
|
74
|
1 |
|
if ($forceCopy) { |
|
75
|
1 |
|
$this->warn('--no-interaction flag detected - will copy all new keys'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
1 |
|
$diffs = $this->sync->getDiff($first, $second); |
|
80
|
|
|
|
|
81
|
1 |
|
foreach ($diffs as $key => $diff) { |
|
82
|
1 |
|
$action = self::YES; |
|
83
|
1 |
|
if (!$forceCopy) { |
|
84
|
|
|
$question = sprintf("'%s' is not present into your %s file. Its default value is '%s'. Would you like to add it ? [y=yes/n=no/c=change default value]", $key, basename($second), $diff); |
|
85
|
|
|
$action = $this->choice($question, [ |
|
86
|
|
|
self::YES => 'Copy the default value', |
|
87
|
|
|
self::CHANGE => 'Change the default value', |
|
88
|
|
|
self::NO => 'Skip' |
|
89
|
|
|
], self::YES); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
if ($action == self::NO) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
if ($action == self::CHANGE) { |
|
97
|
|
|
$diff = $this->ask(sprintf("Please choose a value for '%s' :", $key, $diff)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
1 |
|
$this->writer->append($second, $key, $diff); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$this->info($second . ' is now synced with ' . $first); |
|
104
|
1 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|