Skip to content

Commit 00045d0

Browse files
committed
add files
0 parents  commit 00045d0

File tree

7 files changed

+344
-0
lines changed

7 files changed

+344
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
.buildpath
3+
.project
4+
.settings/

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ubiquity-commands

composer.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name" : "phpmv/ubiquity-commands",
3+
"description" : "Ubiquity commands helper",
4+
"type" : "extension",
5+
"keywords" : [
6+
"php",
7+
"framework",
8+
"mvc",
9+
"tools",
10+
"devtools",
11+
"php-framework",
12+
"cmd"
13+
],
14+
"require" : {
15+
"php" : "^7.4"
16+
},
17+
"license" : "Apache-2.0",
18+
"authors" : [{
19+
"name" : "Jean-Christophe HERON",
20+
"email" : "[email protected]",
21+
"role" : "Lead developer"
22+
}
23+
],
24+
"autoload" : {
25+
"psr-4" : {
26+
"Ubiquity\\" : "src/Ubiquity/"
27+
}
28+
},
29+
"extra" : {
30+
"branch-alias" : {
31+
"dev-master" : "1.0.x-dev"
32+
}
33+
}
34+
}
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
namespace Ubiquity\devtools\cmd;
3+
4+
class Parameter {
5+
protected $name;
6+
protected $description;
7+
protected $values;
8+
protected $defaultValue;
9+
public function __construct($name,$description,$values,$defaultValue=""){
10+
$this->name=$name;
11+
$this->description=$description;
12+
$this->values=$values;
13+
$this->defaultValue=$defaultValue;
14+
}
15+
16+
public function __toString(){
17+
$dec="\t\t\t";
18+
$result= "\tshortcut of --<b>".$this->name."</b>\n".$dec.$this->description;
19+
if(sizeof($this->values)>0){
20+
$result.="\n".$dec."Possibles values :";
21+
$result.="\n".$dec.ConsoleFormatter::colorize(implode(",", $this->values),ConsoleFormatter::DARK_GREY);
22+
}
23+
if($this->defaultValue!==""){
24+
$result.="\n".$dec."Default : [".ConsoleFormatter::colorize($this->defaultValue,ConsoleFormatter::GREEN)."]";
25+
}
26+
return $result;
27+
}
28+
29+
public function getName() {
30+
return $this->name;
31+
}
32+
33+
public function setName($name) {
34+
$this->name=$name;
35+
return $this;
36+
}
37+
38+
public function getDescription() {
39+
return $this->description;
40+
}
41+
42+
public function setDescription($description) {
43+
$this->description=$description;
44+
return $this;
45+
}
46+
47+
public function getValues() {
48+
return $this->values;
49+
}
50+
51+
public function setValues($values) {
52+
$this->values=$values;
53+
return $this;
54+
}
55+
56+
public function getDefaultValue() {
57+
return $this->defaultValue;
58+
}
59+
60+
public function setDefaultValue($defaultValue) {
61+
$this->defaultValue=$defaultValue;
62+
return $this;
63+
}
64+
65+
public static function create($name,$description,$values,$defaultValue=""){
66+
return new Parameter($name, $description, $values,$defaultValue);
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Ubiquity\devtools\cmd\commands;
4+
5+
use Ubiquity\devtools\cmd\traits\CmdTrait;
6+
7+
abstract class AbstractCmd {
8+
use CmdTrait;
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
namespace Ubiquity\devtools\cmd\commands;
3+
4+
use Ubiquity\devtools\cmd\Command;
5+
6+
/**
7+
* Ubiquity\devtools\cmd$AbstractCustomCommand
8+
* This class is part of Ubiquity
9+
*
10+
* @author jc
11+
* @version 1.0.0
12+
* @package ubiquity.devtools
13+
*
14+
*/
15+
abstract class AbstractCustomCommand extends AbstractCmd {
16+
17+
/**
18+
*
19+
* @var Command
20+
*/
21+
private $command;
22+
23+
public function __construct() {
24+
$this->command = new Command($this->getName(), $this->getValue(), $this->getDescription(), $this->getAliases(), $this->getParameters(), $this->getExamples());
25+
}
26+
27+
/**
28+
* Return the command name.
29+
*
30+
* @return string
31+
*/
32+
abstract protected function getName(): string;
33+
34+
/**
35+
* Return the command parameter name.
36+
*
37+
* @return string
38+
*/
39+
abstract protected function getValue(): string;
40+
41+
/**
42+
* Return the command description.
43+
*
44+
* @return string
45+
*/
46+
abstract protected function getDescription(): string;
47+
48+
/**
49+
*
50+
* @return array
51+
*/
52+
abstract protected function getAliases(): array;
53+
54+
/**
55+
* Return the list of parameters.
56+
* Sample: ['d'=>Parameter::create('database', 'The database connection to use', [],'default')].
57+
*
58+
* @return array
59+
*/
60+
abstract protected function getParameters(): array;
61+
62+
/**
63+
* Return a list of examples.
64+
* Sample: ['Clear all caches'=>'Ubiquity clear-cache -t=all','Clear models cache'=>'Ubiquity clear-cache -t=models']
65+
*
66+
* @return array
67+
*/
68+
abstract protected function getExamples(): array;
69+
70+
/**
71+
* Return the command informations.
72+
*
73+
* @return \Ubiquity\devtools\cmd\Command
74+
*/
75+
public function getCommand() {
76+
return $this->command;
77+
}
78+
79+
abstract public function run(&$config, $options, $what, ...$otherArgs);
80+
}
81+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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

Comments
 (0)