Skip to content

Commit 3c0ebee

Browse files
authored
Merge pull request #405 from qiniu/features/compatible-php-8.x
make compatible with php 8.x and fix compatible with php 5.3
2 parents af52094 + 605f852 commit 3c0ebee

24 files changed

+219
-87
lines changed

Diff for: .github/workflows/test-ci.yml

+17-4
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ jobs:
1010
fail-fast: false
1111
max-parallel: 1
1212
matrix:
13-
php-versions: ['5.4', '5.5', '5.6', '7.0']
13+
php-versions: ['5.3', '5.4', '5.5', '5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2']
1414
runs-on: ubuntu-latest
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v2
1818

19+
- name: Setup php for mock server
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: '8.2'
23+
24+
- name: Setup build-in server
25+
run: |
26+
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
27+
echo $! > mock-server.pid
28+
1929
- name: Setup php
2030
uses: shivammathur/setup-php@v2
2131
with:
@@ -28,19 +38,22 @@ jobs:
2838
2939
- name: Run cases
3040
run: |
31-
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
32-
export PHP_SERVER_PID=$!
3341
./vendor/bin/phpcs --standard=PSR2 src
3442
./vendor/bin/phpcs --standard=PSR2 examples
3543
./vendor/bin/phpcs --standard=PSR2 tests
3644
./vendor/bin/phpunit --coverage-clover=coverage.xml
37-
kill $PHP_SERVER_PID
45+
cat mock-server.pid | xargs kill
3846
3947
env:
4048
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
4149
QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
4250
QINIU_TEST_BUCKET: ${{ secrets.QINIU_TEST_BUCKET }}
4351
QINIU_TEST_DOMAIN: ${{ secrets.QINIU_TEST_DOMAIN }}
4452

53+
- name: Print mock servion log
54+
if: ${{ failure() }}
55+
run: |
56+
cat phpd.log
57+
4558
- name: After_success
4659
run: bash <(curl -s https://codecov.io/bash)

Diff for: README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212

1313
## 安装
1414

15-
* 推荐使用 `composer` 进行安装。可以使用 composer.json 声明依赖,或者运行下面的命令。SDK 包已经放到这里 [`qiniu/php-sdk`][install-packagist]
15+
推荐使用 `composer` 进行安装。可以使用 composer.json 声明依赖,或者运行下面的命令。SDK 包已经放到这里 [`qiniu/php-sdk`][install-packagist]
16+
1617
```bash
1718
$ composer require qiniu/php-sdk
1819
```
19-
* 直接下载安装,SDK 没有依赖其他第三方库,但需要参照 composer 的 autoloader,增加一个自己的 autoloader 程序。
2020

2121
## 运行环境
2222

23-
| Qiniu SDK版本 | PHP 版本 |
24-
|:--------------------:|:---------------------------:|
25-
| 7.x | cURL extension, 5.3 - 5.6,7.0 |
26-
| 6.x | cURL extension, 5.2 - 5.6 |
23+
| Qiniu SDK版本 | PHP 版本 |
24+
|:--------------------:|:-----------------------------------------------:|
25+
| 7.x | cURL extension, 5.3 - 5.6, 7.0 - 7.4, 8.0-8.1 |
26+
| 6.x | cURL extension, 5.2 - 5.6 |
2727

2828
## 使用方法
2929

Diff for: composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919
],
2020
"require": {
2121
"php": ">=5.3.3",
22-
"myclabs/php-enum": "1.6.6"
22+
"myclabs/php-enum": "~1.5.2 || ~1.6.6 || ~1.7.7 || ~1.8.4"
2323
},
2424
"require-dev": {
2525
"paragonie/random_compat": ">=2",
26-
"phpunit/phpunit": "~4.0",
27-
"squizlabs/php_codesniffer": "~3.6"
26+
"phpunit/phpunit": "^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4",
27+
"squizlabs/php_codesniffer": "^2.3 || ~3.6"
2828
},
2929
"autoload": {
3030
"psr-4": {

Diff for: src/Qiniu/Enum/QiniuEnum.php

+37-25
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
<?php
2+
// @codingStandardsIgnoreStart
3+
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
24

35
namespace Qiniu\Enum;
46

57
use MyCLabs\Enum\Enum;
68

7-
/**
8-
* 扩展 MyCLabs\Enum\Enum 以使用其新版本的 from 方法
9-
*
10-
* @link https://github.com/myclabs/php-enum
11-
*/
12-
abstract class QiniuEnum extends Enum
13-
{
14-
/**
15-
* @param mixed $value
16-
* @return static
17-
*/
18-
public static function from($value)
9+
if (method_exists("MyCLabs\\Enum\\Enum", "from")) {
10+
abstract class QiniuEnum extends Enum
1911
{
20-
$key = self::assertValidValueReturningKey($value);
21-
22-
return self::__callStatic($key, array());
12+
// @codingStandardsIgnoreEnd
13+
// @codingStandardsIgnoreStart
2314
}
24-
15+
} else {
2516
/**
26-
* Asserts valid enum value
17+
* poly fill MyCLabs\Enum\Enum::from in low version
2718
*
28-
* @psalm-pure
29-
* @psalm-assert T $value
30-
* @param mixed $value
31-
* @return string
19+
* @link https://github.com/myclabs/php-enum
3220
*/
33-
private static function assertValidValueReturningKey($value)
21+
abstract class QiniuEnum extends Enum
3422
{
35-
if (false === ($key = self::search($value))) {
36-
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
23+
// @codingStandardsIgnoreEnd
24+
/**
25+
* @param mixed $value
26+
* @return static
27+
*/
28+
public static function from($value)
29+
{
30+
$key = self::assertValidValueReturningKey($value);
31+
32+
return self::__callStatic($key, array());
3733
}
3834

39-
return $key;
35+
/**
36+
* Asserts valid enum value
37+
*
38+
* @psalm-pure
39+
* @psalm-assert T $value
40+
* @param mixed $value
41+
* @return string
42+
*/
43+
private static function assertValidValueReturningKey($value)
44+
{
45+
if (false === ($key = self::search($value))) {
46+
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
47+
}
48+
49+
return $key;
50+
}
51+
// @codingStandardsIgnoreStart
4052
}
4153
}

Diff for: src/Qiniu/Http/Header.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function normalizeKey($key)
8383
return $key;
8484
}
8585

86-
return ucwords(strtolower($key), '-');
86+
return \Qiniu\ucwords(strtolower($key), '-');
8787
}
8888

8989
/**

Diff for: src/Qiniu/Storage/BucketManager.php

+43-14
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,11 @@ public function changeMime($bucket, $key, $mime)
705705
*
706706
* @param string $bucket 待操作资源所在空间
707707
* @param string $key 待操作资源文件名
708-
* @param int $fileType 0 表示标准存储;1 表示低频存储;2 表示归档存储;3 表示深度归档存储
708+
* @param int $fileType 对象存储类型
709+
* 0 表示标准存储;
710+
* 1 表示低频存储;
711+
* 2 表示归档存储;
712+
* 3 表示深度归档存储;
709713
*
710714
* @return array
711715
* @link https://developer.qiniu.com/kodo/api/3710/chtype
@@ -931,10 +935,18 @@ public function deleteAfterDays($bucket, $key, $days)
931935
*
932936
* @param string $bucket 空间名
933937
* @param string $key 目标资源
934-
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
935-
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
936-
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
937-
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
938+
* @param int $to_line_after_days 多少天后将文件转为低频存储。
939+
* -1 表示取消已设置的转低频存储的生命周期规则;
940+
* 0 表示不修改转低频生命周期规则。
941+
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
942+
* -1 表示取消已设置的转归档存储的生命周期规则;
943+
* 0 表示不修改转归档生命周期规则。
944+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
945+
* -1 表示取消已设置的转深度归档存储的生命周期规则;
946+
* 0 表示不修改转深度归档生命周期规则。
947+
* @param int $delete_after_days 多少天后将文件删除。
948+
* -1 表示取消已设置的删除存储的生命周期规则;
949+
* 0 表示不修改删除存储的生命周期规则。
938950
* @return array
939951
*/
940952
public function setObjectLifecycle(
@@ -961,11 +973,20 @@ public function setObjectLifecycle(
961973
*
962974
* @param string $bucket 空间名
963975
* @param string $key 目标资源
964-
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
965-
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
966-
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
967-
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
968-
* @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime
976+
* @param int $to_line_after_days 多少天后将文件转为低频存储。
977+
* 设置为 -1 表示取消已设置的转低频存储的生命周期规则;
978+
* 0 表示不修改转低频生命周期规则。
979+
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
980+
* -1 表示取消已设置的转归档存储的生命周期规则;
981+
* 0 表示不修改转归档生命周期规则。
982+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
983+
* -1 表示取消已设置的转深度归档存储的生命周期规则;
984+
* 0 表示不修改转深度归档生命周期规则。
985+
* @param int $delete_after_days 多少天后将文件删除。
986+
* -1 表示取消已设置的删除存储的生命周期规则;
987+
* 0 表示不修改删除存储的生命周期规则。
988+
* @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功。
989+
* 目前支持:hash、mime、fsize、putTime
969990
* @return array
970991
*/
971992
public function setObjectLifecycleWithCond(
@@ -1124,10 +1145,18 @@ public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
11241145
/**
11251146
* @param string $bucket 空间名
11261147
* @param array<string> $keys 目标资源
1127-
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
1128-
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
1129-
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
1130-
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
1148+
* @param int $to_line_after_days 多少天后将文件转为低频存储。
1149+
* -1 表示取消已设置的转低频存储的生命周期规则;
1150+
* 0 表示不修改转低频生命周期规则。
1151+
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
1152+
* -1 表示取消已设置的转归档存储的生命周期规则;
1153+
* 0 表示不修改转归档生命周期规则。
1154+
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
1155+
* -1 表示取消已设置的转深度归档存储的生命周期规则;
1156+
* 0 表示不修改转深度归档生命周期规则。
1157+
* @param int $delete_after_days 多少天后将文件删除。
1158+
* -1 表示取消已设置的删除存储的生命周期规则;
1159+
* 0 表示不修改删除存储的生命周期规则。
11311160
*
11321161
* @retrun array<string>
11331162
*/

Diff for: src/Qiniu/functions.php

+24
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,28 @@ function explodeUpToken($upToken)
278278
$bucket = $scopeItems[0];
279279
return array($accessKey, $bucket, null);
280280
}
281+
282+
// polyfill ucwords for `php version < 5.4.32` or `5.5.0 <= php version < 5.5.16`
283+
if (version_compare(phpversion(), "5.4.32") < 0 ||
284+
(
285+
version_compare(phpversion(), "5.5.0") >= 0 &&
286+
version_compare(phpversion(), "5.5.16") < 0
287+
)
288+
) {
289+
function ucwords($str, $delimiters = " \t\r\n\f\v")
290+
{
291+
$delims = preg_split('//u', $delimiters, -1, PREG_SPLIT_NO_EMPTY);
292+
293+
foreach ($delims as $delim) {
294+
$str = implode($delim, array_map('ucfirst', explode($delim, $str)));
295+
}
296+
297+
return $str;
298+
}
299+
} else {
300+
function ucwords($str, $delimiters)
301+
{
302+
return \ucwords($str, $delimiters);
303+
}
304+
}
281305
}

Diff for: tests/Qiniu/Tests/AuthTest.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ function time()
1111
}
1212

1313
namespace Qiniu\Tests {
14+
use PHPUnit\Framework\TestCase;
15+
1416
use Qiniu\Auth;
1517
use Qiniu\Http\Header;
1618

1719
// @codingStandardsIgnoreEnd
1820

19-
class AuthTest extends \PHPUnit_Framework_TestCase
21+
class AuthTest extends TestCase
2022
{
2123

2224
public function testSign()
@@ -65,10 +67,6 @@ public function testUploadToken()
6567
unset($_SERVER['override_qiniu_auth_time']);
6668
}
6769

68-
public function testVerifyCallback()
69-
{
70-
}
71-
7270
public function testSignQiniuAuthorization()
7371
{
7472
$auth = new Auth("ak", "sk");

Diff for: tests/Qiniu/Tests/Base64Test.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22
namespace Qiniu\Tests;
33

4+
use PHPUnit\Framework\TestCase;
5+
46
use Qiniu;
57

6-
class Base64Test extends \PHPUnit_Framework_TestCase
8+
class Base64Test extends TestCase
79
{
810
public function testUrlSafe()
911
{

Diff for: tests/Qiniu/Tests/BucketTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Qiniu\Tests;
44

5+
use PHPUnit\Framework\TestCase;
6+
57
use Qiniu\Config;
68
use Qiniu\Storage\BucketManager;
79

8-
class BucketTest extends \PHPUnit_Framework_TestCase
10+
class BucketTest extends TestCase
911
{
1012
protected $bucketManager;
1113
protected $dummyBucketManager;
@@ -14,7 +16,10 @@ class BucketTest extends \PHPUnit_Framework_TestCase
1416
protected $key2;
1517
protected $customCallbackURL;
1618

17-
protected function setUp()
19+
/**
20+
* @before
21+
*/
22+
protected function setUpBucketManager()
1823
{
1924
global $bucketName;
2025
global $key;

Diff for: tests/Qiniu/Tests/CdnManagerTest.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88

99
namespace Qiniu\Tests;
1010

11+
use PHPUnit\Framework\TestCase;
12+
1113
use Qiniu\Cdn\CdnManager;
1214
use Qiniu\Http\Client;
1315

14-
class CdnManagerTest extends \PHPUnit_Framework_TestCase
16+
class CdnManagerTest extends TestCase
1517
{
1618
protected $cdnManager;
1719
protected $encryptKey;
@@ -24,7 +26,10 @@ class CdnManagerTest extends \PHPUnit_Framework_TestCase
2426
protected $customDomain;
2527
protected $customDomain2;
2628

27-
protected function setUp()
29+
/**
30+
* @before
31+
*/
32+
protected function setUpCdnManager()
2833
{
2934
global $testAuth;
3035
$this->cdnManager = new CdnManager($testAuth);

0 commit comments

Comments
 (0)