Skip to content

Commit 7292b4b

Browse files
committed
update, add more block message output methods
1 parent fa42324 commit 7292b4b

File tree

5 files changed

+152
-117
lines changed

5 files changed

+152
-117
lines changed

examples/HomeController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ public function blockMsgCommand()
8080
{
8181
$this->write('block message:');
8282

83-
foreach (Interact::$defaultBlocks as $type) {
84-
$this->output->$type('message text');
83+
foreach (Interact::getBlockMethods() as $type) {
84+
$this->output->$type("$type style message text");
8585
}
8686

8787
return 0;
@@ -209,10 +209,10 @@ public function fmtMsgCommand()
209209
public function useArgCommand()
210210
{
211211
$this->write('input arguments:');
212-
echo Helper::dumpVar($this->input->getArgs());
212+
echo Helper::dumpVars($this->input->getArgs());
213213

214214
$this->write('input options:');
215-
echo Helper::dumpVar($this->input->getOpts());
215+
echo Helper::dumpVars($this->input->getOpts());
216216

217217
// $this->write('the Input object:');
218218
// var_dump($this->input);
@@ -288,7 +288,7 @@ public function downCommand()
288288

289289
$d = Download::down($url, $saveAs, $type);
290290

291-
echo Helper::dumpVar($d);
291+
echo Helper::dumpVars($d);
292292

293293
return 0;
294294
}

src/AbstractCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ public function validateInput()
132132

133133
$this->input->setArgs($args);
134134

135+
// check options
136+
// $givenOpts = $this->input->getOpts();
137+
// $defOpts = $definition->getOptions();
138+
135139
return true;
136140
}
137141

src/io/InputDefinition.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,15 @@ public function hasShortcut($name)
358358
* @param string $shortcut the Shortcut name
359359
* @return array
360360
*/
361-
public function getOptionForShortcut($shortcut)
361+
public function getOptionByShortcut($shortcut)
362362
{
363363
return $this->getOption($this->shortcutToName($shortcut));
364364
}
365365

366+
/**
367+
* @param string $shortcut
368+
* @return mixed
369+
*/
366370
private function shortcutToName($shortcut)
367371
{
368372
if (!isset($this->shortcuts[$shortcut])) {

src/traits/FormatOutputTrait.php

Lines changed: 51 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@
1414
/**
1515
* Class FormatOutputTrait
1616
* @package inhere\console\traits
17+
*
18+
* @method int info($messages, $quit = false)
19+
* @method int note($messages, $quit = false)
20+
* @method int notice($messages, $quit = false)
21+
* @method int success($messages, $quit = false)
22+
* @method int primary($messages, $quit = false)
23+
* @method int warning($messages, $quit = false)
24+
* @method int danger($messages, $quit = false)
25+
* @method int error($messages, $quit = false)
26+
*
27+
* @method int liteInfo($messages, $quit = false)
28+
* @method int liteNote($messages, $quit = false)
29+
* @method int liteNotice($messages, $quit = false)
30+
* @method int liteSuccess($messages, $quit = false)
31+
* @method int litePrimary($messages, $quit = false)
32+
* @method int liteWarning($messages, $quit = false)
33+
* @method int liteDanger($messages, $quit = false)
34+
* @method int liteError($messages, $quit = false)
1735
*/
1836
trait FormatOutputTrait
1937
{
@@ -38,6 +56,24 @@ public function writeln($text, $quit = false, array $opts = [])
3856
return Show::writeln($text, $quit, $opts);
3957
}
4058

59+
/**
60+
* @inheritdoc
61+
* @see Show::block()
62+
*/
63+
public function block($messages, $type = 'MESSAGE', $style = Style::NORMAL, $quit = false)
64+
{
65+
return Show::block($messages, $type, $style, $quit);
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
* @see Show::liteBlock()
71+
*/
72+
public function liteBlock($messages, $type = 'MESSAGE', $style = Style::NORMAL, $quit = false)
73+
{
74+
return Show::liteBlock($messages, $type, $style, $quit);
75+
}
76+
4177
/**
4278
* @inheritdoc
4379
* @see Show::title()
@@ -130,94 +166,27 @@ public function progressBar($total, array $opts = [])
130166
}
131167

132168
/**
133-
* @param mixed $messages
134-
* @param string|null $type
135-
* @param string $style
136-
* @param int|boolean $quit If is int, setting it is exit code.
137-
* @return int
138-
*/
139-
public function block($messages, $type = 'MESSAGE', $style = Style::NORMAL, $quit = false)
140-
{
141-
return Show::block($messages, $type, $style, $quit);
142-
}
143-
144-
/**
145-
* @param mixed $messages
146-
* @param bool $quit
147-
* @return int
148-
*/
149-
public function primary($messages, $quit = false)
150-
{
151-
return $this->block($messages, 'IMPORTANT', Style::PRIMARY, $quit);
152-
}
153-
154-
/**
155-
* @param mixed $messages
156-
* @param bool $quit
157-
* @return int
158-
*/
159-
public function success($messages, $quit = false)
160-
{
161-
return $this->block($messages, 'SUCCESS', Style::SUCCESS, $quit);
162-
}
163-
164-
/**
165-
* @param mixed $messages
166-
* @param bool $quit
169+
* @param string $method
170+
* @param array $args
167171
* @return int
168172
*/
169-
public function info($messages, $quit = false)
173+
public function __call($method, array $args = [])
170174
{
171-
return $this->block($messages, 'INFO', Style::INFO, $quit);
172-
}
175+
$map = Show::getBlockMethods(false);
173176

174-
/**
175-
* @param mixed $messages
176-
* @param bool $quit
177-
* @return int
178-
*/
179-
public function note($messages, $quit = false)
180-
{
181-
return $this->block($messages, 'NOTE', Style::INFO, $quit);
182-
}
183-
184-
/**
185-
* @param mixed $messages
186-
* @param bool $quit
187-
* @return int
188-
*/
189-
public function notice($messages, $quit = false)
190-
{
191-
return $this->block($messages, 'NOTICE', Style::COMMENT, $quit);
192-
}
177+
if (isset($map[$method])) {
178+
$msg = $args[0];
179+
$quit = $args[1] ?? false;
180+
$style = $map[$method];
193181

194-
/**
195-
* @param mixed $messages
196-
* @param bool $quit
197-
* @return int
198-
*/
199-
public function warning($messages, $quit = false)
200-
{
201-
return $this->block($messages, 'WARNING', Style::WARNING, $quit);
202-
}
182+
if (0 === strpos($method, 'lite')) {
183+
$type = substr($method, 4);
184+
return Show::liteBlock($msg, $type === 'Primary' ? 'IMPORTANT' : $type, $style, $quit);
185+
}
203186

204-
/**
205-
* @param mixed $messages
206-
* @param bool $quit
207-
* @return int
208-
*/
209-
public function danger($messages, $quit = false)
210-
{
211-
return $this->block($messages, 'DANGER', Style::DANGER, $quit);
212-
}
187+
return Show::block($msg, $style === 'primary' ? 'IMPORTANT' : $style, $style, $quit);
188+
}
213189

214-
/**
215-
* @param mixed $messages
216-
* @param bool $quit
217-
* @return int
218-
*/
219-
public function error($messages, $quit = false)
220-
{
221-
return $this->block($messages, 'ERROR', Style::ERROR, $quit);
190+
throw new \LogicException("Call a not exists method: $method");
222191
}
223192
}

src/utils/Show.php

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@
1515
* show formatted message text
1616
*
1717
* @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)
1836
*/
1937
class Show
2038
{
@@ -74,48 +92,80 @@ public static function block($messages, $type = 'MESSAGE', $style = Style::NORMA
7492
$text = sprintf('<%s>%s</%s>', $style, $text, $style);
7593
}
7694

77-
// $this->write($text);
7895
return self::write($text, true, $quit);
7996
}
8097

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)
82106
{
83-
return static::block($messages, 'IMPORTANT', Style::PRIMARY, $quit);
84-
}
107+
$messages = is_array($messages) ? array_values($messages) : array($messages);
85108

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+
}
90113

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();
95116

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+
}
100120

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);
104122
}
105123

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+
];
110148

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 = [])
112155
{
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];
115160

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");
119169
}
120170

121171
/////////////////////////////////////////////////////////////////
@@ -839,4 +889,12 @@ public static function stderr($text, $nl = true, $quit = -200)
839889
]);
840890
}
841891

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+
}
842900
}

0 commit comments

Comments
 (0)