Skip to content

Commit 7c4a6ee

Browse files
committed
add createCommand command
1 parent fa5a553 commit 7c4a6ee

File tree

2 files changed

+98
-87
lines changed

2 files changed

+98
-87
lines changed

src/Ubiquity/devtools/cmd/Command.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,22 @@ public static function newMail() {
513513
]);
514514
}
515515

516+
public static function createCommand() {
517+
return new Command("create-command", "commandName", "Creates a new custom command for the devtools.", [
518+
"create:command",
519+
'add:command',
520+
'addCommand',
521+
'add-command'
522+
], [
523+
"v" => Parameter::create("value", "The command value (first parameter).", []),
524+
"p" => Parameter::create("parameters", "The command parameters (comma separated).", []),
525+
"d" => Parameter::create("description", "The command description.", []),
526+
"a" => Parameter::create("aliases", "The command aliases (comma separated).", [])
527+
], [
528+
'Creates a new custom command' => 'Ubiquity create-command custom'
529+
]);
530+
}
531+
516532
protected static function getCustomCommandInfos() {
517533
$result = [];
518534
$commands = self::getCustomCommands();
@@ -544,16 +560,16 @@ public static function getCustomAliases() {
544560
return self::$customAliases;
545561
}
546562

547-
public static function preloadCustomCommands() {
548-
$files = UFileSystem::glob_recursive("*.cmd.php");
563+
public static function preloadCustomCommands(array $config = []) {
564+
$config['cmd-pattern'] ??= 'commands' . \DS . '*.cmd.php';
565+
$files = UFileSystem::glob_recursive($config['cmd-pattern']);
549566
foreach ($files as $file) {
550567
include $file;
551568
}
552569
}
553570

554571
public static function getCommands() {
555572
return [
556-
...self::getCustomCommandInfos(),
557573
self::project(),
558574
self::serve(),
559575
self::bootstrap(),
@@ -583,7 +599,9 @@ public static function getCommands() {
583599
self::newTheme(),
584600
self::mailer(),
585601
self::newMail(),
586-
self::sendMails()
602+
self::sendMails(),
603+
self::createCommand(),
604+
...self::getCustomCommandInfos()
587605
];
588606
}
589607

src/Ubiquity/devtools/cmd/traits/CmdTrait.php

Lines changed: 76 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
namespace Ubiquity\devtools\cmd\traits;
43

54
use Ubiquity\devtools\cmd\ConsoleFormatter;
@@ -9,132 +8,126 @@
98

109
trait CmdTrait {
1110

12-
protected static function parseArguments(){
11+
protected static function parseArguments() {
1312
global $argv;
14-
$argv_copy=$argv;
15-
array_shift($argv_copy);
13+
$argv_copy = $argv;
14+
\array_shift($argv_copy);
1615
$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);
2322
$out[$key] = isset($out[$key]) ? $out[$key] : true;
23+
} else {
24+
$key = \substr($arg, 2, $eqPos - 2);
25+
$out[$key] = \substr($arg, $eqPos + 1);
2426
}
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) {
3834
$key = $char;
3935
$out[$key] = isset($out[$key]) ? $out[$key] : true;
4036
}
4137
}
42-
}
43-
else{
38+
} else {
4439
$out[] = $arg;
4540
}
4641
}
4742
return $out;
4843
}
4944

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;
6155
}
6256

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);
6761
}
6862
return $option;
6963
}
7064

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;
8174
}
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;
8679
}
8780
return $option;
8881
}
8982

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) {
9588
exit(1);
96-
}else{
89+
} else {
9790
return $answer;
9891
}
9992
}
10093
return $what;
10194
}
10295

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 {
112105
echo ConsoleFormatter::showInfo("Cancelled operation.");
113106
exit(1);
114107
}
115108
}
116109
return self::getCompleteClassname($config, $resource);
117110
}
118111

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;
125118
}
126119
}
127120
return $classname;
128121
}
129122

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;
138131
}
139132
}
140133
return $result;

0 commit comments

Comments
 (0)