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
|
|
|
* Will be returned when program exits. |
14
|
|
|
* @var integer |
15
|
|
|
*/ |
16
|
|
|
protected $exitCode = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The mode the program is running in. |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $mode; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Instance of the FileCollector class. |
26
|
|
|
* @var FileCollector |
27
|
|
|
*/ |
28
|
|
|
protected $fileCollector; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Instance of the EnvironmentFilder class. |
32
|
|
|
* @var EnvironmentFinder |
33
|
|
|
*/ |
34
|
|
|
protected $envFinder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Instance of the TableBuilder class. |
38
|
|
|
* @var TableBuilder |
39
|
|
|
*/ |
40
|
|
|
protected $tableBuilder; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The source colde folder that will be inspected. |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $folder; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a new instance of the SyncerCommand. |
50
|
|
|
* @param FileCollector $fileCollector File Collector. |
51
|
|
|
* @param EnvironmentFinder $envFinder Environment Variable Filder. |
52
|
|
|
* @param TableBuilder $tableBuilder Table Builder. |
53
|
|
|
*/ |
54
|
|
|
public function __construct(FileCollector $fileCollector, EnvironmentFinder $envFinder, TableBuilder $tableBuilder) |
55
|
|
|
{ |
56
|
|
|
$this->fileCollector = $fileCollector; |
57
|
|
|
$this->envFinder = $envFinder; |
58
|
|
|
$this->tableBuilder = $tableBuilder; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Runs the command. |
63
|
|
|
* @param array $arguments Command line arguments. |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function handle(array $arguments) |
67
|
|
|
{ |
68
|
|
|
if (! $this->argumentsValid($arguments)) { |
69
|
|
|
echo $this->error; |
|
|
|
|
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
print "\n\nEnvSyncer Report - https://github.com/bmitch/envsync\n"; |
74
|
|
|
|
75
|
|
|
$envData = $this->getEnvironmentVariables(); |
76
|
|
|
|
77
|
|
|
$results = $this->getResults($envData); |
78
|
|
|
|
79
|
|
|
if ($this->mode == 'ci') { |
80
|
|
|
$this->checkForCiBuild($results); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($this->mode == 'deploy') { |
84
|
|
|
$this->checkForDeploy($results); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->tableBuilder->outputTable($results, $this->mode); |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
exit($this->exitCode); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets a list of the environment variables defined in the |
95
|
|
|
* source code, .env and .env.example file. |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function getEnvironmentVariables() |
99
|
|
|
{ |
100
|
|
|
$files = $this->fileCollector |
101
|
|
|
->get('.php') |
102
|
|
|
->from($this->folder); |
103
|
|
|
|
104
|
|
|
$env = []; |
105
|
|
|
$env['source'] = $this->envFinder->getFromFiles($files); |
106
|
|
|
$env['example'] = $this->envFinder->getFromFile('.env.example'); |
107
|
|
|
$env['env'] = $this->envFinder->getFromFile('.env'); |
108
|
|
|
$env['all'] = $this->mergeEnvs($env); |
109
|
|
|
|
110
|
|
|
return $env; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Takes the list of environment variables defined and creates |
115
|
|
|
* a results array showing each variable and where it is or is not |
116
|
|
|
* defined. |
117
|
|
|
* @param array $envData Environment Variable Data. |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
|
protected function getResults(array $envData) |
121
|
|
|
{ |
122
|
|
|
$results = []; |
123
|
|
|
|
124
|
|
|
foreach ($envData['all'] as $variable) { |
125
|
|
|
$results[] = [ |
126
|
|
|
'variable' => $variable, |
127
|
|
|
'insource' => in_array($variable, $envData['source']) ? 'Yes' : 'No', |
128
|
|
|
'inenvexample' => in_array($variable, $envData['example']) ? 'Yes' : 'No', |
129
|
|
|
'inenv' => in_array($variable, $envData['env']) ? 'Yes' : 'No', |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $results; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Looks through all the current env variables from all |
138
|
|
|
* sources and makes a master list of all of them. |
139
|
|
|
* @param array $currentEnvs Current Env Variables. |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
protected function mergeEnvs(array $currentEnvs) |
143
|
|
|
{ |
144
|
|
|
$allEnvs = []; |
|
|
|
|
145
|
|
|
$allEnvs = array_unique(array_merge($currentEnvs['env'], $currentEnvs['example'])); |
146
|
|
|
$allEnvs = array_unique(array_merge($allEnvs, $currentEnvs['source'])); |
147
|
|
|
|
148
|
|
|
return $allEnvs; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* If a variable is defined in the source code but |
153
|
|
|
* not defined in the .env.example file we write |
154
|
|
|
* to the message and set response to 1 |
155
|
|
|
* @param array $results Environment Variable Results. |
156
|
|
|
* @return void |
157
|
|
|
*/ |
158
|
|
View Code Duplication |
protected function checkForCiBuild(array $results) |
|
|
|
|
159
|
|
|
{ |
160
|
|
|
foreach ($results as $row) { |
161
|
|
|
if ($row['insource'] == 'Yes' && $row['inenvexample'] == 'No') { |
162
|
|
|
$this->exitCode = 1; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* If a variable is defined in the source code but |
169
|
|
|
* not defined in the .env file we write |
170
|
|
|
* to the message and set response to 1 |
171
|
|
|
* @param array $results Environment Variable Results. |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
View Code Duplication |
protected function checkForDeploy(array $results) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
foreach ($results as $row) { |
177
|
|
|
if ($row['insource'] == 'Yes' && $row['inenv'] == 'No') { |
178
|
|
|
$this->exitCode = 1; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Collects the command line arguments. |
185
|
|
|
* @param array $arguments Command line arguments. |
186
|
|
|
* @return boolean |
187
|
|
|
*/ |
188
|
|
|
protected function argumentsValid(array $arguments) |
189
|
|
|
{ |
190
|
|
|
// Default value |
191
|
|
|
$this->folder = '.'; |
192
|
|
|
|
193
|
|
|
if (isset($arguments[1])) { |
194
|
|
|
$this->folder = $arguments[1]; |
195
|
|
|
if (! file_exists($this->folder)) { |
196
|
|
|
$this->error = "Error: Folder {$this->folder} does not exist."; |
197
|
|
|
return false; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (isset($arguments[2])) { |
202
|
|
|
$this->mode = strtolower($arguments[2]); |
203
|
|
|
if (!in_array($this->mode, ['ci', 'deploy', 'default'])) { |
204
|
|
|
$this->error = "Error: Invalid argument {$this->mode}"; |
205
|
|
|
return false; |
206
|
|
|
} |
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
$this->mode = 'default'; |
211
|
|
|
|
212
|
|
|
return true; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
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: