|
15 | 15 | * show formatted message text |
16 | 16 | * |
17 | 17 | * @package inhere\console\utils |
| 18 | + * |
| 19 | + * @method static int info($messages, $quit = false) |
| 20 | + * @method static int note($messages, $quit = false) |
| 21 | + * @method static int notice($messages, $quit = false) |
| 22 | + * @method static int success($messages, $quit = false) |
| 23 | + * @method static int primary($messages, $quit = false) |
| 24 | + * @method static int warning($messages, $quit = false) |
| 25 | + * @method static int danger($messages, $quit = false) |
| 26 | + * @method static int error($messages, $quit = false) |
| 27 | + * |
| 28 | + * @method static int liteInfo($messages, $quit = false) |
| 29 | + * @method static int liteNote($messages, $quit = false) |
| 30 | + * @method static int liteNotice($messages, $quit = false) |
| 31 | + * @method static int liteSuccess($messages, $quit = false) |
| 32 | + * @method static int litePrimary($messages, $quit = false) |
| 33 | + * @method static int liteWarning($messages, $quit = false) |
| 34 | + * @method static int liteDanger($messages, $quit = false) |
| 35 | + * @method static int liteError($messages, $quit = false) |
18 | 36 | */ |
19 | 37 | class Show |
20 | 38 | { |
@@ -74,48 +92,80 @@ public static function block($messages, $type = 'MESSAGE', $style = Style::NORMA |
74 | 92 | $text = sprintf('<%s>%s</%s>', $style, $text, $style); |
75 | 93 | } |
76 | 94 |
|
77 | | - // $this->write($text); |
78 | 95 | return self::write($text, true, $quit); |
79 | 96 | } |
80 | 97 |
|
81 | | - public static function primary($messages, $quit = false) |
| 98 | + /** |
| 99 | + * @param mixed $messages |
| 100 | + * @param string|null $type |
| 101 | + * @param string $style |
| 102 | + * @param int|boolean $quit If is int, setting it is exit code. |
| 103 | + * @return int |
| 104 | + */ |
| 105 | + public static function liteBlock($messages, $type = 'MESSAGE', $style = Style::NORMAL, $quit = false) |
82 | 106 | { |
83 | | - return static::block($messages, 'IMPORTANT', Style::PRIMARY, $quit); |
84 | | - } |
| 107 | + $messages = is_array($messages) ? array_values($messages) : array($messages); |
85 | 108 |
|
86 | | - public static function success($messages, $quit = false) |
87 | | - { |
88 | | - return static::block($messages, 'SUCCESS', Style::SUCCESS, $quit); |
89 | | - } |
| 109 | + // add type |
| 110 | + if (null !== $type) { |
| 111 | + $type = sprintf('[%s]', strtoupper($type)); |
| 112 | + } |
90 | 113 |
|
91 | | - public static function info($messages, $quit = false) |
92 | | - { |
93 | | - return static::block($messages, 'INFO', Style::INFO, $quit); |
94 | | - } |
| 114 | + $text = implode(PHP_EOL, $messages); |
| 115 | + $color = static::getStyle(); |
95 | 116 |
|
96 | | - public static function note($messages, $quit = false) |
97 | | - { |
98 | | - return static::block($messages, 'NOTE', Style::INFO, $quit); |
99 | | - } |
| 117 | + if (is_string($style) && $color->hasStyle($style)) { |
| 118 | + $type = sprintf('<%s>%s</%s> ', $style, $type, $style); |
| 119 | + } |
100 | 120 |
|
101 | | - public static function notice($messages, $quit = false) |
102 | | - { |
103 | | - return static::block($messages, 'NOTICE', Style::COMMENT, $quit); |
| 121 | + return self::write($type . $text, true, $quit); |
104 | 122 | } |
105 | 123 |
|
106 | | - public static function warning($messages, $quit = false) |
107 | | - { |
108 | | - return static::block($messages, 'WARNING', Style::WARNING, $quit); |
109 | | - } |
| 124 | + /** |
| 125 | + * @var array |
| 126 | + */ |
| 127 | + private static $blockMethods = [ |
| 128 | + // method => style |
| 129 | + 'info' => 'info', |
| 130 | + 'note' => 'note', |
| 131 | + 'notice' => 'notice', |
| 132 | + 'success' => 'success', |
| 133 | + 'primary' => 'primary', |
| 134 | + 'warning' => 'warning', |
| 135 | + 'danger' => 'danger', |
| 136 | + 'error' => 'error', |
| 137 | + |
| 138 | + // lite style |
| 139 | + 'liteInfo' => 'info', |
| 140 | + 'liteNote' => 'note', |
| 141 | + 'liteNotice' => 'notice', |
| 142 | + 'liteSuccess' => 'success', |
| 143 | + 'litePrimary' => 'primary', |
| 144 | + 'liteWarning' => 'yellow', |
| 145 | + 'liteDanger' => 'danger', |
| 146 | + 'liteError' => 'red', |
| 147 | + ]; |
110 | 148 |
|
111 | | - public static function danger($messages, $quit = false) |
| 149 | + /** |
| 150 | + * @param string $method |
| 151 | + * @param array $args |
| 152 | + * @return int |
| 153 | + */ |
| 154 | + public static function __callStatic($method, array $args = []) |
112 | 155 | { |
113 | | - return static::block($messages, 'DANGER', Style::DANGER, $quit); |
114 | | - } |
| 156 | + if (isset(self::$blockMethods[$method])) { |
| 157 | + $msg = $args[0]; |
| 158 | + $quit = $args[1] ?? false; |
| 159 | + $style = self::$blockMethods[$method]; |
115 | 160 |
|
116 | | - public static function error($messages, $quit = false) |
117 | | - { |
118 | | - return static::block($messages, 'ERROR', Style::ERROR, $quit); |
| 161 | + if (0 === strpos($method, 'lite')) { |
| 162 | + return self::liteBlock($msg, $style === 'primary' ? 'IMPORTANT' : $style, $style, $quit); |
| 163 | + } |
| 164 | + |
| 165 | + return self::block($msg, $style === 'primary' ? 'IMPORTANT' : $style, $style, $quit); |
| 166 | + } |
| 167 | + |
| 168 | + throw new \LogicException("Call a not exists method: $method"); |
119 | 169 | } |
120 | 170 |
|
121 | 171 | ///////////////////////////////////////////////////////////////// |
@@ -839,4 +889,12 @@ public static function stderr($text, $nl = true, $quit = -200) |
839 | 889 | ]); |
840 | 890 | } |
841 | 891 |
|
| 892 | + /** |
| 893 | + * @param bool $onlyKey |
| 894 | + * @return array |
| 895 | + */ |
| 896 | + public static function getBlockMethods($onlyKey = true): array |
| 897 | + { |
| 898 | + return $onlyKey ? array_keys(self::$blockMethods) : self::$blockMethods; |
| 899 | + } |
842 | 900 | } |
0 commit comments