/
ArgumentValidator.php
357 lines (306 loc) · 9.64 KB
/
ArgumentValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/**
* Validate command line arguments
*
* PHP version 5.4
*
* @category [cv-pls]
* @package Build
* @author Chris Wright <info@daverandom.com>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 1.0.0
*/
namespace CvPls\Build;
/**
* Validate command line arguments
*
* @category [cv-pls]
* @package Build
* @author Chris Wright <info@daverandom.com>
*/
class ArgumentValidator
{
/**
* @var \CvPls\Build\ArgvParser Command line argument parser
*/
private $argvParser;
/**
* @var \CvPls\Build\ArgumentsFactory Arguments factory object
*/
private $argumentsFactory;
/**
* @var \CvPls\Build\Loggable Logging object
*/
private $logger;
/**
* Constructor
*
* @param \CvPls\Build\ArgvParser $argv Command line argument parser
*/
public function __construct(ArgvParser $argvParser, ArgumentsFactory $argumentsFactory, Loggable $logger = null)
{
$this->setArgvParser($argvParser);
$this->setArgumentsFactory($argumentsFactory);
if (isset($logger)) {
$this->setLogger($logger);
}
}
/**
* Log a message
*
* @param string $message Message to log
*/
private function log($message)
{
if (isset($this->logger)) {
$this->logger->log($message);
}
}
/**
* Test that a file can be opened successfully in read/write mode
*
* @param string $path Path to test
*
* @return bool Whether the file can be opened in read/write mode
*/
private function openAndTruncateFile($path)
{
if (!$fp = @fopen($path, 'w+')) {
return false;
}
@fclose($fp);
return true;
}
/**
* Get the target platform from the raw arguments
*
* @return string The target platform identifier
*
* @throws \LogicException When no platform or more than one platform is specified
*/
private function getPlatform()
{
$this->log('Detecting platform...');
if ($this->argvParser->hasFlag(['chrome'])) {
$platform = 'chrome';
}
if ($this->argvParser->hasFlag(['mozilla'])) {
if (isset($platform)) {
throw new \LogicException('You must specify only one target platform');
}
$platform = 'mozilla';
}
if (!isset($platform)) {
throw new \LogicException('You must specify a target platform with the --chrome or --mozilla flags');
}
$this->log('Platform detected: '.$platform);
return $platform;
}
/**
* Get the path to the private key file from the raw arguments
*
* @return string The path to the private key file
*
* @throws \LogicException When no path was specified, or the path is not valid
*/
private function getKeyFile()
{
$this->log('Validating private key file...');
if (!$keyFile = current($this->argvParser->getArg(['k', 'key']))) {
throw new \LogicException('No private key specified, you must use the -k/--key option');
}
if (!is_file($keyFile) || !is_readable($keyFile)) {
throw new \LogicException('The specified private key file does not exist or not readable');
}
$this->log('Private key file detected successfully');
return $keyFile;
}
/**
* Get the path to the package source base directory
*
* @return string The path to the package source base directory
*
* @throws \LogicException When the path is not valid
*/
private function getBaseDir()
{
$this->log('Locating package source base directory...');
$baseDir = current($this->argvParser->getArg(['d', 'base-dir'], getcwd()));
if (!is_dir($baseDir) || !is_readable($baseDir)) {
throw new \LogicException('The specified base directory does not exist or not readable');
}
$this->log('Base directory detected successfully');
return $baseDir;
}
/**
* Get the package version to use
*
* @return string|null The package version to use
*/
private function getVersion()
{
$this->log('Detecting package version...');
$version = current($this->argvParser->getArg(['v', 'version']));
if ($version !== false) {
$this->log('Package version detected successfully: ' . $version);
} else {
$version = null;
$this->log('Package version will be read from package manifest');
}
return $version;
}
/**
* Get the path for the output package binary
*
* @param string $version The package version
*
* @return string|null The path for the output package binary
*/
private function getOutputFilePath($version)
{
$this->log('Detecting output file path...');
$outFile = current($this->argvParser->getArg(['o', 'out-file']));
if ($outFile !== false) {
if (file_exists($outFile) && !$this->argvParser->hasFlag(['f', 'force'])) {
throw new \LogicException('The specified output file already exists, use the -f/--force option to overwrite');
} else if (!$this->openAndTruncateFile($outFile)) {
throw new \LogicException('Unable to open output file in read/write mode');
}
$this->log('Output file path detected successfully: ' . $outFile);
} else {
$outFile = null;
$this->log('Output file path will be derived from package version.');
}
return $outFile;
}
/**
* Get the path for the manifest file
*
* @return string|null The path for the manifest file
*/
private function getManifestFilePath()
{
if ($this->argvParser->hasFlag(['n', 'no-manifest'])) {
return false;
}
$this->log('Detecting manifest file path...');
$manifestFile = current($this->argvParser->getArg(['m', 'manifest']));
if ($manifestFile !== false) {
if (!$this->openAndTruncateFile($manifestFile)) {
throw new \LogicException('Unable to open manifest file in read/write mode');
}
$this->log('Manifest file path detected successfully: ' . $manifestFile);
} else {
$manifestFile = null;
$this->log('Manifest file path will be derived from platform.');
}
return $manifestFile;
}
/**
* Get the package URL for the manifest file
*
* @param string $manifestFile The path for the manifest file
*
* @return string|null The package URL for the manifest file
*/
private function getUrl($manifestFile)
{
if ($manifestFile === false) {
return;
}
$this->log('Detecting URL for manifest file...');
$url = current($this->argvParser->getArg(['u', 'url']));
if ($url === false) {
throw new \LogicException('Unable to detect manifest URL');
}
$this->log('URL detected successfully: ' . $url);
return $url;
}
/**
* Get the full URL of the update manifest file
*
* @return string|null The full URL of the update manifest file
*/
private function getUpdateUrl()
{
$this->log('Detecting URL of update manifest file...');
$url = current($this->argvParser->getArg(['p', 'updateurl'])) ?: null;
if ($url === null) {
$this->log('Update URL will be determined from install manifest');
} else {
$this->log('URL detected successfully: ' . $url);
}
return $url;
}
/**
* Get the argv parser
*
* @return \CvPls\Build\ArgvParser Command line argument parser
*/
public function getArgvParser()
{
return $this->argvParser;
}
/**
* Set the argv parser
*
* @param \CvPls\Build\ArgvParser $argvParser Command line argument parser
*/
public function setArgvParser(ArgvParser $argvParser)
{
$this->argvParser = $argvParser;
}
/**
* Get the arguments factory object
*
* @return \CvPls\Build\ArgumentsFactory Arguments factory object
*/
public function getArgumentsFactory()
{
return $this->argumentsFactory;
}
/**
* Set the arguments factory object
*
* @param \CvPls\Build\ArgumentsFactory $argumentsFactory Arguments factory object
*/
public function setArgumentsFactory(ArgumentsFactory $argumentsFactory)
{
$this->argumentsFactory = $argumentsFactory;
}
/**
* Get the logger
*
* @return \CvPls\Build\Loggable The logger
*/
public function getLogger()
{
return $this->logger;
}
/**
* Set the logger
*
* @param \CvPls\Build\Loggable $logger The logger
*/
public function setLogger(Loggable $logger)
{
$this->logger = $logger;
}
/**
* Validate command line arguments
*
* @return \CvPls\Build\Arguments The validated arguments
*/
public function validate()
{
$platform = $this->getPlatform();
$keyFile = $this->getKeyFile();
$baseDir = $this->getBaseDir();
$version = $this->getVersion();
$outFile = $this->getOutputFilePath($version);
$manifestFile = $this->getManifestFilePath();
$url = $this->getUrl($manifestFile);
$updateUrl = $this->getUpdateUrl();
return $this->argumentsFactory->create($platform, $keyFile, $baseDir, $version, $outFile, $manifestFile, $url, $updateUrl);
}
}