Skip to content

Commit

Permalink
Merge pull request #405 from qiniu/features/compatible-php-8.x
Browse files Browse the repository at this point in the history
make compatible with php 8.x and fix compatible with php 5.3
  • Loading branch information
bachue committed May 6, 2023
2 parents af52094 + 605f852 commit 3c0ebee
Show file tree
Hide file tree
Showing 24 changed files with 219 additions and 87 deletions.
21 changes: 17 additions & 4 deletions .github/workflows/test-ci.yml
Expand Up @@ -10,12 +10,22 @@ jobs:
fail-fast: false
max-parallel: 1
matrix:
php-versions: ['5.4', '5.5', '5.6', '7.0']
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']
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup php for mock server
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Setup build-in server
run: |
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
echo $! > mock-server.pid
- name: Setup php
uses: shivammathur/setup-php@v2
with:
Expand All @@ -28,19 +38,22 @@ jobs:
- name: Run cases
run: |
nohup php -S localhost:9000 -t ./tests/mock-server/ > phpd.log 2>&1 &
export PHP_SERVER_PID=$!
./vendor/bin/phpcs --standard=PSR2 src
./vendor/bin/phpcs --standard=PSR2 examples
./vendor/bin/phpcs --standard=PSR2 tests
./vendor/bin/phpunit --coverage-clover=coverage.xml
kill $PHP_SERVER_PID
cat mock-server.pid | xargs kill
env:
QINIU_ACCESS_KEY: ${{ secrets.QINIU_ACCESS_KEY }}
QINIU_SECRET_KEY: ${{ secrets.QINIU_SECRET_KEY }}
QINIU_TEST_BUCKET: ${{ secrets.QINIU_TEST_BUCKET }}
QINIU_TEST_DOMAIN: ${{ secrets.QINIU_TEST_DOMAIN }}

- name: Print mock servion log
if: ${{ failure() }}
run: |
cat phpd.log
- name: After_success
run: bash <(curl -s https://codecov.io/bash)
12 changes: 6 additions & 6 deletions README.md
Expand Up @@ -12,18 +12,18 @@

## 安装

* 推荐使用 `composer` 进行安装。可以使用 composer.json 声明依赖,或者运行下面的命令。SDK 包已经放到这里 [`qiniu/php-sdk`][install-packagist]
推荐使用 `composer` 进行安装。可以使用 composer.json 声明依赖,或者运行下面的命令。SDK 包已经放到这里 [`qiniu/php-sdk`][install-packagist]

```bash
$ composer require qiniu/php-sdk
```
* 直接下载安装,SDK 没有依赖其他第三方库,但需要参照 composer 的 autoloader,增加一个自己的 autoloader 程序。

## 运行环境

| Qiniu SDK版本 | PHP 版本 |
|:--------------------:|:---------------------------:|
| 7.x | cURL extension, 5.3 - 5.6,7.0 |
| 6.x | cURL extension, 5.2 - 5.6 |
| Qiniu SDK版本 | PHP 版本 |
|:--------------------:|:-----------------------------------------------:|
| 7.x | cURL extension, 5.3 - 5.6, 7.0 - 7.4, 8.0-8.1 |
| 6.x | cURL extension, 5.2 - 5.6 |

## 使用方法

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -19,12 +19,12 @@
],
"require": {
"php": ">=5.3.3",
"myclabs/php-enum": "1.6.6"
"myclabs/php-enum": "~1.5.2 || ~1.6.6 || ~1.7.7 || ~1.8.4"
},
"require-dev": {
"paragonie/random_compat": ">=2",
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~3.6"
"phpunit/phpunit": "^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4",
"squizlabs/php_codesniffer": "^2.3 || ~3.6"
},
"autoload": {
"psr-4": {
Expand Down
62 changes: 37 additions & 25 deletions src/Qiniu/Enum/QiniuEnum.php
@@ -1,41 +1,53 @@
<?php
// @codingStandardsIgnoreStart
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses

namespace Qiniu\Enum;

use MyCLabs\Enum\Enum;

/**
* 扩展 MyCLabs\Enum\Enum 以使用其新版本的 from 方法
*
* @link https://github.com/myclabs/php-enum
*/
abstract class QiniuEnum extends Enum
{
/**
* @param mixed $value
* @return static
*/
public static function from($value)
if (method_exists("MyCLabs\\Enum\\Enum", "from")) {
abstract class QiniuEnum extends Enum
{
$key = self::assertValidValueReturningKey($value);

return self::__callStatic($key, array());
// @codingStandardsIgnoreEnd
// @codingStandardsIgnoreStart
}

} else {
/**
* Asserts valid enum value
* poly fill MyCLabs\Enum\Enum::from in low version
*
* @psalm-pure
* @psalm-assert T $value
* @param mixed $value
* @return string
* @link https://github.com/myclabs/php-enum
*/
private static function assertValidValueReturningKey($value)
abstract class QiniuEnum extends Enum
{
if (false === ($key = self::search($value))) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
// @codingStandardsIgnoreEnd
/**
* @param mixed $value
* @return static
*/
public static function from($value)
{
$key = self::assertValidValueReturningKey($value);

return self::__callStatic($key, array());
}

return $key;
/**
* Asserts valid enum value
*
* @psalm-pure
* @psalm-assert T $value
* @param mixed $value
* @return string
*/
private static function assertValidValueReturningKey($value)
{
if (false === ($key = self::search($value))) {
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
}

return $key;
}
// @codingStandardsIgnoreStart
}
}
2 changes: 1 addition & 1 deletion src/Qiniu/Http/Header.php
Expand Up @@ -83,7 +83,7 @@ public static function normalizeKey($key)
return $key;
}

return ucwords(strtolower($key), '-');
return \Qiniu\ucwords(strtolower($key), '-');
}

/**
Expand Down
57 changes: 43 additions & 14 deletions src/Qiniu/Storage/BucketManager.php
Expand Up @@ -705,7 +705,11 @@ public function changeMime($bucket, $key, $mime)
*
* @param string $bucket 待操作资源所在空间
* @param string $key 待操作资源文件名
* @param int $fileType 0 表示标准存储;1 表示低频存储;2 表示归档存储;3 表示深度归档存储
* @param int $fileType 对象存储类型
* 0 表示标准存储;
* 1 表示低频存储;
* 2 表示归档存储;
* 3 表示深度归档存储;
*
* @return array
* @link https://developer.qiniu.com/kodo/api/3710/chtype
Expand Down Expand Up @@ -931,10 +935,18 @@ public function deleteAfterDays($bucket, $key, $days)
*
* @param string $bucket 空间名
* @param string $key 目标资源
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
* @param int $to_line_after_days 多少天后将文件转为低频存储。
* -1 表示取消已设置的转低频存储的生命周期规则;
* 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
* -1 表示取消已设置的转归档存储的生命周期规则;
* 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
* -1 表示取消已设置的转深度归档存储的生命周期规则;
* 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除。
* -1 表示取消已设置的删除存储的生命周期规则;
* 0 表示不修改删除存储的生命周期规则。
* @return array
*/
public function setObjectLifecycle(
Expand All @@ -961,11 +973,20 @@ public function setObjectLifecycle(
*
* @param string $bucket 空间名
* @param string $key 目标资源
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
* @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功,目前支持:hash、mime、fsize、putTime
* @param int $to_line_after_days 多少天后将文件转为低频存储。
* 设置为 -1 表示取消已设置的转低频存储的生命周期规则;
* 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
* -1 表示取消已设置的转归档存储的生命周期规则;
* 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
* -1 表示取消已设置的转深度归档存储的生命周期规则;
* 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除。
* -1 表示取消已设置的删除存储的生命周期规则;
* 0 表示不修改删除存储的生命周期规则。
* @param array<string, mixed> $cond 匹配条件,只有条件匹配才会设置成功。
* 目前支持:hash、mime、fsize、putTime
* @return array
*/
public function setObjectLifecycleWithCond(
Expand Down Expand Up @@ -1124,10 +1145,18 @@ public static function buildBatchDeleteAfterDays($bucket, $key_day_pairs)
/**
* @param string $bucket 空间名
* @param array<string> $keys 目标资源
* @param int $to_line_after_days 多少天后将文件转为低频存储,设置为 -1 表示取消已设置的转低频存储的生命周期规则, 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储,设置为 -1 表示取消已设置的转归档存储的生命周期规则, 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储,设置为 -1 表示取消已设置的转深度归档存储的生命周期规则, 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除,设置为 -1 表示取消已设置的删除存储的生命周期规则, 0 表示不修改删除存储的生命周期规则。
* @param int $to_line_after_days 多少天后将文件转为低频存储。
* -1 表示取消已设置的转低频存储的生命周期规则;
* 0 表示不修改转低频生命周期规则。
* @param int $to_archive_after_days 多少天后将文件转为归档存储。
* -1 表示取消已设置的转归档存储的生命周期规则;
* 0 表示不修改转归档生命周期规则。
* @param int $to_deep_archive_after_days 多少天后将文件转为深度归档存储。
* -1 表示取消已设置的转深度归档存储的生命周期规则;
* 0 表示不修改转深度归档生命周期规则。
* @param int $delete_after_days 多少天后将文件删除。
* -1 表示取消已设置的删除存储的生命周期规则;
* 0 表示不修改删除存储的生命周期规则。
*
* @retrun array<string>
*/
Expand Down
24 changes: 24 additions & 0 deletions src/Qiniu/functions.php
Expand Up @@ -278,4 +278,28 @@ function explodeUpToken($upToken)
$bucket = $scopeItems[0];
return array($accessKey, $bucket, null);
}

// polyfill ucwords for `php version < 5.4.32` or `5.5.0 <= php version < 5.5.16`
if (version_compare(phpversion(), "5.4.32") < 0 ||
(
version_compare(phpversion(), "5.5.0") >= 0 &&
version_compare(phpversion(), "5.5.16") < 0
)
) {
function ucwords($str, $delimiters = " \t\r\n\f\v")
{
$delims = preg_split('//u', $delimiters, -1, PREG_SPLIT_NO_EMPTY);

foreach ($delims as $delim) {
$str = implode($delim, array_map('ucfirst', explode($delim, $str)));
}

return $str;
}
} else {
function ucwords($str, $delimiters)
{
return \ucwords($str, $delimiters);
}
}
}
8 changes: 3 additions & 5 deletions tests/Qiniu/Tests/AuthTest.php
Expand Up @@ -11,12 +11,14 @@ function time()
}

namespace Qiniu\Tests {
use PHPUnit\Framework\TestCase;

use Qiniu\Auth;
use Qiniu\Http\Header;

// @codingStandardsIgnoreEnd

class AuthTest extends \PHPUnit_Framework_TestCase
class AuthTest extends TestCase
{

public function testSign()
Expand Down Expand Up @@ -65,10 +67,6 @@ public function testUploadToken()
unset($_SERVER['override_qiniu_auth_time']);
}

public function testVerifyCallback()
{
}

public function testSignQiniuAuthorization()
{
$auth = new Auth("ak", "sk");
Expand Down
4 changes: 3 additions & 1 deletion tests/Qiniu/Tests/Base64Test.php
@@ -1,9 +1,11 @@
<?php
namespace Qiniu\Tests;

use PHPUnit\Framework\TestCase;

use Qiniu;

class Base64Test extends \PHPUnit_Framework_TestCase
class Base64Test extends TestCase
{
public function testUrlSafe()
{
Expand Down
9 changes: 7 additions & 2 deletions tests/Qiniu/Tests/BucketTest.php
Expand Up @@ -2,10 +2,12 @@

namespace Qiniu\Tests;

use PHPUnit\Framework\TestCase;

use Qiniu\Config;
use Qiniu\Storage\BucketManager;

class BucketTest extends \PHPUnit_Framework_TestCase
class BucketTest extends TestCase
{
protected $bucketManager;
protected $dummyBucketManager;
Expand All @@ -14,7 +16,10 @@ class BucketTest extends \PHPUnit_Framework_TestCase
protected $key2;
protected $customCallbackURL;

protected function setUp()
/**
* @before
*/
protected function setUpBucketManager()
{
global $bucketName;
global $key;
Expand Down
9 changes: 7 additions & 2 deletions tests/Qiniu/Tests/CdnManagerTest.php
Expand Up @@ -8,10 +8,12 @@

namespace Qiniu\Tests;

use PHPUnit\Framework\TestCase;

use Qiniu\Cdn\CdnManager;
use Qiniu\Http\Client;

class CdnManagerTest extends \PHPUnit_Framework_TestCase
class CdnManagerTest extends TestCase
{
protected $cdnManager;
protected $encryptKey;
Expand All @@ -24,7 +26,10 @@ class CdnManagerTest extends \PHPUnit_Framework_TestCase
protected $customDomain;
protected $customDomain2;

protected function setUp()
/**
* @before
*/
protected function setUpCdnManager()
{
global $testAuth;
$this->cdnManager = new CdnManager($testAuth);
Expand Down

0 comments on commit 3c0ebee

Please sign in to comment.