|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ubiquity\devtools\cmd\traits; |
| 4 | + |
| 5 | +use Ubiquity\devtools\cmd\ConsoleFormatter; |
| 6 | +use Ubiquity\cache\CacheManager; |
| 7 | +use Ubiquity\devtools\cmd\Console; |
| 8 | +use Ubiquity\utils\base\UString; |
| 9 | + |
| 10 | +trait CmdTrait { |
| 11 | + |
| 12 | + protected static function parseArguments(){ |
| 13 | + global $argv; |
| 14 | + $argv_copy=$argv; |
| 15 | + array_shift($argv_copy); |
| 16 | + $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); |
| 23 | + $out[$key] = isset($out[$key]) ? $out[$key] : true; |
| 24 | + } |
| 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){ |
| 38 | + $key = $char; |
| 39 | + $out[$key] = isset($out[$key]) ? $out[$key] : true; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + else{ |
| 44 | + $out[] = $arg; |
| 45 | + } |
| 46 | + } |
| 47 | + return $out; |
| 48 | + } |
| 49 | + |
| 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; |
| 61 | + } |
| 62 | + |
| 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); |
| 67 | + } |
| 68 | + return $option; |
| 69 | + } |
| 70 | + |
| 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; |
| 81 | + } |
| 82 | + if(filter_var ( $option, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ) === false){ |
| 83 | + $option=false; |
| 84 | + }else{ |
| 85 | + $option=true; |
| 86 | + } |
| 87 | + return $option; |
| 88 | + } |
| 89 | + |
| 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){ |
| 95 | + exit(1); |
| 96 | + }else{ |
| 97 | + return $answer; |
| 98 | + } |
| 99 | + } |
| 100 | + return $what; |
| 101 | + } |
| 102 | + |
| 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{ |
| 112 | + echo ConsoleFormatter::showInfo("Cancelled operation."); |
| 113 | + exit(1); |
| 114 | + } |
| 115 | + } |
| 116 | + return self::getCompleteClassname($config, $resource); |
| 117 | + } |
| 118 | + |
| 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; |
| 125 | + } |
| 126 | + } |
| 127 | + return $classname; |
| 128 | + } |
| 129 | + |
| 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; |
| 138 | + } |
| 139 | + } |
| 140 | + return $result; |
| 141 | + } |
| 142 | + return null; |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments