Skip to content

Commit 178d285

Browse files
committed
improve the javadoc
1 parent 905acc1 commit 178d285

File tree

6 files changed

+246
-92
lines changed

6 files changed

+246
-92
lines changed

src/Ubiquity/devtools/cmd/Command.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
use Ubiquity\utils\base\UIntrospection;
55
use Ubiquity\utils\base\UFileSystem;
66

7+
/**
8+
* Define a command complete desciption.
9+
* Ubiquity\devtools\cmd$Command
10+
* This class is part of Ubiquity
11+
*
12+
* @author jc
13+
* @version 1.0.0
14+
*
15+
*/
716
class Command {
817

918
protected $name;
Lines changed: 75 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,94 @@
11
<?php
2-
namespace Ubiquity\devtools\cmd;
2+
namespace Ubiquity\devtools\cmd;
33

4+
/**
5+
* Ubiquity\devtools\cmd$Console
6+
* This class is part of Ubiquity
7+
*
8+
* @author jc
9+
* @version 1.0.0
10+
*
11+
*/
412
class Console {
5-
public static function readline(){
6-
return rtrim(fgets(STDIN));
13+
14+
/**
15+
* Read a line from the user input.
16+
*
17+
* @return string
18+
*/
19+
public static function readline() {
20+
return \rtrim(\fgets(STDIN));
721
}
822

9-
public static function question($prompt,array $propositions=null){
10-
echo ConsoleFormatter::colorize($prompt,ConsoleFormatter::BLACK,ConsoleFormatter::BG_YELLOW);
11-
if(is_array($propositions)){
12-
if(sizeof($propositions)>2){
13-
$props="";
14-
foreach ($propositions as $index=>$prop){
15-
$props.="[".($index+1)."] ".$prop."\n";
23+
/**
24+
* Ask the user a question and return the answer.
25+
*
26+
* @param string $prompt
27+
* @param ?array $propositions
28+
* @return string
29+
*/
30+
public static function question($prompt, array $propositions = null) {
31+
echo ConsoleFormatter::colorize($prompt, ConsoleFormatter::BLACK, ConsoleFormatter::BG_YELLOW);
32+
if (is_array($propositions)) {
33+
if (sizeof($propositions) > 2) {
34+
$props = "";
35+
foreach ($propositions as $index => $prop) {
36+
$props .= "[" . ($index + 1) . "] " . $prop . "\n";
1637
}
1738
echo ConsoleFormatter::formatContent($props);
18-
do{
19-
$answer=self::readline();
20-
}while((int)$answer!=$answer || !isset($propositions[(int)$answer-1]));
21-
$answer=$propositions[(int)$answer-1];
22-
}else {
23-
echo " (".implode("/", $propositions).")\n";
24-
do{
25-
$answer=self::readline();
26-
}while(array_search($answer, $propositions)===false);
39+
do {
40+
$answer = self::readline();
41+
} while ((int) $answer != $answer || ! isset($propositions[(int) $answer - 1]));
42+
$answer = $propositions[(int) $answer - 1];
43+
} else {
44+
echo " (" . implode("/", $propositions) . ")\n";
45+
do {
46+
$answer = self::readline();
47+
} while (array_search($answer, $propositions) === false);
2748
}
28-
}else{
29-
$answer=self::readline();
49+
} else {
50+
$answer = self::readline();
3051
}
3152

3253
return $answer;
3354
}
3455

35-
public static function isYes($answer){
36-
return array_search($answer, ["yes","y"])!==false;
56+
/**
57+
* Returns true if the answer is yes or y.
58+
*
59+
* @param string $answer
60+
* @return boolean
61+
*/
62+
public static function isYes($answer) {
63+
return \array_search($answer, [
64+
"yes",
65+
"y"
66+
]) !== false;
3767
}
3868

39-
public static function isNo($answer){
40-
return array_search($answer, ["no","n"])!==false;
69+
/**
70+
* Returns true if the answer is no or n.
71+
*
72+
* @param string $answer
73+
* @return boolean
74+
*/
75+
public static function isNo($answer) {
76+
return \array_search($answer, [
77+
"no",
78+
"n"
79+
]) !== false;
4180
}
4281

43-
public static function isCancel($answer){
44-
return array_search($answer, ["cancel","z"])!==false;
82+
/**
83+
* Returns true if the answer is cancel or z.
84+
*
85+
* @param string $answer
86+
* @return boolean
87+
*/
88+
public static function isCancel($answer) {
89+
return \array_search($answer, [
90+
"cancel",
91+
"z"
92+
]) !== false;
4593
}
4694
}
Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
<?php
2-
namespace Ubiquity\devtools\cmd;
2+
namespace Ubiquity\devtools\cmd;
33

4+
/**
5+
* Ubiquity\devtools\cmd$ConsoleFormatter
6+
* This class is part of Ubiquity
7+
*
8+
* @author jc
9+
* @version 1.0.0
10+
*
11+
*/
412
class ConsoleFormatter {
5-
const BLACK='0;30',DARK_GREY='1;30',BLUE='0;34',LIGHT_BLUE='1;34',GREEN='0;32',LIGHT_GREEN='1;32',CYAN='0;36',LIGHT_CYAN='1;36',RED='0;31',LIGHT_RED='1;31',PURPLE='0;35',LIGHT_PURPLE='1;35',BROWN='0;33',YELLOW='1;33',LIGHT_GRAY='0;37',WHITE='1;37';
6-
const BG_BLACK='40',BG_RED='41',BG_GREEN='42',BG_YELLOW='43',BG_BLUE='44',BG_MAGENTA='45',BG_CYAN='46',BG_LIGHT_GRAY='47';
7-
const BOLD='1',END_BOLD='22',CLEAR='0';
13+
14+
const BLACK = '0;30', DARK_GREY = '1;30', BLUE = '0;34', LIGHT_BLUE = '1;34', GREEN = '0;32', LIGHT_GREEN = '1;32', CYAN = '0;36', LIGHT_CYAN = '1;36', RED = '0;31', LIGHT_RED = '1;31', PURPLE = '0;35', LIGHT_PURPLE = '1;35', BROWN = '0;33', YELLOW = '1;33', LIGHT_GRAY = '0;37', WHITE = '1;37';
15+
16+
const BG_BLACK = '40', BG_RED = '41', BG_GREEN = '42', BG_YELLOW = '43', BG_BLUE = '44', BG_MAGENTA = '45', BG_CYAN = '46', BG_LIGHT_GRAY = '47';
17+
18+
const BOLD = '1', END_BOLD = '22', CLEAR = '0';
819

920
/**
1021
* Returns a colored string
22+
*
1123
* @param string $string
1224
* @param string $color
1325
* @param string $bgColor
1426
* @return string
1527
*/
1628
public static function colorize($string, $color = null, $bgColor = null) {
17-
if(!self::isSupported()){
29+
if (! self::isSupported()) {
1830
return $string;
1931
}
2032
$coloredString = "";
@@ -24,36 +36,36 @@ public static function colorize($string, $color = null, $bgColor = null) {
2436
if (isset($bgColor)) {
2537
$coloredString .= self::escape($bgColor);
2638
}
27-
$coloredString .= $string .self::escape(self::CLEAR);
39+
$coloredString .= $string . self::escape(self::CLEAR);
2840
return $coloredString;
2941
}
3042

31-
private static function prefixLines($str,$prefix){
32-
$lines=explode("\n", $str);
33-
array_walk($lines, function(&$line) use($prefix){if(trim($line)!=null) $line=$prefix.$line;});
43+
private static function prefixLines($str, $prefix) {
44+
$lines = explode("\n", $str);
45+
array_walk($lines, function (&$line) use ($prefix) {
46+
if (trim($line) != null)
47+
$line = $prefix . $line;
48+
});
3449
return implode("\n", $lines);
3550
}
3651

37-
private static function escape($value){
52+
private static function escape($value) {
3853
return "\033[{$value}m";
3954
}
4055

41-
public static function showInfo($content,$dColor=self::CYAN){
42-
return self::colorize(self::formatContent($content),$dColor);
56+
public static function showInfo($content, $dColor = self::CYAN) {
57+
return self::colorize(self::formatContent($content), $dColor);
4358
}
4459

4560
/**
61+
*
4662
* @return boolean
4763
*/
48-
public static function isSupported()
49-
{
64+
public static function isSupported() {
5065
if (DIRECTORY_SEPARATOR === '\\') {
5166
if (\function_exists('sapi_windows_vt100_support') && @sapi_windows_vt100_support(STDOUT)) {
5267
return true;
53-
} elseif ('10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
54-
|| false !== \getenv('ANSICON')
55-
|| 'ON' === \getenv('ConEmuANSI')
56-
|| 'xterm' === \getenv('TERM')) {
68+
} elseif ('10.0.10586' === PHP_WINDOWS_VERSION_MAJOR . '.' . PHP_WINDOWS_VERSION_MINOR . '.' . PHP_WINDOWS_VERSION_BUILD || false !== \getenv('ANSICON') || 'ON' === \getenv('ConEmuANSI') || 'xterm' === \getenv('TERM')) {
5769
return true;
5870
}
5971
return false;
@@ -62,43 +74,68 @@ public static function isSupported()
6274
}
6375
}
6476

65-
public static function formatContent($content,$prefix=" · "){
66-
$content = str_replace ( "<br>", "\n", $content );
67-
$content=self::formatHtml($content);
68-
$content= strip_tags ( $content );
69-
return "\n".self::prefixLines($content,$prefix)."\n";
77+
/**
78+
* Format a multilines content.
79+
*
80+
* @param string $content
81+
* @param string $prefix
82+
* @return string
83+
*/
84+
public static function formatContent($content, $prefix = " · ") {
85+
$content = str_replace("<br>", "\n", $content);
86+
$content = self::formatHtml($content);
87+
$content = strip_tags($content);
88+
return "\n" . self::prefixLines($content, $prefix) . "\n";
7089
}
7190

72-
public static function showMessage($content, $type='info',$title=null) {
73-
$header="".$type;
74-
if(isset($title)){
75-
$header.=' : '.$title;
91+
/**
92+
* Return a formated message.
93+
*
94+
* @param string $content
95+
* @param string $type
96+
* @param ?string $title
97+
* @return string
98+
*/
99+
public static function showMessage($content, $type = 'info', $title = null) {
100+
$header = "" . $type;
101+
if (isset($title)) {
102+
$header .= ' : ' . $title;
76103
}
77-
$result=self::formatContent($content);
78-
switch ($type){
104+
$result = self::formatContent($content);
105+
switch ($type) {
79106
case 'error':
80-
$header=self::colorize($header,self::LIGHT_RED);
107+
$header = self::colorize($header, self::LIGHT_RED);
81108
break;
82109
case 'success':
83-
$header=self::colorize($header,self::GREEN);
110+
$header = self::colorize($header, self::GREEN);
84111
break;
85112
case 'info':
86-
$header=self::colorize($header,self::CYAN);
113+
$header = self::colorize($header, self::CYAN);
87114
break;
88115
case 'warning':
89-
$header=self::colorize($header,self::LIGHT_GRAY);
116+
$header = self::colorize($header, self::LIGHT_GRAY);
90117
break;
91118
}
92-
$result=rtrim($result,"\n");
93-
return ConsoleTable::borderType([[$header.$result]], $type);
119+
$result = rtrim($result, "\n");
120+
return ConsoleTable::borderType([
121+
[
122+
$header . $result
123+
]
124+
], $type);
94125
}
95126

96-
public static function formatHtml($str){
97-
$reg='@<(b)>(.+?)</\1>@i';
98-
if(!self::isSupported()){
99-
return preg_replace($reg, '$2', $str);
127+
/**
128+
* Format an html message (only for <b>bold values</b>).
129+
*
130+
* @param string $str
131+
* @return mixed
132+
*/
133+
public static function formatHtml($str) {
134+
$reg = '@<(b)>(.+?)</\1>@i';
135+
if (! self::isSupported()) {
136+
return \preg_replace($reg, '$2', $str);
100137
}
101-
return preg_replace($reg, self::escape(self::BOLD).'$2'.self::escape(self::END_BOLD), $str);
138+
return \preg_replace($reg, self::escape(self::BOLD) . '$2' . self::escape(self::END_BOLD), $str);
102139
}
103140
}
104141

0 commit comments

Comments
 (0)