Skip to content

Commit e298f72

Browse files
committed
format codes, add more new class
1 parent 571d69b commit e298f72

19 files changed

+1201
-670
lines changed

src/App.php

+82-20
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
class App
4242
{
4343
/** @var self */
44-
public static $i;
44+
public static $global;
4545

4646
private const COMMAND_CONFIG = [
4747
'desc' => '',
@@ -55,7 +55,7 @@ class App
5555
/**
5656
* @var array
5757
*/
58-
private $metas = [
58+
protected $params = [
5959
'name' => 'My application',
6060
'desc' => 'My command line application',
6161
'version' => '0.2.1'
@@ -96,6 +96,18 @@ class App
9696
*/
9797
private $keyWidth = 12;
9898

99+
/**
100+
* @return static
101+
*/
102+
public static function global(): self
103+
{
104+
if (!self::$global) {
105+
throw new RuntimeException('please create global app by new App()');
106+
}
107+
108+
return self::$global;
109+
}
110+
99111
/**
100112
* Class constructor.
101113
*
@@ -105,15 +117,17 @@ class App
105117
public function __construct(array $config = [], array $argv = null)
106118
{
107119
// save self
108-
self::$i = $this;
120+
if (!self::$global) {
121+
self::$global = $this;
122+
}
109123

110124
// get current dir
111125
$this->pwd = (string)getcwd();
112126

113127
// parse cli argv
114128
$argv = $argv ?? $_SERVER['argv'];
115129
if ($config) {
116-
$this->setMetas($config);
130+
$this->setParams($config);
117131
}
118132

119133
// get script file
@@ -325,6 +339,11 @@ public function addCommand(string $command, callable $handler, $config = null):
325339

326340
$this->commands[$command] = $handler;
327341

342+
// no config
343+
if (!$config) {
344+
return;
345+
}
346+
328347
if (is_string($config)) {
329348
$desc = trim($config);
330349
$config = self::COMMAND_CONFIG;
@@ -344,18 +363,21 @@ public function addCommand(string $command, callable $handler, $config = null):
344363
*
345364
* @throws InvalidArgumentException
346365
*/
347-
public function commands(array $commands): void
366+
public function addCommands(array $commands): void
348367
{
349368
foreach ($commands as $command => $handler) {
350-
$desc = '';
369+
$conf = [];
370+
$name = is_string($command) ? $command : '';
371+
372+
if (is_array($handler) && isset($handler['handler'])) {
373+
$conf = $handler;
374+
$name = $conf['name'] ?? $name;
351375

352-
if (is_array($handler)) {
353-
$conf = array_values($handler);
354-
$handler = $conf[0];
355-
$desc = $conf[1] ?? '';
376+
$handler = $conf['handler'];
377+
unset($conf['name'], $conf['handler']);
356378
}
357379

358-
$this->addCommand($command, $handler, $desc);
380+
$this->addCommand($name, $handler, $conf);
359381
}
360382
}
361383

@@ -373,8 +395,8 @@ public function displayHelp(string $err = ''): void
373395
}
374396

375397
// help
376-
$desc = ucfirst($this->metas['desc']);
377-
if ($ver = $this->metas['version']) {
398+
$desc = ucfirst($this->params['desc']);
399+
if ($ver = $this->params['version']) {
378400
$desc .= "(<red>v$ver</red>)";
379401
}
380402

@@ -386,9 +408,10 @@ public function displayHelp(string $err = ''): void
386408
ksort($data);
387409

388410
foreach ($data as $command => $item) {
389-
$command = str_pad($command, $this->keyWidth, ' ');
390-
$desc = $item['desc'] ? ucfirst($item['desc']) : 'No description for the command';
391-
$help .= " <green>$command</green> $desc\n";
411+
$command = str_pad($command, $this->keyWidth);
412+
413+
$desc = $item['desc'] ? ucfirst($item['desc']) : 'No description for the command';
414+
$help .= " <green>$command</green> $desc\n";
392415
}
393416

394417
$help .= "\nFor command usage please run: <cyan>$script COMMAND -h</cyan>";
@@ -642,17 +665,56 @@ public function getPwd(): string
642665

643666
/**
644667
* @return array
668+
* @deprecated please use getParams();
645669
*/
646670
public function getMetas(): array
647671
{
648-
return $this->metas;
672+
return $this->getParams();
673+
}
674+
675+
/**
676+
* @param array $params
677+
*
678+
* @deprecated please use setParams()
679+
*/
680+
public function setMetas(array $params): void
681+
{
682+
$this->setParams($params);
683+
}
684+
685+
/**
686+
* @param string $key
687+
* @param null $default
688+
*
689+
* @return mixed|string|null
690+
*/
691+
public function getParam(string $key, $default = null)
692+
{
693+
return $this->params[$key] ?? $default;
694+
}
695+
696+
/**
697+
* @param string $key
698+
* @param mixed $val
699+
*/
700+
public function setParam(string $key, $val): void
701+
{
702+
$this->params[$key] = $val;
703+
}
704+
705+
/**
706+
* @return array
707+
*/
708+
public function getParams(): array
709+
{
710+
return $this->params;
649711
}
650712

651713
/**
652-
* @param array $metas
714+
* @param array $params
653715
*/
654-
public function setMetas(array $metas): void
716+
public function setParams(array $params): void
655717
{
656-
$this->metas = array_merge($this->metas, $metas);
718+
$this->params = array_merge($this->params, $params);
657719
}
658720
}

src/Cli.php

+50-8
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99

1010
namespace Toolkit\Cli;
1111

12+
use RuntimeException;
13+
use Toolkit\Cli\Color\Alert;
14+
use Toolkit\Cli\Color\Prompt;
1215
use Toolkit\Cli\Traits\ReadMessageTrait;
1316
use Toolkit\Cli\Traits\WriteMessageTrait;
1417
use function date;
1518
use function defined;
1619
use function function_exists;
1720
use function getenv;
1821
use function implode;
22+
use function is_array;
1923
use function is_numeric;
2024
use function json_encode;
2125
use function preg_replace;
@@ -32,19 +36,48 @@
3236
* Class Cli
3337
*
3438
* @package Toolkit\Cli
39+
*
40+
* @method static alert(string|array|mixed $message, string $style = 'info')
41+
* @method static prompt(string|array|mixed $message, string $style = 'info')
3542
*/
3643
class Cli
3744
{
3845
use ReadMessageTrait, WriteMessageTrait;
3946

40-
public const LOG_LEVEL2TAG = [
41-
'info' => 'info',
42-
'warn' => 'warning',
43-
'warning' => 'warning',
44-
'debug' => 'cyan',
45-
'notice' => 'notice',
46-
'error' => 'error',
47-
];
47+
/**
48+
* @param string $method {@see Color::STYLES}
49+
* @param array $args
50+
*/
51+
public static function __callStatic(string $method, array $args)
52+
{
53+
if ($method === 'alert') {
54+
Alert::global()->withStyle($args[1] ?? '')->println($args[0]);
55+
return;
56+
}
57+
if ($method === 'prompt') {
58+
Prompt::global()->withStyle($args[1] ?? '')->println($args[0]);
59+
return;
60+
}
61+
62+
if (isset(Color::STYLES[$method])) {
63+
echo Color::render($args[0], $method), "\n";
64+
}
65+
66+
throw new RuntimeException('call unknown method: ' . $method);
67+
}
68+
69+
/**
70+
* Print colored message to STDOUT
71+
*
72+
* @param string|array $message
73+
* @param string|array|null $style
74+
*/
75+
public static function colored($message, $style = 'info'): void
76+
{
77+
$str = is_array($message) ? implode(' ', $message) : (string)$message;
78+
79+
echo Color::render($str, $style) . PHP_EOL;
80+
}
4881

4982
/*******************************************************************************
5083
* color render
@@ -69,6 +102,15 @@ public static function color(string $text, $style = null): string
69102
return Color::render($text, $style);
70103
}
71104

105+
public const LOG_LEVEL2TAG = [
106+
'info' => 'info',
107+
'warn' => 'warning',
108+
'warning' => 'warning',
109+
'debug' => 'cyan',
110+
'notice' => 'notice',
111+
'error' => 'error',
112+
];
113+
72114
/**
73115
* print log to console
74116
*

0 commit comments

Comments
 (0)