Skip to content

Commit bc92caa

Browse files
committed
some update..
1 parent a623efe commit bc92caa

File tree

8 files changed

+156
-117
lines changed

8 files changed

+156
-117
lines changed

examples/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function fmtMsgCommand()
8484
'list' => 'List all group and independent commands',
8585
];
8686
Interact::panel($commands, 'Internal Commands', '');
87-
Interact::aList('Internal Commands', $commands);
87+
Interact::aList($commands, 'Internal Commands');
8888
}
8989

9090
/**

src/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ final protected function showCommandList()
208208
}
209209

210210
$commands[] = "\nFor more information please use: <info>$sName/help [command]</info>";
211-
$this->output->aList('<comment>Sub-Commands:</comment>', $commands);
211+
$this->output->aList($commands, '<comment>Sub-Commands:</comment>');
212212
}
213213

214214
/**

src/io/Input.php

Lines changed: 106 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,51 +54,32 @@ public function __construct($parseArgv = true, $fillToGlobal = false)
5454

5555
/**
5656
* 读取输入信息
57-
* @param string $message 若不为空,则先输出文本消息
57+
* @param string $question 若不为空,则先输出文本消息
5858
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
5959
* @return string
6060
*/
61-
public function read($message = null, $nl = false)
61+
public function read($question = null, $nl = false)
6262
{
63-
fwrite(STDOUT, $message . ($nl ? "\n" : ''));
63+
fwrite(STDOUT, $question . ($nl ? "\n" : ''));
6464

6565
return trim(fgets($this->inputStream));
6666
}
6767

68-
/**
69-
* @return string
70-
*/
71-
public function getScriptName()
72-
{
73-
return self::$scriptName;
74-
}
68+
/////////////////////////////////////////////////////////////////////////////////////////
69+
/// arguments (eg: name=john city=chengdu)
70+
/////////////////////////////////////////////////////////////////////////////////////////
7571

7672
/**
77-
* @return string
78-
*/
79-
public function getScript()
80-
{
81-
return self::$scriptName;
82-
}
83-
84-
/**
85-
* @return string
73+
* @param $name
74+
* @return bool
8675
*/
87-
public function getCommand()
76+
public function hasArg($name)
8877
{
89-
return self::$command;
78+
return isset($this->args[$name]);
9079
}
9180

9281
/**
93-
* @return array
94-
*/
95-
public function getArgs()
96-
{
97-
return $this->args;
98-
}
99-
100-
/**
101-
* @param null|string $name
82+
* @param null|int|string $name
10283
* @param mixed $default
10384
* @return mixed
10485
*/
@@ -119,6 +100,24 @@ public function get($name=null, $default = null)
119100
return isset($this->args[$name]) ? $this->args[$name] : $default;
120101
}
121102

103+
/**
104+
* get first argument
105+
* @return string
106+
*/
107+
public function getFirstArg()
108+
{
109+
return $this->get(0);
110+
}
111+
112+
/**
113+
* get second argument
114+
* @return string
115+
*/
116+
public function getSecondArg()
117+
{
118+
return $this->get(1);
119+
}
120+
122121
/**
123122
* @param $key
124123
* @param int $default
@@ -132,13 +131,26 @@ public function getInt($key, $default = 0)
132131
}
133132

134133
/**
135-
* @return array
134+
* get bool value form args
135+
* @param $key
136+
* @param bool $default
137+
* @return bool
136138
*/
137-
public function getOpts()
139+
public function getBool($key, $default = false)
138140
{
139-
return $this->opts;
141+
if ( !$this->hasArg($key) ) {
142+
return (bool)$default;
143+
}
144+
145+
$value = $this->args[$key];
146+
147+
return !in_array(strtolower($value), ['0', 'false'], true);
140148
}
141149

150+
/////////////////////////////////////////////////////////////////////////////////////////
151+
/// options (eg: -d --help)
152+
/////////////////////////////////////////////////////////////////////////////////////////
153+
142154
/**
143155
* @param $name
144156
* @param null $default
@@ -185,7 +197,7 @@ public function hasOpt($name)
185197
* @param bool $default
186198
* @return bool
187199
*/
188-
public function getBool($key, $default = false)
200+
public function boolOpt($key, $default = false)
189201
{
190202
return $this->getBoolOpt($key, $default);
191203
}
@@ -200,6 +212,66 @@ public function getBoolOpt($key, $default = false)
200212
return !in_array(strtolower($value), ['0', 'false'], true);
201213
}
202214

215+
/////////////////////////////////////////////////////////////////////////////////////////
216+
/// getter/setter
217+
/////////////////////////////////////////////////////////////////////////////////////////
218+
219+
/**
220+
* @return string
221+
*/
222+
public function getScriptName()
223+
{
224+
return self::$scriptName;
225+
}
226+
227+
/**
228+
* @return string
229+
*/
230+
public function getScript()
231+
{
232+
return self::$scriptName;
233+
}
234+
235+
/**
236+
* @return string
237+
*/
238+
public function getCommand()
239+
{
240+
return self::$command;
241+
}
242+
243+
/**
244+
* @param array $args
245+
*/
246+
public function setArgs(array $args)
247+
{
248+
$this->args = $args;
249+
}
250+
251+
/**
252+
* @return array
253+
*/
254+
public function getArgs()
255+
{
256+
return $this->args;
257+
}
258+
259+
/**
260+
* @param array $opts
261+
*/
262+
public function setOpts(array $opts)
263+
{
264+
$this->opts = $opts;
265+
}
266+
267+
/**
268+
* @return array
269+
*/
270+
public function getOpts()
271+
{
272+
return $this->opts;
273+
}
274+
203275
/**
204276
* @return resource
205277
*/

src/io/Output.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function section($title, $body, array $opts = [])
6363
* @inheritdoc
6464
* @see Interact::aList()
6565
*/
66-
public function aList($title, array $data, array $opts = [])
66+
public function aList($data, $title, array $opts = [])
6767
{
68-
Interact::aList($title, $data, $opts);
68+
Interact::aList($data, $title, $opts);
6969
}
7070

7171
/**
@@ -76,14 +76,18 @@ public function multiList(array $data, array $opts = [])
7676
{
7777
Interact::multiList($data, $opts);
7878
}
79+
public function mList(array $data, array $opts = [])
80+
{
81+
Interact::multiList($data, $opts);
82+
}
7983

8084
/**
8185
* helpPanel
8286
* @inheritdoc
8387
* @see Interact::helpPanel()
8488
*/
8589
public function helpPanel(
86-
$usage, array $commands = [], array $options = [], array $examples = [],
90+
$usage, $commands = null, $options = null, $examples = null,
8791
$description = '', $showAfterQuit = true
8892
) {
8993
Interact::helpPanel($usage, $commands, $options, $examples, $description, $showAfterQuit);
@@ -148,14 +152,14 @@ public function notice($messages, $quit = false)
148152

149153
/**
150154
* 读取输入信息
151-
* @param string $message 若不为空,则先输出文本
155+
* @param string $question 若不为空,则先输出文本
152156
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
153157
* @return string
154158
*/
155-
public function read($message = null, $nl = false)
159+
public function read($question = null, $nl = false)
156160
{
157-
if ( $message ) {
158-
$this->write($message, $nl);
161+
if ($question) {
162+
$this->write($question, $nl);
159163
}
160164

161165
return trim(fgets(STDIN));

src/utils/Download.php

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ class Download
1717
const PROGRESS_TEXT = 1;
1818
const PROGRESS_BAR = 2;
1919

20-
private static $fileSize = null;
20+
/**
21+
* @var int
22+
*/
23+
private static $fileSize;
24+
25+
/**
26+
* @var int
27+
*/
2128
private static $showType = 1;
2229

2330
/*
@@ -131,56 +138,6 @@ protected static function showProgressByType($transferredBytes)
131138
... ...
132139
*/
133140

134-
/**
135-
* @param int $notifyCode stream notify code
136-
* @param int $severity severity code
137-
* @param string $message Message text
138-
* @param int $messageCode Message code
139-
* @param int $transferredBytes Have been transferred bytes
140-
* @param int $maxBytes Target max length bytes
141-
*/
142-
protected static function progressText($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
143-
{
144-
$msg = '';
145-
switch($notifyCode) {
146-
case STREAM_NOTIFY_RESOLVE:
147-
case STREAM_NOTIFY_AUTH_REQUIRED:
148-
case STREAM_NOTIFY_COMPLETED:
149-
case STREAM_NOTIFY_FAILURE:
150-
case STREAM_NOTIFY_AUTH_RESULT:
151-
// var_dump($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes);
152-
$msg = "NOTIFY: $message(NO: $messageCode, Severity: $severity)";
153-
/* Ignore */
154-
break;
155-
156-
case STREAM_NOTIFY_REDIRECTED:
157-
$msg = "Being redirected to: $message";
158-
break;
159-
160-
case STREAM_NOTIFY_CONNECT:
161-
$msg = 'Connected...';
162-
break;
163-
164-
case STREAM_NOTIFY_FILE_SIZE_IS:
165-
$fileSize = sprintf('%2d',$maxBytes/1024);
166-
$msg = "Got the file size: <info>$fileSize</info> kb";
167-
break;
168-
169-
case STREAM_NOTIFY_MIME_TYPE_IS:
170-
$msg = "Found the mime-type: <info>$message</info>";
171-
break;
172-
173-
case STREAM_NOTIFY_PROGRESS:
174-
if ( $transferredBytes > 0 ) {
175-
printf("\r\rMade some progress, downloaded %2d kb so far", $transferredBytes/1024);
176-
//$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
177-
}
178-
break;
179-
}
180-
181-
$msg && Show::write($msg);
182-
}
183-
184141
/**
185142
* eg: php down.php <http://example.com/file> <localFile>
186143
* @param string $url
@@ -191,8 +148,9 @@ public static function down($url, $saveAs, $type = self::PROGRESS_TEXT)
191148
{
192149
self::$showType = (int)$type;
193150
$ctx = stream_context_create();
151+
152+
// register stream notification callback
194153
stream_context_set_params($ctx, [
195-
// register stream notification callback
196154
'notification' => [ self::class, 'progressShow']
197155
]);
198156

src/utils/ProgressBar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function display()
101101
private function setMaxSteps($max)
102102
{
103103
$this->max = max(0, (int) $max);
104-
$this->stepWidth = $this->max ? Helper::strlen($this->max) : 4;
104+
$this->stepWidth = $this->max ? Helper::strLen($this->max) : 4;
105105
}
106106

107107
/**

0 commit comments

Comments
 (0)