|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmitch\Envsync; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
6
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
7
|
|
|
use Bmitch\Envsync\Collectors\FileCollector; |
|
8
|
|
|
use Bmitch\Envsync\Finders\EnvironmentFinder; |
|
9
|
|
|
|
|
10
|
|
|
class SyncerCommand |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Creates a new instance of the SyncerCommand. |
|
15
|
|
|
* @param FileCollector $fileCollector File Collector. |
|
16
|
|
|
* @param EnvironmentFinder $envFinder Environment Variable Filder. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->fileCollector = $fileCollector; |
|
|
|
|
|
|
21
|
|
|
$this->envFinder = $envFinder; |
|
|
|
|
|
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Runs the command. |
|
26
|
|
|
* Supressing this for now. |
|
27
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
28
|
|
|
* @param array $arguments Command line arguments. |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handle(array $arguments) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->handleArguments($arguments); |
|
34
|
|
|
|
|
35
|
|
|
$files = $this->fileCollector |
|
36
|
|
|
->get('.php') |
|
37
|
|
|
->from($this->folder); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
$env['source'] = $this->envFinder->getFromFiles($files); |
|
|
|
|
|
|
40
|
|
|
$env['example'] = $this->envFinder->getFromFile('.env.example'); |
|
41
|
|
|
$env['env'] = $this->envFinder->getFromFile('.env'); |
|
42
|
|
|
$env['all'] = $this->getAllEnvs($env); |
|
43
|
|
|
|
|
44
|
|
|
$results = []; |
|
45
|
|
|
|
|
46
|
|
|
foreach ($env['all'] as $variable) { |
|
47
|
|
|
$results[$variable] = []; |
|
48
|
|
|
$results[$variable]['inSource'] = in_array($variable, $env['source']) ? 'Yes' : 'No'; |
|
49
|
|
|
$results[$variable]['inSource'] = in_array($variable, $env['source']) ? 'Yes' : 'No'; |
|
50
|
|
|
$results[$variable]['inEnvExample'] = in_array($variable, $env['example']) ? 'Yes' : 'No'; |
|
51
|
|
|
$results[$variable]['inEnv'] = in_array($variable, $env['env']) ? 'Yes' : 'No'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$data = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach ($results as $variable => $result) { |
|
57
|
|
|
$data[] = [ |
|
58
|
|
|
'variable' => $variable, |
|
59
|
|
|
'insource' => $result['inSource'], |
|
60
|
|
|
'inenvexample' => $result['inEnvExample'], |
|
61
|
|
|
'inenv' => $result['inEnv'], |
|
62
|
|
|
]; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$headers = ['Variable', 'In Source', 'In .env.example', 'In .env']; |
|
66
|
|
|
|
|
67
|
|
|
$table = new Table(new ConsoleOutput); |
|
68
|
|
|
|
|
69
|
|
|
$table->setHeaders($headers)->setRows($data)->render(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Looks through all the current env variables from all |
|
74
|
|
|
* sources and makes a master list of all of them. |
|
75
|
|
|
* @param array $currentEnvs Current Env Variables. |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function getAllEnvs(array $currentEnvs) |
|
79
|
|
|
{ |
|
80
|
|
|
$allEnvs = []; |
|
|
|
|
|
|
81
|
|
|
$allEnvs = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example'])); |
|
82
|
|
|
$allEnvs = array_unique(array_merge($allEnvs, $currentEnvs['source'])); |
|
83
|
|
|
|
|
84
|
|
|
return $allEnvs; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Collects the command line arguments. |
|
89
|
|
|
* @param array $arguments Command line arguments. |
|
90
|
|
|
* @return void |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function handleArguments(array $arguments) |
|
93
|
|
|
{ |
|
94
|
|
|
// Default value |
|
95
|
|
|
$this->folder = '.'; |
|
96
|
|
|
|
|
97
|
|
|
if (isset($arguments[1])) { |
|
98
|
|
|
$this->folder = $arguments[1]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: