-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
341 lines (295 loc) · 8.7 KB
/
Config.php
File metadata and controls
341 lines (295 loc) · 8.7 KB
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
<?php
/**
* Class Config.
*
* Class holding the configuration from the command line input.
*/
class Config
{
/**
* @var AppOutput
*/
protected $output;
/**
* @var AppInput
*/
protected $input;
/**
* @var array Unprocessed command line parameters
*/
protected $parameters = [];
/**
* @var array Array of processed parameters(in the long and the short form)
*/
protected $processedParameters;
/**
* @var bool
*/
protected $displayHelp;
/**
* @var bool
*/
protected $generateXmlHeader;
/**
* @var string empty string or element name
*/
protected $rootElementName;
/**
* @var string
*/
protected $inputFileName;
/**
* @var string
*/
protected $outputFileName;
/**
* @var string
*/
protected $query;
/**
* Config constructor.
*
* @param array $parameters
*/
public function __construct(array $parameters)
{
$this->output = new AppOutput();
$this->input = new AppInput();
$this->parameters = $parameters;
$this->processedParameters = [];
$this->generateXmlHeader = true;
$this->rootElementName = '';
$this->outputFileName = '';
$this->inputFileName = '';
}
public function processParameters()
{
$queryFile = null;
if (in_array('--help', $this->parameters, true) || in_array('-h', $this->parameters, true)) {
//can not combine --help with any other parameter
if (count($this->parameters) !== 2) {
throw new ParametersException('Can not use --help with any other parameter');
}
$this->displayHelp = true;
return;
} else {
$this->displayHelp = false;
}
/* @noinspection CallableInLoopTerminationConditionInspection */
for ($i = 1; $i < count($this->parameters); ++$i) {
$actual = $this->parameters[$i];
if (strpos($actual, '--input=') === 0 || strpos($actual, '-i=') === 0) {
//check for multiple occurrence of the same semantic argument
if ($this->wasProcessed('--input') || $this->wasProcessed('-i')) {
throw new ParametersException('Cannot use input parameter twice');
}
$this->inputFileName = $this->getValueFromParameter($actual);
$this->processedParameters[] = '--input';
$this->processedParameters[] = '-i';
} elseif (strpos($actual, '--output=') === 0 || strpos($actual, '-o=') === 0) {
//check for multiple occurrence of the same semantic argument
if ($this->wasProcessed('--output') || $this->wasProcessed('-o')) {
throw new ParametersException('Cannot use output parameter twice');
}
$this->outputFileName = $this->getValueFromParameter($actual);
$this->processedParameters[] = '--output';
$this->processedParameters[] = '-o';
} elseif (strpos($actual, '--query=') === 0 || strpos($actual, '-q=') === 0) {
//check for multiple occurrence of the same semantic argument
if ($this->wasProcessed('--query') || $this->wasProcessed('-q') || $this->wasProcessed('--qf')) {
throw new ParametersException('Cannot use input query twice');
}
$this->query = $this->getValueFromParameter($actual);
$this->processedParameters[] = '--query';
$this->processedParameters[] = '-q';
} elseif (strpos($actual, '--qf=') === 0) {
if ($this->wasProcessed('--qf') || $this->wasProcessed('--query') || $this->wasProcessed('-q')) {
throw new ParametersException('Cannot use input parameter twice');
}
$this->processedParameters[] = '--qf';
$queryFile = $actual;
} elseif (strpos($actual, '-n') === 0) {
if ($this->wasProcessed('-n')) {
throw new ParametersException('Cannot use n parameter twice');
}
$this->generateXmlHeader = false;
} elseif (strpos($actual, '--root=') === 0 || strpos($actual, '-r=') === 0) {
//check for multiple occurrence of the same semantic argument
if ($this->wasProcessed('--root') || $this->wasProcessed('-r')) {
throw new ParametersException('Cannot use root parameter twice');
}
$this->processedParameters[] = '--root';
$this->processedParameters[] = '-r';
$this->rootElementName = $this->getValueFromParameter($actual);
} else {
throw new ParametersException('Unknown parameter: ' . $actual);
}
}
//delayed query file processing
if ($this->wasProcessed('--qf')) {
$this->getQueryFromFile($this->getValueFromParameter($queryFile));
}
if ($this->wasProcessed('--input') && !$this->input->fileExists($this->inputFileName)) {
throw new InputFileException('Can not open input file: ' . $this->inputFileName);
}
if (!($this->wasProcessed('--qf') || $this->wasProcessed('--query'))) {
throw new InvalidQueryException('No query specified');
}
}
/**
* @return bool
*/
public function getPrintHelp()
{
return $this->displayHelp;
}
/**
* @param bool $displayHelp
*/
public function setDisplayHelp($displayHelp)
{
$this->displayHelp = $displayHelp;
}
/**
* @return string
*/
public function getInputFileName()
{
return $this->inputFileName;
}
/**
* @param string $inputFileName
*/
public function setInputFileName($inputFileName)
{
$this->inputFileName = $inputFileName;
}
/**
* @return string
*/
public function getOutputFileName()
{
return $this->outputFileName;
}
/**
* @param string $outputFileName
*/
public function setOutputFileName($outputFileName)
{
$this->outputFileName = $outputFileName;
}
/**
* @return AppOutput
*/
public function getOutput()
{
return $this->output;
}
/**
* @param AppOutput $output
*/
public function setOutput($output)
{
$this->output = $output;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* @param array $parameters
*/
public function setParameters($parameters)
{
$this->parameters = $parameters;
}
/**
* @return array
*/
public function getProcessedParameters()
{
return $this->processedParameters;
}
/**
* @param array $processedParameters
*/
public function setProcessedParameters($processedParameters)
{
$this->processedParameters = $processedParameters;
}
/**
* @return bool
*/
public function isGenerateXmlHeader()
{
return $this->generateXmlHeader;
}
/**
* @param bool $generateXmlHeader
*/
public function setGenerateXmlHeader($generateXmlHeader)
{
$this->generateXmlHeader = $generateXmlHeader;
}
/**
* @return string
*/
public function getRootElementName()
{
return $this->rootElementName;
}
/**
* @param string $rootElementName
*/
public function setRootElementName($rootElementName)
{
$this->rootElementName = $rootElementName;
}
/**
* @return string
*/
public function getQuery()
{
return $this->query;
}
/**
* @param string $query
*/
public function setQuery($query)
{
$this->query = $query;
}
/**
* @param $parameter
*
* @return bool
*/
protected function wasProcessed($parameter)
{
return in_array($parameter, $this->processedParameters, true);
}
/**
* @param string $parameter
*
* @return string Value after the =
*/
protected function getValueFromParameter($parameter)
{
return substr($parameter, strpos($parameter, '=') + 1); //+1 character after the =
}
/**
* @param $fileName
*
* @return null|string
*/
protected function getQueryFromFile($fileName)
{
$this->query = $this->input->getFileContent($fileName);
if (!$this->query) {
throw new InvalidQueryException('Could not read a query from the file: '.$fileName);
}
}
}