Skip to content

Commit 0d00a70

Browse files
committed
update readme, add new method: askHiddenInput() for read password
1 parent 163afd2 commit 0d00a70

14 files changed

+391
-60
lines changed

README.md

+39-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ $app->run();
8080

8181
然后在命令行里执行 `php examples/app`, 立即就可以看到如下输出了:
8282

83-
!['output-commands-info'](images/output-commands-info.jpg)
83+
!['output-commands-info'](images/example-app.png)
8484

8585
> `Independent Commands` 中的 demo 就是我们上面添加的命令
8686
@@ -194,7 +194,7 @@ class HomeController extends Controller
194194
195195
- 运行效果(by `php examples/app home`):
196196

197-
![command-group-example](./images/command-group-example.jpg)
197+
![command-group-example](./images/example-for-group.png)
198198

199199
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app` 来查看效果
200200

@@ -527,6 +527,8 @@ Show::table($data, 'a table', $opts);
527527

528528
> 渲染效果请看下面的预览
529529
530+
![table-show](images/table-show.png)
531+
530532
### 快速的渲染一个帮助信息面板
531533

532534
```php
@@ -558,7 +560,41 @@ Show::helpPanel([
558560

559561
## 用户交互方法
560562

561-
需引入类 `Inhere\Console\Utils\Interact`
563+
> 要独立使用的话需引入类 `Inhere\Console\Utils\Interact``Controller``Command` 里可以直接调用相关方法
564+
565+
### 读取用户输入
566+
567+
```php
568+
public static function read($message = null, $nl = false, array $opts = []): string
569+
```
570+
571+
- 使用
572+
573+
```php
574+
$userInput = Interact::read();
575+
576+
// 先输出消息,再读取
577+
$userInput = Interact::read('Your name:');
578+
579+
// 在 Controller/Command 中
580+
$userInput = $this->read('Your name:');
581+
```
582+
583+
### 读取密码输入(隐藏输入文字)
584+
585+
```php
586+
public static function askHiddenInput(string $prompt = 'Enter Password:'): string
587+
public static function askPassword(string $prompt = 'Enter Password:'): string
588+
```
589+
590+
- 使用
591+
592+
```php
593+
$pwd = Interact::askPassword();
594+
595+
// 在 Controller/Command 中
596+
$pwd = $this->askPassword();
597+
```
562598

563599
### 从给出的列表中选择一项
564600

README_en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ $app->run();
6868

6969
now, you can see:
7070

71-
!['output-commands-info'](images/output-commands-info.jpg)
71+
!['output-commands-info'](images/example-app.png)
7272

7373
## input
7474

examples/HomeController.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,20 @@ public function indexCommand()
3737
$this->write('hello, welcome!! this is ' . __METHOD__);
3838
}
3939

40+
/**
41+
* a example for input password on command line
42+
* @usage {fullCommand}
43+
*/
44+
public function passwdCommand()
45+
{
46+
$pwd = $this->askPassword();
47+
48+
$this->write('Your input is:' . $pwd);
49+
}
50+
4051
/**
4152
* a example for use color text output on command
42-
* @usage ./bin/app home/color
53+
* @usage {fullCommand}
4354
*/
4455
public function colorCommand()
4556
{
@@ -67,7 +78,7 @@ public function blockMsgCommand()
6778
{
6879
$this->write('block message:');
6980

70-
foreach (Interact::getBlockMethods() as $type) {
81+
foreach (Show::getBlockMethods() as $type) {
7182
$this->output->$type("$type style message text");
7283
}
7384

images/command-group-example.jpg

-79.8 KB
Binary file not shown.

images/example-app.png

359 KB
Loading

images/example-for-group.png

282 KB
Loading

images/output-color-text.png

-107 KB
Loading

images/output-commands-info.jpg

-71.5 KB
Binary file not shown.

src/Base/AbstractCommand.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ protected function createDefinition()
105105
return $this->definition;
106106
}
107107

108+
/**
109+
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
110+
* @return array
111+
*/
112+
public function annotationVars()
113+
{
114+
// e.g: `more info see {name}/index`
115+
return [
116+
'script' => $this->input->getScript(),
117+
'command' => $this->input->getCommand(),
118+
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
119+
'name' => self::getName(),
120+
];
121+
}
122+
108123
/**************************************************************************
109124
* running a command
110125
**************************************************************************/
@@ -283,20 +298,6 @@ public function validateInput()
283298
* helper methods
284299
**************************************************************************/
285300

286-
/**
287-
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
288-
* @return array
289-
*/
290-
protected function annotationVars()
291-
{
292-
// e.g: `more info see {name}/index`
293-
return [
294-
'script' => $this->input->getScript(),
295-
'command' => $this->input->getCommand(),
296-
'name' => self::getName(),
297-
];
298-
}
299-
300301
/**
301302
* 为命令注解提供可解析解析变量. 可以在命令的注释中使用
302303
* @param string $str

src/BuiltIn/Resources/exe/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# exe file
2+
3+
## Hidden Input
4+
5+
From https://github.com/Seldaek/hidden-input
9 KB
Binary file not shown.

src/Traits/UserInteractTrait.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
/**
1414
* Class UserInteractTrait
1515
* @package Inhere\Console\Traits
16+
* @see Interact
17+
*
18+
* @method string readRow($message = null, $nl = false)
19+
* @method string read($message = null, $nl = false, array $opts = [])
20+
*
21+
* @method string askHiddenInput(string $prompt = 'Enter Password:')
22+
* @method string promptSilent(string $prompt = 'Enter Password:')
23+
* @method string askPassword(string $prompt = 'Enter Password:')
1624
*/
1725
trait UserInteractTrait
1826
{
@@ -61,18 +69,23 @@ public function question($question, $default = null, \Closure $validator = null)
6169
* @inheritdoc
6270
* @see Interact::limitedAsk()
6371
*/
64-
public function loopAsk($question, $default = null, \Closure $validator = null, $times = 3)
72+
public function limitedAsk($question, $default = null, \Closure $validator = null, $times = 3)
6573
{
6674
return Interact::limitedAsk($question, $default, $validator, $times);
6775
}
6876

6977
/**
70-
* @inheritdoc
71-
* @see Interact::limitedAsk()
78+
* @param string $method
79+
* @param array $args
80+
* @return int
81+
* @throws \LogicException
7282
*/
73-
public function limitedAsk($question, $default = null, \Closure $validator = null, $times = 3)
83+
public function __call($method, array $args = [])
7484
{
75-
return Interact::limitedAsk($question, $default, $validator, $times);
76-
}
85+
if (method_exists(Interact::class, $method)) {
86+
return Interact::$method(...$args);
87+
}
7788

89+
throw new \LogicException("Call a not exists method: $method of the " . static::class);
90+
}
7891
}

0 commit comments

Comments
 (0)