Skip to content

Commit a475ab3

Browse files
committed
refactor code
1 parent 64b99ab commit a475ab3

File tree

5 files changed

+29
-32
lines changed

5 files changed

+29
-32
lines changed

src/Qiniu/Config.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ final class Config
1212
const RSF_HOST = 'http://rsf.qbox.me'; // 列举操作Host
1313
const API_HOST = 'http://api.qiniu.com'; // 数据处理操作Host
1414

15-
public static $upHost = 'http://up.qiniu.com'; // 默认上传Host
15+
public static $upHost; // 上传Host
1616
public static $zone; // 设置上传的Zone
1717

18-
public function __construct(Zone $zone=null) { // 构造函数,默认为zone0
19-
if ($zone!==null) {
20-
self::$zone = $zone;
21-
self::$upHost = self::$zone->upHost;
22-
}
18+
public function __construct() { // 构造函数,默认为zone0
19+
self::setZone(Zone::zone0());
20+
return;
21+
}
22+
23+
public static function setZone(Zone $z) {
24+
self::$zone = $z;
25+
self::$upHost = $z::$upHost;
2326
return;
2427
}
2528

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@ public function __construct(
4343
$inputStream,
4444
$size,
4545
$params,
46-
$mime
46+
$mime,
47+
$config
4748
) {
4849
$this->upToken = $upToken;
4950
$this->key = $key;
5051
$this->inputStream = $inputStream;
5152
$this->size = $size;
5253
$this->params = $params;
5354
$this->mime = $mime;
54-
$this->host = Config::$defaultHost;
55+
$this->host = $config::$upHost;
5556
$this->contexts = array();
5657
}
5758

src/Qiniu/Storage/UploadManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function putFile(
116116
$size,
117117
$params,
118118
$mime,
119-
$checkCrc
119+
$this->config
120120
);
121121
return $up->upload();
122122
}

src/Qiniu/Zone.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,21 @@
33

44
final Class Zone
55
{
6-
const ZONE0 = 0, // Zone 常量, 现在有两个zone可以设置,zone0 & zone1
7-
ZONE1 = 1;
6+
public static $upHost = 'http://up.qiniu.com';
87

9-
public $upHost;
10-
11-
public function __construct($upHost) {
8+
public function __construct($upHost)
9+
{
1210
$this->upHost = $upHost;
1311
}
1412

15-
public static function zone0() {
16-
return self::zone(self::ZONE0);
17-
}
18-
19-
public static function zone1() {
20-
return self::zone(self::ZONE1);
13+
public static function zone0()
14+
{
15+
return new self('http://up.qiniu.com');
2116
}
2217

23-
public static function zone($z) {
24-
switch ($z) {
25-
case self::ZONE0:
26-
return new self('http://up.qiniu.com');
27-
break;
28-
case self::ZONE1:
29-
return new self('http://up-z1.qiniu.com');
30-
break;
31-
}
32-
return;
18+
public static function zone1()
19+
{
20+
return new self('http://up-z1.qiniu.com');
3321
}
3422
}
3523

tests/Qiniu/Tests/FormUpTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33

44
use Qiniu\Storage\FormUploader;
55
use Qiniu\Storage\UploadManager;
6+
use Qiniu\Config;
67

78
class FormUpTest extends \PHPUnit_Framework_TestCase
89
{
910
protected $bucketName;
1011
protected $auth;
12+
protected $config;
13+
1114
protected function setUp()
1215
{
1316
global $bucketName;
1417
$this->bucketName = $bucketName;
1518

1619
global $testAuth;
1720
$this->auth = $testAuth;
21+
$this->config = new Config();
1822
}
23+
1924
public function testData()
2025
{
2126
$token = $this->auth->uploadToken($this->bucketName);
22-
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', null, 'text/plain', true);
27+
list($ret, $error) = FormUploader::put($token, 'formput', 'hello world', $this->config, null, 'text/plain', true);
2328
$this->assertNull($error);
2429
$this->assertNotNull($ret['hash']);
2530
}
@@ -37,7 +42,7 @@ public function testFile()
3742
{
3843
$key = 'formPutFile';
3944
$token = $this->auth->uploadToken($this->bucketName, $key);
40-
list($ret, $error) = FormUploader::putFile($token, $key, __file__, null, 'text/plain', true);
45+
list($ret, $error) = FormUploader::putFile($token, $key, __file__, $this->config, null, 'text/plain', true);
4146
$this->assertNull($error);
4247
$this->assertNotNull($ret['hash']);
4348
}

0 commit comments

Comments
 (0)