Skip to content

Commit 47cee13

Browse files
committed
move some format methods to class from Show::class
1 parent 334a9c6 commit 47cee13

File tree

18 files changed

+909
-666
lines changed

18 files changed

+909
-666
lines changed

examples/Controller/ShowController.php

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88

99
namespace Inhere\Console\Examples\Controller;
1010

11+
use Inhere\Console\Component\Formatter\HelpPanel;
12+
use Inhere\Console\Component\Formatter\Panel;
1113
use Inhere\Console\Component\Symbol\Char;
1214
use Inhere\Console\Component\Symbol\Emoji;
13-
use Toolkit\Cli\Color;
1415
use Inhere\Console\Controller;
1516
use Inhere\Console\IO\Input;
1617
use Inhere\Console\Util\Show;
18+
use Toolkit\Cli\Color;
1719
use Toolkit\Cli\Highlighter;
1820

1921
/**
@@ -28,9 +30,9 @@ class ShowController extends Controller
2830
public static function commandAliases(): array
2931
{
3032
return [
31-
'hp' => 'helpPanel',
33+
'hp' => 'helpPanel',
3234
'hpl' => 'helpPanel',
33-
'hl' => 'highlight',
35+
'hl' => 'highlight',
3436
];
3537
}
3638

@@ -93,8 +95,8 @@ public function panelCommand(): void
9395
{
9496
$data = [
9597
'application version' => '1.2.0',
96-
'system version' => '5.2.3',
97-
'key' => 'value ...',
98+
'system version' => '5.2.3',
99+
'key' => 'value ...',
98100
'a only value message text',
99101
];
100102

@@ -105,6 +107,13 @@ public function panelCommand(): void
105107
Show::panel($data, 'panel show', [
106108
'borderChar' => '='
107109
]);
110+
111+
Panel::create([
112+
'data' => $data,
113+
'title' => 'panel show',
114+
'titleBorder' => '=',
115+
'footBorder' => '=',
116+
])->display();
108117
}
109118

110119
/**
@@ -209,7 +218,7 @@ public function highlightCommand($in): void
209218
{
210219
// $file = $this->app->getRootPath() . '/examples/routes.php';
211220
$file = $this->app->getRootPath() . '/src/Utils/Show.php';
212-
$src = \file_get_contents($file);
221+
$src = \file_get_contents($file);
213222

214223
$code = Highlighter::create()->highlight($src, $in->getBoolOpt('ln'));
215224

@@ -222,19 +231,19 @@ public function highlightCommand($in): void
222231
public function helpPanelCommand(): void
223232
{
224233
Show::helpPanel([
225-
Show::HELP_DES => 'a help panel description text. (help panel show)',
226-
Show::HELP_USAGE => 'a usage text',
227-
Show::HELP_ARGUMENTS => [
234+
HelpPanel::DESC => 'a help panel description text. (help panel show)',
235+
HelpPanel::USAGE => 'a usage text',
236+
HelpPanel::ARGUMENTS => [
228237
'arg1' => 'arg1 description',
229238
'arg2' => 'arg2 description',
230239
],
231-
Show::HELP_OPTIONS => [
232-
'--opt1' => 'a long option',
233-
'-s' => 'a short option',
234-
'-d' => 'Run the server on daemon.(default: <comment>false</comment>)',
240+
HelpPanel::OPTIONS => [
241+
'--opt1' => 'a long option',
242+
'-s' => 'a short option',
243+
'-d' => 'Run the server on daemon.(default: <comment>false</comment>)',
235244
'-h, --help' => 'Display this help message'
236245
],
237-
], false);
246+
]);
238247
}
239248

240249
/**
@@ -272,22 +281,22 @@ public function jsonCommand(): void
272281
{
273282
$data = [
274283
[
275-
'id' => 1,
276-
'name' => 'john',
284+
'id' => 1,
285+
'name' => 'john',
277286
'status' => 2,
278-
'email' => '[email protected]',
287+
'email' => '[email protected]',
279288
],
280289
[
281-
'id' => 2,
282-
'name' => 'tom',
290+
'id' => 2,
291+
'name' => 'tom',
283292
'status' => 0,
284-
'email' => '[email protected]',
293+
'email' => '[email protected]',
285294
],
286295
[
287-
'id' => 3,
288-
'name' => 'jack',
296+
'id' => 3,
297+
'name' => 'jack',
289298
'status' => 1,
290-
'email' => '[email protected]',
299+
'email' => '[email protected]',
291300
],
292301
];
293302

src/Component/Formatter/Alert.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,4 @@
1414
*/
1515
class Alert extends Formatter
1616
{
17-
/**
18-
* @return string
19-
*/
20-
public function toString(): string
21-
{
22-
return '';
23-
}
2417
}

src/Component/Formatter/Block.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Class Block
1313
* @package Inhere\Console\Component\Formatter
1414
*/
15-
class Block
15+
class Block extends Formatter
1616
{
1717

1818
}

src/Component/Formatter/Formatter.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88

99
namespace Inhere\Console\Component\Formatter;
1010

11+
use Inhere\Console\Util\Show;
1112
use Toolkit\PhpUtil\PhpHelper;
1213

1314
/**
1415
* Class Formatter - message formatter
1516
* @package Inhere\Console\Component\Formatter
1617
*/
17-
abstract class Formatter
18+
abstract class Formatter implements FormatterInterface
1819
{
1920
// content align
2021
public const ALIGN_LEFT = 'left';
@@ -26,7 +27,11 @@ abstract class Formatter
2627
*/
2728
protected $config = [];
2829

29-
public static function create(array $config = [])
30+
/**
31+
* @param array $config
32+
* @return Formatter
33+
*/
34+
public static function create(array $config = []): Formatter
3035
{
3136
return new static($config);
3237
}
@@ -50,15 +55,41 @@ public function __toString()
5055
return $this->toString();
5156
}
5257

53-
public function render(): void
58+
/**
59+
* @return string
60+
*/
61+
public function format(): string
5462
{
63+
throw new \RuntimeException('Please implement the method on sub-class');
64+
}
5565

66+
/**
67+
* Format and output message to steam.
68+
*
69+
* @return int
70+
*/
71+
public function render(): int
72+
{
73+
return $this->display();
74+
}
75+
76+
/**
77+
* Format and output message to steam.
78+
*
79+
* @return int
80+
*/
81+
public function display(): int
82+
{
83+
return Show::write($this->toString());
5684
}
5785

5886
/**
5987
* @return string
6088
*/
61-
abstract public function toString(): string;
89+
public function toString(): string
90+
{
91+
return $this->format();
92+
}
6293

6394
/**
6495
* @return array

src/Component/Formatter/FormatterInterface.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,18 @@
1414
*/
1515
interface FormatterInterface
1616
{
17+
public const FINISHED = -1;
1718

19+
public const CHAR_SPACE = ' ';
20+
public const CHAR_HYPHEN = '-';
21+
public const CHAR_UNDERLINE = '_';
22+
public const CHAR_VERTICAL = '|';
23+
public const CHAR_EQUAL = '=';
24+
public const CHAR_STAR = '*';
25+
26+
public const POS_LEFT = 'l';
27+
public const POS_MIDDLE = 'm';
28+
public const POS_RIGHT = 'r';
29+
30+
public function format(): string;
1831
}

src/Component/Formatter/HelpPanel.php

Lines changed: 106 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,122 @@
88

99
namespace Inhere\Console\Component\Formatter;
1010

11+
use Inhere\Console\Util\FormatUtil;
12+
use Inhere\Console\Util\Show;
13+
1114
/**
1215
* Class HelpPanel
1316
* - method version please {@see \Inhere\Console\Util\Show::helpPanel()}
1417
* @package Inhere\Console\Component\Formatter
1518
*/
1619
class HelpPanel extends Formatter
1720
{
21+
/**
22+
* help panel keys
23+
*/
24+
public const DESC = 'description';
25+
public const USAGE = 'usage';
26+
public const COMMANDS = 'commands';
27+
public const ARGUMENTS = 'arguments';
28+
public const OPTIONS = 'options';
29+
public const EXAMPLES = 'examples';
30+
public const EXTRAS = 'extras';
1831

1932
/**
20-
* @return string
33+
* Show console help message
34+
* @param array $config The config data
35+
* There are config structure. you can setting some or ignore some. will only render it when value is not empty.
36+
* [
37+
* description string The description text. e.g 'Composer version 1.3.2'
38+
* usage string The usage message text. e.g 'command [options] [arguments]'
39+
* commands array|string The command list. e.g:
40+
* [
41+
* // command => description
42+
* 'start' => 'Start the app server',
43+
* ... ...
44+
* ]
45+
* arguments array|string The argument list. e.g:
46+
* [
47+
* // argument => description
48+
* 'name' => 'Your name',
49+
* 'city' => 'Your city name'
50+
* ... ...
51+
* ]
52+
* options array|string The option list. e.g:
53+
* [
54+
* // option => description
55+
* '-d' => 'Run the server on daemon.(default: <comment>false</comment>)',
56+
* '-h, --help' => 'Display this help message'
57+
* ... ...
58+
* ]
59+
* examples array|string The command usage example. e.g 'php server.php {start|reload|restart|stop} [-d]'
60+
* ]
2161
*/
22-
public function toString(): string
62+
public static function show(array $config): void
2363
{
24-
return '';
64+
$parts = [];
65+
$option = [
66+
'indentDes' => ' ',
67+
];
68+
$config = \array_merge([
69+
'description' => '',
70+
'usage' => '',
71+
72+
'commands' => [],
73+
'arguments' => [],
74+
'options' => [],
75+
76+
'examples' => [],
77+
78+
// extra
79+
'extras' => [],
80+
81+
'_opts' => [],
82+
], $config);
83+
84+
// some option for show.
85+
if (isset($config['_opts'])) {
86+
$option = \array_merge($option, $config['_opts']);
87+
unset($config['_opts']);
88+
}
89+
90+
// description
91+
if ($config['description']) {
92+
$parts[] = "{$option['indentDes']}{$config['description']}\n";
93+
unset($config['description']);
94+
}
95+
96+
// now, render usage,commands,arguments,options,examples ...
97+
foreach ($config as $section => $value) {
98+
if (!$value) {
99+
continue;
100+
}
101+
102+
// if $value is array, translate array to string
103+
if (\is_array($value)) {
104+
// is natural key ['text1', 'text2'](like usage,examples)
105+
if (isset($value[0])) {
106+
$value = \implode(\PHP_EOL . ' ', $value);
107+
108+
// is key-value [ 'key1' => 'text1', 'key2' => 'text2']
109+
} else {
110+
$value = FormatUtil::spliceKeyValue($value, [
111+
'leftChar' => ' ',
112+
'sepChar' => ' ',
113+
'keyStyle' => 'info',
114+
]);
115+
}
116+
}
117+
118+
if (\is_string($value)) {
119+
$value = \trim($value);
120+
$section = \ucfirst($section);
121+
$parts[] = "<comment>$section</comment>:\n {$value}\n";
122+
}
123+
}
124+
125+
if ($parts) {
126+
Show::write(\implode("\n", $parts), false);
127+
}
25128
}
26129
}

0 commit comments

Comments
 (0)