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 BaseCommand |
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} {--src=} {--dest=}'; |
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
|
7 |
|
public function __construct(SyncService $sync, WriterInterface $writer) |
50
|
|
|
{ |
51
|
7 |
|
parent::__construct(); |
52
|
7 |
|
$this->sync = $sync; |
53
|
7 |
|
$this->writer = $writer; |
54
|
7 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Execute the console command. |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
3 |
|
public function handle() |
62
|
|
|
{ |
63
|
3 |
|
list($src, $dest) = $this->getSrcAndDest(); |
64
|
|
|
|
65
|
|
|
|
66
|
3 |
|
if ($this->option('reverse')) { |
67
|
1 |
|
$switch = $src; |
68
|
1 |
|
$src = $dest; |
69
|
1 |
|
$dest = $switch; |
70
|
1 |
|
unset($switch); |
71
|
|
|
} |
72
|
|
|
|
73
|
3 |
|
$forceCopy = $this->option('no-interaction'); |
74
|
3 |
|
if ($forceCopy) { |
75
|
3 |
|
$this->warn('--no-interaction flag detected - will copy all new keys'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
3 |
|
$diffs = $this->sync->getDiff($src, $dest); |
80
|
|
|
|
81
|
3 |
|
foreach ($diffs as $key => $diff) { |
82
|
3 |
|
$action = self::YES; |
83
|
3 |
|
if (!$forceCopy) { |
84
|
|
|
$question = sprintf("'%s' is not present into your %s file. Its default value is '%s'. Would you like to add it?", $key, basename($dest), $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
|
3 |
|
if ($action == self::NO) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
if ($action == self::CHANGE) { |
97
|
|
|
$diff = $this->output->ask(sprintf("Please choose a value for '%s'", $key, $diff), null, function ($value) { |
98
|
|
|
return $value; |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->writer->append($dest, $key, $diff); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->info($dest . ' is now synced with ' . $src . '.'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|