Skip to content

Commit f53fdb4

Browse files
committed
fix some error for create client
1 parent 53e843e commit f53fdb4

File tree

12 files changed

+115
-109
lines changed

12 files changed

+115
-109
lines changed

.github/dependabot.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "github-actions"
4+
directory: "/"
5+
schedule:
6+
# Check for updates to GitHub Actions every weekday
7+
interval: "daily"

.github/workflows/php.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ jobs:
1515
strategy:
1616
fail-fast: true
1717
matrix:
18-
php: [7.2, 7.3, 7.4] # 7.1
18+
php: [7.3, 7.4, 8.0]
1919
os: [ubuntu-latest, macOS-latest] # windows-latest,
20+
include:
21+
- os: 'ubuntu-latest'
22+
php: '7.2'
23+
phpunit: '8.5.13'
2024

2125
steps:
2226
- name: Checkout
@@ -28,18 +32,17 @@ jobs:
2832
uses: shivammathur/setup-php@v2
2933
with:
3034
php-version: ${{ matrix.php}}
31-
tools: pecl, php-cs-fixer, phpunit:8.5.8
32-
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis, swoole-4.5.11 # , swoole-4.4.19 #optional, setup extensions
35+
tools: pecl, php-cs-fixer, phpunit:${{ matrix.phpunit }}
36+
extensions: mbstring, dom, fileinfo, mysql, openssl, igbinary, redis, swoole-4.6.7 # , swoole-4.4.19 #optional, setup extensions
3337
ini-values: post_max_size=56M, short_open_tag=On #optional, setup php.ini configuration
3438
coverage: none #optional, setup coverage driver: xdebug, none
3539

3640
- name: Install dependencies
3741
run: |
3842
composer install --no-progress
39-
# composer require phpunit/phpunit ^8.0 --no-progress # default
4043
4144
# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
4245
# Docs: https://getcomposer.org/doc/articles/scripts.md
4346

4447
- name: Run test suite
45-
run: phpunit -v # vendor/bin/phpunit -v
48+
run: phpunit -v --debug

src/AbstractClient.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Psr\Http\Message\ResponseInterface;
1616
use RuntimeException;
1717
use stdClass;
18+
use Toolkit\Stdlib\Str\UrlHelper;
1819
use function array_merge;
1920
use function base64_encode;
2021
use function fclose;
@@ -596,17 +597,17 @@ protected function buildFullUrl(string $url, $data = null): string
596597
$url = trim($url);
597598

598599
// is a url part.
599-
if ($this->baseUrl && !ClientUtil::isFullURL($url)) {
600+
if ($this->baseUrl && !UrlHelper::isFullURL($url)) {
600601
$url = $this->baseUrl . '/' . ltrim($url, '/');
601602
}
602603

603604
// check again
604-
if (!ClientUtil::isFullURL($url)) {
605+
if (!UrlHelper::isFullURL($url)) {
605606
throw new RuntimeException("The request url is not full, URL $url");
606607
}
607608

608609
if ($data) {
609-
return ClientUtil::buildURL($url, $data);
610+
return UrlHelper::build($url, $data);
610611
}
611612

612613
return $url;
@@ -772,10 +773,25 @@ public function getOptions(): array
772773

773774
/**
774775
* @param array $options
776+
*
777+
* @return ClientInterface
775778
*/
776-
public function setOptions(array $options): void
779+
public function setOptions(array $options): ClientInterface
777780
{
778781
$this->options = array_merge($this->options, $options);
782+
return $this;
783+
}
784+
785+
/**
786+
* @param string $key
787+
* @param mixed $value
788+
*
789+
* @return ClientInterface
790+
*/
791+
public function setOption(string $key, $value): ClientInterface
792+
{
793+
$this->options[$key] = $value;
794+
return $this;
779795
}
780796

781797
/**

src/Client.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ class Client
3737
* @var ClientInterface[]
3838
*/
3939
private static $drivers = [
40-
'co' => CoClient::class,
4140
'curl' => CurlClient::class,
4241
'stream' => StreamClient::class,
4342
'fsock' => FSockClient::class,
4443
'fopen' => FOpenClient::class,
4544
'file' => FileClient::class,
45+
'co' => CoClient::class,
4646
'co2' => CoClient2::class,
4747
];
4848

@@ -76,6 +76,7 @@ public static function factory(array $config): ClientInterface
7676
foreach (self::$drivers as $driverClass) {
7777
if ($driverClass::isAvailable()) {
7878
$class = $driverClass;
79+
break;
7980
}
8081
}
8182
} else {
@@ -122,13 +123,9 @@ public static function setDefaultDriver(ClientInterface $defaultDriver): void
122123
*/
123124
public static function __callStatic(string $method, array $args)
124125
{
126+
// if no default driver, create it.
125127
if (!$client = self::$defaultDriver) {
126-
// has config, create driver instance from config.
127-
if (!$config = self::$defaultConfig) {
128-
throw new RuntimeException('must be setting default client driver before call');
129-
}
130-
131-
$client = self::factory($config);
128+
$client = self::$defaultDriver = self::factory(self::$defaultConfig);
132129
}
133130

134131
if (method_exists($client, $method)) {

src/ClientInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,21 @@ public function setProxy(string $host, int $port): ClientInterface;
238238
*/
239239
public function setUserAgent(string $userAgent): ClientInterface;
240240

241+
/**
242+
* @param array $options
243+
*
244+
* @return ClientInterface
245+
*/
246+
public function setOptions(array $options): ClientInterface;
247+
248+
/**
249+
* @param string $key
250+
* @param $value
251+
*
252+
* @return ClientInterface
253+
*/
254+
public function setOption(string $key, $value): ClientInterface;
255+
241256
/**************************************************************************
242257
* request cookies
243258
*************************************************************************/

src/ClientUtil.php

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use InvalidArgumentException;
1313
use PhpComp\Http\Client\Exception\ClientException;
1414
use Toolkit\Stdlib\Helper\JsonHelper;
15+
use Toolkit\Stdlib\Str\UrlHelper;
1516
use function array_merge;
1617
use function http_build_query;
1718
use function is_scalar;
@@ -78,13 +79,14 @@ public static function ucwordArrayKeys(array $arr): array
7879
*/
7980
public static function isFullURL(string $url): bool
8081
{
81-
return 0 === strpos($url, 'http:') || 0 === strpos($url, 'https:') || 0 === strpos($url, '//');
82+
return UrlHelper::isFullUrl($url);
8283
}
8384

8485
/**
8586
* @param string $url
8687
*
8788
* @return array
89+
* @deprecated please use UrlHelper::parse2($url);
8890
*/
8991
public static function parseUrl(string $url): array
9092
{
@@ -93,15 +95,13 @@ public static function parseUrl(string $url): array
9395
throw new ClientException('invalid request url: ' . $url);
9496
}
9597

96-
$info = array_merge([
98+
return array_merge([
9799
'scheme' => 'http',
98100
'host' => '',
99101
'port' => 80,
100102
'path' => '/',
101103
'query' => '',
102104
], $info);
103-
104-
return $info;
105105
}
106106

107107
/**
@@ -119,75 +119,19 @@ public static function buildURL(string $url, $data = null): string
119119
return $url;
120120
}
121121

122-
// Build arrays of values we need to decode before parsing
123-
protected static $entities = [
124-
'%21',
125-
'%2A',
126-
'%27',
127-
'%28',
128-
'%29',
129-
'%3B',
130-
'%3A',
131-
'%40',
132-
'%26',
133-
'%3D',
134-
'%24',
135-
'%2C',
136-
'%2F',
137-
'%3F',
138-
'%23',
139-
'%5B',
140-
'%5D'
141-
];
142-
143-
protected static $replacements = [
144-
'!',
145-
'*',
146-
"'",
147-
'(',
148-
')',
149-
';',
150-
':',
151-
'@',
152-
'&',
153-
'=',
154-
'$',
155-
',',
156-
'/',
157-
'?',
158-
'#',
159-
'[',
160-
']'
161-
];
162-
163122
/**
164-
* [urlEncode 会先转换编码]
165-
* $url="ftp://ud03:password@www.xxx.net/中文/中文.rar";
166-
* $url1 = url_encode($url);
167-
* //ftp://ud03:password@www.xxx.net/%C3%A4%C2%B8%C2%AD%C3%A6%C2%96%C2%87/%C3%A4%C2%B8%C2%AD%C3%A6%C2%96%C2%87.rar
168-
* $url2 = urldecode($url);
169-
* echo $url1.PHP_EOL.$url2;
170-
*
171-
* @param string $url [description]
123+
* @param string $url
172124
*
173-
* @return mixed|string [type] [description]
125+
* @return string
126+
* @deprecated please use UrlHelper::encode2($url);
174127
*/
175-
public static function encodeURL(string $url)
128+
public static function encodeURL(string $url): string
176129
{
177-
if (!$url = trim($url)) {
178-
return '';
179-
}
180-
181-
// 若已被编码的url,将被解码,再继续重新编码
182-
$url = urldecode($url);
183-
184-
$encodeUrl = rawurlencode(mb_convert_encoding($url, 'utf-8'));
185-
// $url = rawurlencode($url);
186-
return str_replace(self::$entities, self::$replacements, $encodeUrl);
130+
return UrlHelper::encode2($url);
187131
}
188132

189133
/**
190-
* @param array $headers
134+
* @param array $headers
191135
* @param string|array|object $data body data
192136
*
193137
* @return string

src/Curl/CurlClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpComp\Http\Client\ClientUtil;
1616
use PhpComp\Http\Client\Exception\ClientException;
1717
use PhpComp\Http\Client\Traits\ParseRawResponseTrait;
18+
use Toolkit\Stdlib\Str\UrlHelper;
1819
use function array_merge;
1920
use function curl_close;
2021
use function curl_errno;
@@ -418,7 +419,7 @@ protected function prepareRequest(string $url, $data, array $headers, array $opt
418419
}
419420

420421
// set request url
421-
$curlOptions[CURLOPT_URL] = ClientUtil::encodeURL($url);
422+
$curlOptions[CURLOPT_URL] = UrlHelper::encode2($url);
422423

423424
// append http headers
424425
if ($headers = array_merge($this->headers, $options['headers'], $headers)) {

src/Curl/CurlMulti.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use PhpComp\Http\Client\ClientUtil;
1313
use RuntimeException;
14+
use Toolkit\Stdlib\Str\UrlHelper;
1415
use function array_merge;
1516
use function curl_errno;
1617
use function curl_error;
@@ -324,12 +325,12 @@ protected function buildUrl(string $url, $data = null): string
324325
$url = trim($url);
325326

326327
// is a url part.
327-
if ($this->baseUrl && !ClientUtil::isFullURL($url)) {
328+
if ($this->baseUrl && !UrlHelper::isFullURL($url)) {
328329
$url = $this->baseUrl . $url;
329330
}
330331

331332
// check again
332-
if (!ClientUtil::isFullURL($url)) {
333+
if (!UrlHelper::isFullURL($url)) {
333334
throw new RuntimeException("The request url is not full, URL $url");
334335
}
335336

src/FOpenClient.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpComp\Http\Client\Traits\ParseRawResponseTrait;
1414
use PhpComp\Http\Client\Traits\StreamContextBuildTrait;
1515
use Throwable;
16+
use Toolkit\Stdlib\Str\UrlHelper;
1617
use function array_merge;
1718
use function fclose;
1819
use function feof;
@@ -99,7 +100,7 @@ public function request(
99100
try {
100101
$ctx = $this->buildStreamContext($url, $headers, $options, $data);
101102

102-
$fullUrl = ClientUtil::encodeURL($this->fullUrl);
103+
$fullUrl = UrlHelper::encode2($this->fullUrl);
103104
// send request
104105
$this->handle = fopen($fullUrl, 'rb', false, $ctx);
105106
} catch (Throwable $e) {

src/FileClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpComp\Http\Client\Traits\ParseRawResponseTrait;
1414
use PhpComp\Http\Client\Traits\StreamContextBuildTrait;
1515
use Throwable;
16+
use Toolkit\Stdlib\Str\UrlHelper;
1617
use function array_merge;
1718
use function file_get_contents;
1819
use function function_exists;
@@ -56,11 +57,11 @@ public function request(
5657
$options = array_merge($this->options, $options);
5758

5859
try {
59-
$ctx = $this->buildStreamContext($url, $headers, $options, $data);
60-
$fullUrl = ClientUtil::encodeURL($this->fullUrl);
60+
$reqCtx = $this->buildStreamContext($url, $headers, $options, $data);
61+
$fullUrl = UrlHelper::encode2($this->fullUrl);
6162

6263
// send request
63-
$this->responseBody = file_get_contents($fullUrl, false, $ctx);
64+
$this->responseBody = file_get_contents($fullUrl, false, $reqCtx);
6465

6566
// false is failure
6667
if ($this->responseBody === false) {

0 commit comments

Comments
 (0)