-
Notifications
You must be signed in to change notification settings - Fork 7
/
PhpCli.class.php
executable file
·398 lines (343 loc) · 12.1 KB
/
PhpCli.class.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<?php
/**
* Copyright 2012 Jason Bradley
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
//Colors class does foreground/background colors for linux cli
require_once dirname(__FILE__).'/lib/Colors.class.php';
require_once dirname(__FILE__).'/lib/ProgressBar.class.php';
require_once dirname(__FILE__).'/PhpCliUtil.class.php';
class PhpCli extends PhpCliUtil
{
/**
* List of options the script accepts
*
* @var Array
*/
protected $options = array();
/**
* Description of what the script should do
*
* @var String
*/
protected $description = '';
/**
* An array of arguments called from command line
*
* @var Array
*/
protected $arguments = array();
/**
* Instance of Color
*
* @var Color
*/
protected $color = null;
/**
* @param array $arguments
* @param array $options array([0] = Option, [1] = Description, [2] = Required)
* @param string Script Description
* @throws PhpCliException
*/
public function __construct($arguments, $options = array(), $description = '')
{
$this->obtainLock($_SERVER['SCRIPT_FILENAME']);
$this->setDescription($description);
$this->addOptions($options);
$this->setArguments($arguments);
$this->color = new Colors();
//display help
if ($this->hasArg('h'))
{
$this->showHelp();
}
}
protected function setOptions(Array $options)
{
$this->options = $options;
}
protected function setDescription($description)
{
$this->description = $description;
}
protected function setArguments($arguments)
{
if (is_array($arguments))
{
$this->arguments = $arguments;
//validate that the arguments passed in match what options are available
if (!$this->hasArg('h') && !$this->hasValidArguments())
{
throw new PhpCliException("Invalid arguments provided. Run with -h to see available arguments.");
}
}
else
{
throw new PhpCliException("Arguments should be an array.");
}
}
public function getOptions()
{
return $this->options;
}
public function getDescription()
{
return $this->description;
}
protected function hasValidArgumentRegExpression($regularExpression, $value) {
return (preg_match('/' . $regularExpression . '/', $value) == 1);
}
/**
* Determines if the arguments passed in match
* what was expected in the list of options
*
* @returns boolean
*/
public function hasValidArguments()
{
if (!is_array($this->arguments))
{
return false;
}
else
{
//did we get all the required arguments?
foreach ($this->options as $option)
{
if ((boolean)$option[2] === true)
{
if (!$this->hasArg($option[0]))
{
return false;
}
}
//check regular expression
if (isset($option[3])) {
if (!$this->hasValidArgumentRegExpression($option[3], $this->getArgValue($option[0]))) {
return false;
}
}
}
//look for extra arguments
foreach ($this->arguments as $index => $argument)
{
if ($index > 0) //0 is the script
{
if ($this->hasArg($argument) === false)
{
return false;
}
}
}
return true;
}
}
protected function addHelpOption(&$options)
{
array_push($options, array('h','Displays Help',false));
}
protected function addVerboseOption(&$options)
{
array_push($options, array('v','Show Verbose Messages',false));
}
/**
* Add the options for the script
*
* @param array $options array([0] = Option, [1] = Description, [2] = Required)
* @throws PhpCliException
*/
protected function addOptions(Array $options)
{
//h should always be an option
$this->addHelpOption($options);
//v should always be an option
$this->addVerboseOption($options);
foreach ($options as $option)
{
if (!is_array($option) || count($option) == 1)
{
throw new PhpCliException("Options are not valid.");
}
}
$this->setOptions($options);
}
/**
* Adds a description of the script to display
* in PhpCli::showHelp()
*
* @param type $description
*/
public function addDescription($description)
{
$description = ($description == NULL || !is_string($description)) ? '' : $description;
$this->setDescription($description);
}
/**
* Does the argument exist?
*
* @param string $option
* @return boolean
*/
public function hasArg($arg)
{
if (is_string($arg) === false || trim($arg) == '')
{
return false;
}
else
{
$arg = $this->getArgKey($arg);
$exists = false;
foreach ($this->arguments as $argument)
{
if ($this->getArgKey($argument) == $arg)
{
$exists = true;
}
}
if (!$exists)
{
return false;
}
foreach ($this->options as $index => $option)
{
if ($option[0] == $arg)
{
return true;
}
}
}
return false;
}
/**
* Remove -, = and value and return
* just the key
*
* @param string $arg
* @return string
*/
protected function getArgKey($arg)
{
if (substr($arg, 0, 2) == '--')
{
$arg_explode = explode("=", $arg);
if (is_array($arg_explode) && count($arg_explode) == 2)
{
$arg = $arg_explode[0];
}
}
$arg = str_replace("-", "", $arg);
return $arg;
}
/**
* Return the value of the argument (if applicable)
*
* @param mixed $arg
*/
public function getArgValue($arg)
{
if ($this->hasArg($arg))
{
foreach ($this->arguments as $argument)
{
if ($this->getArgKey($argument) == $arg)
{
if (substr($argument, 0, 2) == '--')
{
$arg_explode = explode("=", $argument);
if (is_array($arg_explode) && count($arg_explode) == 2)
{
return $arg_explode[1];
}
}
}
}
}
return null;
}
/**
* Displays the description of the script and the
* options for running the script
*/
protected function showHelp()
{
echo $this->color->getColoredString(PHP_EOL . 'Description of "' . $this->arguments[0] . '":', 'light_green');
echo $this->color->getColoredString(PHP_EOL . $this->getDescription() . PHP_EOL, 'light_blue');
$options = $this->getOptions();
if (is_array($options))
{
echo $this->color->getColoredString(PHP_EOL . "Below are the options for this script: " . PHP_EOL . PHP_EOL, 'light_gray');
foreach ($options as $index => $option)
{
//show the number of "-"
$option[0] = ((strlen($option[0]) == 1) ? '-' : '--') . $option[0];
echo $this->color->getColoredString(($index+1) .
". " . str_pad($option[0], 20, " ",STR_PAD_RIGHT) . "Description: " .
str_pad($option[1], 40," ", STR_PAD_RIGHT) . " Required: " .
((isset($option[2]) && $option[2] === true) ? 'Yes' : 'No'), 'yellow');
echo PHP_EOL;
}
echo PHP_EOL;
}
else
{
echo PHP_EOL . "There are no options for this script." . PHP_EOL;
}
exit(); //kill the script since the user requested help
}
/**
* Outputs a message with optional foreground/background colors
* if -v is passed in to the script
*
* @param string $msg
* @param string $foreground_color
* @param string $background_color
*/
public function printLine($msg, $foreground_color = '', $background_color = '')
{
echo $this->formatLine($msg, $foreground_color, $background_color);
}
public function returnLine($msg, $foreground_color = '', $background_color = '')
{
return $this->formatLine($msg, $foreground_color, $background_color);
}
private function formatLine($msg, $foreground_color, $background_color)
{
//don't print if they don't have verbose message set
if ($this->hasArg('v') === false || trim($msg) == '')
{
return;
}
if (in_array($foreground_color, $this->color->getForegroundColors()) === false)
{
$foreground_color = null;
}
if (in_array($background_color, $this->color->getBackgroundColors()) === false)
{
$background_color = null;
}
return $this->color->getColoredString($msg, $foreground_color, $background_color) . PHP_EOL;
}
}
class PhpCliException extends Exception
{
public function __construct($message = null, $code = 0)
{
parent::__construct($message, $code);
}
public function __toString()
{
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}