|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bmitch\Envsync; |
|
4
|
|
|
|
|
5
|
|
|
use Bmitch\Envsync\Collectors\FileCollector; |
|
6
|
|
|
use Bmitch\Envsync\Finders\EnvironmentFinder; |
|
7
|
|
|
use Bmitch\Envsync\Builders\TableBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class SyncerCommand |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Creates a new instance of the SyncerCommand. |
|
14
|
|
|
* @param FileCollector $fileCollector File Collector. |
|
15
|
|
|
* @param EnvironmentFinder $envFinder Environment Variable Filder. |
|
16
|
|
|
* @param TableBuilder $tableBuilder Table Builder. |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->fileCollector = $fileCollector; |
|
|
|
|
|
|
21
|
|
|
$this->envFinder = $envFinder; |
|
|
|
|
|
|
22
|
|
|
$this->tableBuilder = $tableBuilder; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Runs the command. |
|
27
|
|
|
* Supressing this for now. |
|
28
|
|
|
* @SuppressWarnings(PHPMD.NPathComplexity) |
|
29
|
|
|
* @param array $arguments Command line arguments. |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
public function handle(array $arguments) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->handleArguments($arguments); |
|
35
|
|
|
|
|
36
|
|
|
$envData = $this->getEnvironmentVariables(); |
|
37
|
|
|
|
|
38
|
|
|
$results = $this->getResults($envData); |
|
39
|
|
|
|
|
40
|
|
|
$this->tableBuilder->outputTable($results); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Gets a list of the environment variables defined in the |
|
45
|
|
|
* source code, .env and .env.example file. |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getEnvironmentVariables() |
|
49
|
|
|
{ |
|
50
|
|
|
$files = $this->fileCollector |
|
51
|
|
|
->get('.php') |
|
52
|
|
|
->from($this->folder); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
$env = []; |
|
55
|
|
|
$env['source'] = $this->envFinder->getFromFiles($files); |
|
56
|
|
|
$env['example'] = $this->envFinder->getFromFile('.env.example'); |
|
57
|
|
|
$env['env'] = $this->envFinder->getFromFile('.env'); |
|
58
|
|
|
$env['all'] = $this->mergeEnvs($env); |
|
59
|
|
|
|
|
60
|
|
|
return $env; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Takes the list of environment variables defined and creates |
|
65
|
|
|
* a results array showing each variable and where it is or is not |
|
66
|
|
|
* defined. |
|
67
|
|
|
* @param array $envData Environment Variable Data. |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getResults(array $envData) |
|
71
|
|
|
{ |
|
72
|
|
|
$results = []; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($envData['all'] as $variable) { |
|
75
|
|
|
$results[] = [ |
|
76
|
|
|
'variable' => $variable, |
|
77
|
|
|
'insource' => in_array($variable, $envData['source']) ? 'Yes' : 'No', |
|
78
|
|
|
'inenvexample' => in_array($variable, $envData['example']) ? 'Yes' : 'No', |
|
79
|
|
|
'inenv' => in_array($variable, $envData['env']) ? 'Yes' : 'No', |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $results; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Looks through all the current env variables from all |
|
88
|
|
|
* sources and makes a master list of all of them. |
|
89
|
|
|
* @param array $currentEnvs Current Env Variables. |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function mergeEnvs(array $currentEnvs) |
|
93
|
|
|
{ |
|
94
|
|
|
$allEnvs = []; |
|
|
|
|
|
|
95
|
|
|
$allEnvs = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example'])); |
|
96
|
|
|
$allEnvs = array_unique(array_merge($allEnvs, $currentEnvs['source'])); |
|
97
|
|
|
|
|
98
|
|
|
return $allEnvs; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Collects the command line arguments. |
|
103
|
|
|
* @param array $arguments Command line arguments. |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function handleArguments(array $arguments) |
|
107
|
|
|
{ |
|
108
|
|
|
// Default value |
|
109
|
|
|
$this->folder = '.'; |
|
110
|
|
|
|
|
111
|
|
|
if (isset($arguments[1])) { |
|
112
|
|
|
$this->folder = $arguments[1]; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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: