Skip to content

Commit 2de72fe

Browse files
committed
update
1 parent f80f057 commit 2de72fe

File tree

3 files changed

+134
-52
lines changed

3 files changed

+134
-52
lines changed

examples/HomeController.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,9 @@ public function downCommand()
171171
return 0;
172172
}
173173

174-
Download::down(
175-
$url,
176-
$saveAs,
177-
$type === 'bar' ? Download::PROGRESS_BAR : Download::PROGRESS_TEXT
178-
);
174+
$d = Download::down($url, $saveAs, $type);
175+
176+
var_dump($d);
179177

180178
return 0;
181179
}

src/io/Input.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ public function getInt($key, $default = 0): int
138138
*/
139139
public function getBool($key, $default = false): bool
140140
{
141-
if ( !$this->hasArg($key) ) {
141+
if (!$this->hasArg($key)) {
142142
return (bool)$default;
143143
}
144144

145145
$value = strtolower($this->args[$key]);
146146

147-
return 'true' === $value || 'on' === $value;
147+
return 'true' === $value || 'yes' === $value || 'on' === $value;
148148
}
149149

150150
/////////////////////////////////////////////////////////////////////////////////////////
@@ -372,9 +372,9 @@ protected static function parseOption($item, &$opts)
372372
$tVal = strtolower($val);
373373

374374
// check it is a bool value.
375-
if ($tVal === 'on' || $tVal === 'true') {
375+
if ($tVal === 'on' || $tVal === 'yes' || $tVal === 'true') {
376376
$opts[$name] = true;
377-
} elseif ($tVal === 'off' || $tVal === 'false') {
377+
} elseif ($tVal === 'off' || $tVal === 'no' || $tVal === 'false') {
378378
$opts[$name] = false;
379379

380380
// is array. eg: `--id=23 --id=154`

src/utils/Download.php

Lines changed: 127 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,85 @@
1414
*/
1515
class Download
1616
{
17-
const PROGRESS_TEXT = 1;
18-
const PROGRESS_BAR = 2;
17+
const PROGRESS_TEXT = 'text';
18+
const PROGRESS_BAR = 'bar';
1919

2020
/**
2121
* @var int
2222
*/
23-
private static $fileSize;
23+
private $fileSize;
2424

2525
/**
2626
* @var int
2727
*/
28-
private static $showType = 1;
28+
private $showType;
29+
30+
/**
31+
* @var string
32+
*/
33+
public $url;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $saveAs;
39+
40+
41+
/**
42+
* eg: php down.php <http://example.com/file> <localFile>
43+
* @param string $url
44+
* @param string $saveAs
45+
* @param string $type
46+
* @return Download
47+
*/
48+
public static function down(string $url, string $saveAs, string $type = self::PROGRESS_TEXT)
49+
{
50+
$d = new self($url, $saveAs, $type);
51+
52+
return $d->start();
53+
}
54+
55+
/**
56+
* Download constructor.
57+
* @param string $url
58+
* @param string $saveAs
59+
* @param string $type
60+
*/
61+
public function __construct(string $url, string $saveAs, $type = self::PROGRESS_TEXT)
62+
{
63+
$this->url = $url;
64+
$this->saveAs = $saveAs;
65+
$this->showType = $type === self::PROGRESS_BAR ? self::PROGRESS_BAR : self::PROGRESS_TEXT;
66+
}
67+
68+
public function start()
69+
{
70+
if (!$this->url || !$this->saveAs) {
71+
Show::error("Please the property 'url' and 'saveAs'.", 1);
72+
}
73+
74+
$ctx = stream_context_create();
75+
76+
// register stream notification callback
77+
stream_context_set_params($ctx, [
78+
'notification' => [ $this, 'progressShow']
79+
]);
80+
81+
Show::write("Download: {$this->url}\nSave As: {$this->saveAs}\n");
82+
83+
$fp = fopen($this->url, 'rb', false, $ctx);
84+
85+
if (is_resource($fp) && file_put_contents($this->saveAs, $fp)) {
86+
Show::write("\nDone!");
87+
} else {
88+
$err = error_get_last();
89+
Show::error("\nErr.rrr..orr...\n {$err['message']}\n", 1);
90+
}
91+
92+
$this->fileSize = null;
93+
94+
return $this;
95+
}
2996

3097
/*
3198
progressBar() OUT:
@@ -36,7 +103,7 @@ class Download
36103
FileSize: 7773024
37104
Mime-type: application/octet-stream
38105
[========================================> ] 40% (3076/7590 kb)
39-
*/
106+
*/
40107

41108
/**
42109
* @param int $notifyCode stream notify code
@@ -46,7 +113,7 @@ class Download
46113
* @param int $transferredBytes Have been transferred bytes
47114
* @param int $maxBytes Target max length bytes
48115
*/
49-
protected static function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
116+
protected function progressShow($notifyCode, $severity, $message, $messageCode, $transferredBytes, $maxBytes)
50117
{
51118
$msg = '';
52119

@@ -69,7 +136,7 @@ protected static function progressShow($notifyCode, $severity, $message, $messag
69136
break;
70137

71138
case STREAM_NOTIFY_FILE_SIZE_IS:
72-
self::$fileSize = $maxBytes;
139+
$this->fileSize = $maxBytes;
73140
$fileSize = sprintf('%2d',$maxBytes/1024);
74141
$msg = "Got the file size: <info>$fileSize</info> kb";
75142
break;
@@ -80,7 +147,7 @@ protected static function progressShow($notifyCode, $severity, $message, $messag
80147

81148
case STREAM_NOTIFY_PROGRESS:
82149
if ($transferredBytes > 0) {
83-
self::showProgressByType($transferredBytes);
150+
$this->showProgressByType($transferredBytes);
84151
}
85152

86153
break;
@@ -93,24 +160,23 @@ protected static function progressShow($notifyCode, $severity, $message, $messag
93160
* @param $transferredBytes
94161
* @return string
95162
*/
96-
protected static function showProgressByType($transferredBytes)
163+
protected function showProgressByType($transferredBytes)
97164
{
98165
if ($transferredBytes <= 0) {
99166
return '';
100167
}
101168

102169
$tfKb = $transferredBytes/1024;
103170

104-
if ( self::$showType === self::PROGRESS_BAR ) {
105-
$size = self::$fileSize;
171+
if ($this->showType === self::PROGRESS_BAR) {
172+
$size = $this->fileSize;
106173

107-
if ( $size === null ) {
174+
if ($size === null) {
108175
printf("\rUnknown file size... %2d kb done..", $tfKb);
109176
} else {
110177
$length = ceil(($transferredBytes/$size)*100); // ■ =
111178
printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat('=', $length). '>', $length, $tfKb, $size/1024);
112179
}
113-
114180
} else {
115181
printf("\r\rMade some progress, downloaded %2d kb so far", $tfKb);
116182
//$msg = "Made some progress, downloaded <info>$transferredBytes</info> so far";
@@ -119,6 +185,54 @@ protected static function showProgressByType($transferredBytes)
119185
return '';
120186
}
121187

188+
/**
189+
* @return int
190+
*/
191+
public function getShowType(): int
192+
{
193+
return $this->showType;
194+
}
195+
196+
/**
197+
* @param int $showType
198+
*/
199+
public function setShowType(int $showType)
200+
{
201+
$this->showType = $showType;
202+
}
203+
204+
/**
205+
* @return string
206+
*/
207+
public function getUrl(): string
208+
{
209+
return $this->url;
210+
}
211+
212+
/**
213+
* @param string $url
214+
*/
215+
public function setUrl(string $url)
216+
{
217+
$this->url = $url;
218+
}
219+
220+
/**
221+
* @return string
222+
*/
223+
public function getSaveAs(): string
224+
{
225+
return $this->saveAs;
226+
}
227+
228+
/**
229+
* @param string $saveAs
230+
*/
231+
public function setSaveAs(string $saveAs)
232+
{
233+
$this->saveAs = $saveAs;
234+
}
235+
122236
/*
123237
progressText() OUT:
124238
Connected...
@@ -138,35 +252,5 @@ protected static function showProgressByType($transferredBytes)
138252
... ...
139253
*/
140254

141-
/**
142-
* eg: php down.php <http://example.com/file> <localFile>
143-
* @param string $url
144-
* @param string $saveAs
145-
* @param int $type
146-
*/
147-
public static function down($url, $saveAs, $type = self::PROGRESS_TEXT)
148-
{
149-
self::$showType = (int)$type;
150-
$ctx = stream_context_create();
151-
152-
// register stream notification callback
153-
stream_context_set_params($ctx, [
154-
'notification' => [ self::class, 'progressShow']
155-
]);
156-
157-
Show::write("Download: $url\nSave As: $saveAs \n");
158-
159-
$fp = fopen($url, 'rb', false, $ctx);
160-
161-
if (is_resource($fp) && file_put_contents($saveAs, $fp)) {
162-
self::$fileSize = null;
163-
164-
Show::write("\nDone!", true, 0);
165-
}
166-
167-
self::$fileSize = null;
168-
$err = error_get_last();
169-
Show::error("\nErr.rrr..orr...\n {$err['message']}\n", 1);
170-
}
171255

172256
}

0 commit comments

Comments
 (0)