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 Jtant\LaravelEnvSync\SyncService; |
11
|
|
|
use Illuminate\Console\Command; |
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
|
|
|
* @var SyncService |
35
|
|
|
*/ |
36
|
|
|
private $sync; |
37
|
|
|
/** |
38
|
|
|
* @var WriterInterface |
39
|
|
|
*/ |
40
|
|
|
private $writer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new command instance. |
44
|
|
|
* |
45
|
|
|
* @param SyncService $sync |
46
|
|
|
*/ |
47
|
|
|
public function __construct(SyncService $sync, WriterInterface $writer) |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
$this->sync = $sync; |
51
|
|
|
$this->writer = $writer; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Execute the console command. |
56
|
|
|
* |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
public function handle() |
60
|
|
|
{ |
61
|
|
|
$first = base_path('.env.example'); |
62
|
|
|
$second = base_path('.env'); |
63
|
|
|
|
64
|
|
|
if ($this->option('reverse')) { |
65
|
|
|
$switch = $first; |
66
|
|
|
$first = $second; |
67
|
|
|
$second = $switch; |
68
|
|
|
unset($switch); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$diffs = $this->sync->getDiff($first, $second); |
72
|
|
|
|
73
|
|
|
foreach ($diffs as $key => $diff) { |
74
|
|
|
$question = sprintf("'%s' is not present into your %s file. It's default value is '%s'. Would you like to add it ? [y=yes/n=no/c=change default value]", $key, basename($second), $diff); |
75
|
|
|
$action = strtolower(trim($this->ask($question, self::YES))); |
76
|
|
|
if ($action == self::NO) { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($action == self::CHANGE) { |
81
|
|
|
$diff = $this->ask(sprintf("Please choose a value for '%s' :", $key, $diff)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->writer->append($second, $key, $diff); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->info($second . ' is now synced with ' . $first); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|