|
1 | 1 | <?php |
2 | | - |
3 | 2 | namespace Ubiquity\devtools\cmd\traits; |
4 | 3 |
|
5 | 4 | use Ubiquity\devtools\cmd\ConsoleFormatter; |
|
9 | 8 |
|
10 | 9 | trait CmdTrait { |
11 | 10 |
|
12 | | - protected static function parseArguments(){ |
| 11 | + protected static function parseArguments() { |
13 | 12 | global $argv; |
14 | | - $argv_copy=$argv; |
15 | | - array_shift($argv_copy); |
| 13 | + $argv_copy = $argv; |
| 14 | + \array_shift($argv_copy); |
16 | 15 | $out = array(); |
17 | | - foreach($argv_copy as $arg){ |
18 | | - if(substr($arg, 0, 2) == '--'){ |
19 | | - preg_match ("/\=|\:|\ /", $arg, $matches, PREG_OFFSET_CAPTURE); |
20 | | - $eqPos=$matches[0][1]; |
21 | | - if($eqPos === false){ |
22 | | - $key = substr($arg, 2); |
| 16 | + foreach ($argv_copy as $arg) { |
| 17 | + if (\substr($arg, 0, 2) == '--') { |
| 18 | + \preg_match("/\=|\:|\ /", $arg, $matches, PREG_OFFSET_CAPTURE); |
| 19 | + $eqPos = $matches[0][1]; |
| 20 | + if ($eqPos === false) { |
| 21 | + $key = \substr($arg, 2); |
23 | 22 | $out[$key] = isset($out[$key]) ? $out[$key] : true; |
| 23 | + } else { |
| 24 | + $key = \substr($arg, 2, $eqPos - 2); |
| 25 | + $out[$key] = \substr($arg, $eqPos + 1); |
24 | 26 | } |
25 | | - else{ |
26 | | - $key = substr($arg, 2, $eqPos - 2); |
27 | | - $out[$key] = substr($arg, $eqPos + 1); |
28 | | - } |
29 | | - } |
30 | | - else if(substr($arg, 0, 1) == '-'){ |
31 | | - if(substr($arg, 2, 1) == '='||substr($arg, 2, 1) == ':' || substr($arg, 2, 1) == ' '){ |
32 | | - $key = substr($arg, 1, 1); |
33 | | - $out[$key] = substr($arg, 3); |
34 | | - } |
35 | | - else{ |
36 | | - $chars = str_split(substr($arg, 1)); |
37 | | - foreach($chars as $char){ |
| 27 | + } else if (\substr($arg, 0, 1) == '-') { |
| 28 | + if (\substr($arg, 2, 1) == '=' || \substr($arg, 2, 1) == ':' || \substr($arg, 2, 1) == ' ') { |
| 29 | + $key = \substr($arg, 1, 1); |
| 30 | + $out[$key] = \substr($arg, 3); |
| 31 | + } else { |
| 32 | + $chars = \str_split(\substr($arg, 1)); |
| 33 | + foreach ($chars as $char) { |
38 | 34 | $key = $char; |
39 | 35 | $out[$key] = isset($out[$key]) ? $out[$key] : true; |
40 | 36 | } |
41 | 37 | } |
42 | | - } |
43 | | - else{ |
| 38 | + } else { |
44 | 39 | $out[] = $arg; |
45 | 40 | } |
46 | 41 | } |
47 | 42 | return $out; |
48 | 43 | } |
49 | 44 |
|
50 | | - protected static function getOption($options,$option,$longOption,$default=NULL){ |
51 | | - if(array_key_exists($option, $options)){ |
52 | | - $option=$options[$option]; |
53 | | - }else if(array_key_exists($longOption, $options)){ |
54 | | - $option=$options[$longOption]; |
55 | | - } |
56 | | - else if(isset($default)===true){ |
57 | | - $option=$default; |
58 | | - }else |
59 | | - $option=""; |
60 | | - return $option; |
| 45 | + protected static function getOption($options, $option, $longOption, $default = NULL) { |
| 46 | + if (\array_key_exists($option, $options)) { |
| 47 | + $option = $options[$option]; |
| 48 | + } else if (\array_key_exists($longOption, $options)) { |
| 49 | + $option = $options[$longOption]; |
| 50 | + } else if (isset($default) === true) { |
| 51 | + $option = $default; |
| 52 | + } else |
| 53 | + $option = ""; |
| 54 | + return $option; |
61 | 55 | } |
62 | 56 |
|
63 | | - protected static function getOptionArray($options,$option,$longOption,$default=NULL){ |
64 | | - $option=self::getOption($options, $option, $longOption,$default); |
65 | | - if(is_string($option)){ |
66 | | - return explode(',', $option); |
| 57 | + protected static function getOptionArray($options, $option, $longOption, $default = NULL) { |
| 58 | + $option = self::getOption($options, $option, $longOption, $default); |
| 59 | + if (\is_string($option)) { |
| 60 | + return \explode(',', $option); |
67 | 61 | } |
68 | 62 | return $option; |
69 | 63 | } |
70 | 64 |
|
71 | | - protected static function getBooleanOption($options,$option,$longOption,$default=NULL){ |
72 | | - if(array_key_exists($option, $options)){ |
73 | | - $option=$options[$option]; |
74 | | - }else if(array_key_exists($longOption, $options)){ |
75 | | - $option=$options[$longOption]; |
76 | | - } |
77 | | - else if(isset($default)===true){ |
78 | | - $option=$default; |
79 | | - }else{ |
80 | | - $option=false; |
| 65 | + protected static function getBooleanOption($options, $option, $longOption, $default = NULL) { |
| 66 | + if (\array_key_exists($option, $options)) { |
| 67 | + $option = $options[$option]; |
| 68 | + } else if (\array_key_exists($longOption, $options)) { |
| 69 | + $option = $options[$longOption]; |
| 70 | + } else if (isset($default) === true) { |
| 71 | + $option = $default; |
| 72 | + } else { |
| 73 | + $option = false; |
81 | 74 | } |
82 | | - if(filter_var ( $option, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) === false){ |
83 | | - $option=false; |
84 | | - }else{ |
85 | | - $option=true; |
| 75 | + if (\filter_var($option, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === false) { |
| 76 | + $option = false; |
| 77 | + } else { |
| 78 | + $option = true; |
86 | 79 | } |
87 | 80 | return $option; |
88 | 81 | } |
89 | 82 |
|
90 | | - protected static function requiredParam($what,$paramName){ |
91 | | - if($what==null){ |
92 | | - ConsoleFormatter::showMessage($paramName.' is a required parameter','error'); |
93 | | - $answer=Console::question("Enter a value for ".$paramName.':'); |
94 | | - if($answer==null){ |
| 83 | + protected static function requiredParam($what, $paramName) { |
| 84 | + if ($what == null) { |
| 85 | + ConsoleFormatter::showMessage($paramName . ' is a required parameter', 'error'); |
| 86 | + $answer = Console::question("Enter a value for " . $paramName . ':'); |
| 87 | + if ($answer == null) { |
95 | 88 | exit(1); |
96 | | - }else{ |
| 89 | + } else { |
97 | 90 | return $answer; |
98 | 91 | } |
99 | 92 | } |
100 | 93 | return $what; |
101 | 94 | } |
102 | 95 |
|
103 | | - protected static function answerModel($options,$option,$longOption,$part,$config){ |
104 | | - $resource=self::getOption($options, $option, $longOption,null); |
105 | | - if($resource==null){ |
106 | | - echo ConsoleFormatter::showMessage($longOption.' is a required parameter! You must add <b>-'.$option.'</b> option.','error',$part); |
107 | | - $models=CacheManager::getModels($config,true); |
108 | | - $answer=Console::question("Enter the ".$longOption." to add from the following:\n",$models); |
109 | | - if(array_search($answer, $models)!==false){ |
110 | | - $resource=$answer; |
111 | | - }else{ |
| 96 | + protected static function answerModel($options, $option, $longOption, $part, $config) { |
| 97 | + $resource = self::getOption($options, $option, $longOption, null); |
| 98 | + if ($resource == null) { |
| 99 | + echo ConsoleFormatter::showMessage($longOption . ' is a required parameter! You must add <b>-' . $option . '</b> option.', 'error', $part); |
| 100 | + $models = CacheManager::getModels($config, true); |
| 101 | + $answer = Console::question("Enter the " . $longOption . " to add from the following:\n", $models); |
| 102 | + if (array_search($answer, $models) !== false) { |
| 103 | + $resource = $answer; |
| 104 | + } else { |
112 | 105 | echo ConsoleFormatter::showInfo("Cancelled operation."); |
113 | 106 | exit(1); |
114 | 107 | } |
115 | 108 | } |
116 | 109 | return self::getCompleteClassname($config, $resource); |
117 | 110 | } |
118 | 111 |
|
119 | | - protected static function getCompleteClassname($config,$classname,$type='models'){ |
120 | | - $prefix=$config["mvcNS"][$type]??null; |
121 | | - $classname=ltrim($classname,"\\"); |
122 | | - if(isset($prefix)){ |
123 | | - if(!UString::startswith($classname,$prefix)){ |
124 | | - $classname=$prefix."\\".$classname; |
| 112 | + protected static function getCompleteClassname($config, $classname, $type = 'models') { |
| 113 | + $prefix = $config["mvcNS"][$type] ?? null; |
| 114 | + $classname = \ltrim($classname, "\\"); |
| 115 | + if (isset($prefix)) { |
| 116 | + if (! UString::startswith($classname, $prefix)) { |
| 117 | + $classname = $prefix . "\\" . $classname; |
125 | 118 | } |
126 | 119 | } |
127 | 120 | return $classname; |
128 | 121 | } |
129 | 122 |
|
130 | | - protected static function getSelectedModels($models,$config){ |
131 | | - if($models!=null){ |
132 | | - $result=[]; |
133 | | - $models=explode(",", $models); |
134 | | - foreach ($models as $model){ |
135 | | - $model=self::getCompleteClassname($config, $model); |
136 | | - if(class_exists($model)){ |
137 | | - $result[]=$model; |
| 123 | + protected static function getSelectedModels($models, $config) { |
| 124 | + if ($models != null) { |
| 125 | + $result = []; |
| 126 | + $models = \explode(",", $models); |
| 127 | + foreach ($models as $model) { |
| 128 | + $model = self::getCompleteClassname($config, $model); |
| 129 | + if (\class_exists($model)) { |
| 130 | + $result[] = $model; |
138 | 131 | } |
139 | 132 | } |
140 | 133 | return $result; |
|
0 commit comments