Skip to content

Commit 75801bd

Browse files
committed
up: update some for client config and tests
1 parent 1d6e05d commit 75801bd

21 files changed

+298
-180
lines changed

example/coreq.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use PhpComp\Http\Client\Client;
4+
use PhpComp\Http\Client\ClientConst;
5+
use PhpComp\Http\Client\Swoole\CoClient;
6+
7+
require dirname(__DIR__) . '/test/bootstrap.php';
8+
9+
$client = Client::factory([
10+
'driver' => CoClient::driverName(),
11+
]);
12+
$client->setUserAgent(ClientConst::USERAGENT_CURL);
13+
14+
// debug
15+
// $client->setDebug(true);
16+
// $client->setOption('logFile', './client-req.log');
17+
18+
$client->get('https://cht.sh/php/array_shift');
19+
20+
vdump(
21+
$client->getDriverName(),
22+
$client->getResponseHeaders()
23+
);
24+
25+
echo $client->getResponseBody();

example/demo.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use PhpComp\Http\Client\Client;
4+
use PhpComp\Http\Client\ClientConst;
5+
6+
require dirname(__DIR__) . '/test/bootstrap.php';
7+
8+
$client = Client::factory([]);
9+
$client->setUserAgent(ClientConst::USERAGENT_CURL);
10+
11+
// debug
12+
// $client->setDebug(true);
13+
// $client->setOption('logFile', './client-req.log');
14+
15+
$client->get('https://cht.sh/php/array_shift');
16+
17+
vdump(
18+
$client->getDriverName(),
19+
$client->getResponseHeaders()
20+
);
21+
22+
echo $client->getResponseBody();

phpunit.xml.dist renamed to phpunit.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<phpunit backupGlobals="false"
44
backupStaticAttributes="false"
5-
bootstrap="test/boot.php"
6-
colors="false"
5+
bootstrap="test/bootstrap.php"
6+
colors="true"
77
convertErrorsToExceptions="true"
88
convertNoticesToExceptions="true"
99
convertWarningsToExceptions="true"

src/AbstractClient.php

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ abstract class AbstractClient implements ClientInterface
4343
* for create psr7 ResponseInterface instance
4444
*
4545
* @var Closure function(): ResponseInterface {..}
46+
* @psalm-var Closure(): ResponseInterface
4647
*/
4748
protected $responseCreator;
4849

@@ -60,10 +61,10 @@ abstract class AbstractClient implements ClientInterface
6061
// enable SSL verify
6162
'sslVerify' => false,
6263
// request headers
63-
'headers' => [// name => value
64-
],
65-
'cookies' => [// name => value
66-
],
64+
// name => value
65+
'headers' => [],
66+
// name => value
67+
'cookies' => [],
6768
'proxy' => [
6869
// 'host' => '',
6970
// 'port' => '',
@@ -79,14 +80,15 @@ abstract class AbstractClient implements ClientInterface
7980
// send data(todo)
8081
'data' => [],
8182
'json' => [],
83+
// extra
8284
// 一些针对不同驱动的自定义选项
8385
// 'curlOptions' => [],
8486
// 'coOptions' => [],
8587
// 'streamContextOptions' => [],
8688
];
8789

8890
/**
89-
* global options data. init from $defaultOptions
91+
* Global options data. init from $defaultOptions
9092
*
9193
* @see AbstractClient::$defaultOptions
9294
* @var array
@@ -110,11 +112,13 @@ abstract class AbstractClient implements ClientInterface
110112
* [ 'Content-Type' => 'Content-Type: application/json' ]
111113
*
112114
* @var array
115+
* @psalm-var array<string, string>
113116
*/
114117
protected $headers = [];
115118

116119
/**
117120
* @var array
121+
* @psalm-var array<string, string>
118122
*/
119123
protected $cookies = [];
120124

@@ -147,6 +151,17 @@ abstract class AbstractClient implements ClientInterface
147151
*/
148152
protected $responseHeaders = [];
149153

154+
/**
155+
* @param array $options
156+
*
157+
* @return static
158+
* @throws RuntimeException
159+
*/
160+
public static function new(array $options = []): ClientInterface
161+
{
162+
return new static($options);
163+
}
164+
150165
/**
151166
* @param array $options
152167
*
@@ -158,6 +173,17 @@ public static function create(array $options = []): ClientInterface
158173
return new static($options);
159174
}
160175

176+
/**
177+
* @return string
178+
*/
179+
public static function driverName(): string
180+
{
181+
$class = static::class;
182+
$name = substr($class, strrpos($class, '\\') + 1);
183+
184+
return strtolower(str_replace('Client', '', $name));
185+
}
186+
161187
/**
162188
* SimpleCurl constructor.
163189
*
@@ -257,9 +283,9 @@ public function head(string $url, $params = null, array $headers = [], array $op
257283

258284
/**
259285
* @param string $url
260-
* @param null $params
261-
* @param array $headers
262-
* @param array $options
286+
* @param null $params
287+
* @param array $headers
288+
* @param array $options
263289
*
264290
* @return ClientInterface
265291
*/
@@ -289,9 +315,9 @@ public function sendRequest(RequestInterface $request): ResponseInterface
289315

290316
/**
291317
* @param string $url
292-
* @param mixed $data
293-
* @param array $headers
294-
* @param array $options
318+
* @param mixed $data
319+
* @param array $headers
320+
* @param array $options
295321
*
296322
* @return ClientInterface
297323
*/
@@ -368,14 +394,14 @@ public function SSLVerify(bool $enable): self
368394
/**
369395
* Set contents of HTTP Cookie header.
370396
*
371-
* @param string $key The name of the cookie
397+
* @param string $key The name of the cookie
372398
* @param string $value The value for the provided cookie name
373399
*
374400
* @return $this
375401
*/
376402
public function setCookie(string $key, $value): ClientInterface
377403
{
378-
$this->cookies[$key] = $value;
404+
$this->cookies[$key] = (string)$value;
379405
return $this;
380406
}
381407

@@ -471,7 +497,7 @@ public function setUserAgent(string $userAgent): ClientInterface
471497
*
472498
* @param string $user
473499
* @param string $pwd
474-
* @param int $authType CURLAUTH_BASIC CURLAUTH_DIGEST
500+
* @param int $authType CURLAUTH_BASIC CURLAUTH_DIGEST
475501
*
476502
* @return $this
477503
*/
@@ -491,7 +517,7 @@ public function setUserAuth(string $user, string $pwd = '', int $authType = self
491517

492518
/**
493519
* @param string $host
494-
* @param int $port
520+
* @param int $port
495521
*
496522
* @return $this
497523
*/
@@ -533,7 +559,7 @@ public function setHeaders(array $headers): ClientInterface
533559

534560
/**
535561
* @param array $headers
536-
* @param bool $override
562+
* @param bool $override
537563
*
538564
* @return $this
539565
*/
@@ -549,7 +575,7 @@ public function addHeaders(array $headers, bool $override = true): ClientInterfa
549575
/**
550576
* @param string $name
551577
* @param string $value
552-
* @param bool $override
578+
* @param bool $override
553579
*
554580
* @return $this
555581
*/
@@ -588,7 +614,7 @@ public function delHeader($names): ClientInterface
588614

589615
/**
590616
* @param string $url
591-
* @param mixed $data
617+
* @param mixed $data
592618
*
593619
* @return string
594620
*/
@@ -754,7 +780,7 @@ public function getBaseUrl(): string
754780

755781
/**
756782
* @param int|string $name
757-
* @param null $default
783+
* @param null|mixed $default
758784
*
759785
* @return mixed
760786
*/
@@ -900,7 +926,7 @@ public function getResponseHeaders(): array
900926

901927
/**
902928
* @param string $name
903-
* @param null $default
929+
* @param null $default
904930
*
905931
* @return string
906932
*/
@@ -936,6 +962,8 @@ public function isRedirect(): bool
936962

937963
/**
938964
* Was an 'error' returned (client error or server error).
965+
*
966+
* @return bool
939967
*/
940968
public function isError(): bool
941969
{
@@ -955,9 +983,6 @@ public function getStatusCode(): int
955983
*/
956984
public function getDriverName(): string
957985
{
958-
$class = static::class;
959-
$name = substr($class, strrpos($class, '\\') + 1);
960-
961-
return strtolower(str_replace('Client', '', $name));
986+
return self::driverName();
962987
}
963988
}

src/CType.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Client.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* Class Client
2121
*
2222
* @package PhpComp\Http\Client
23+
*
2324
* @method static ClientInterface get(string $url, $params = null, array $headers = [], array $options = [])
2425
* @method static ClientInterface delete(string $url, $params = null, array $headers = [], array $options = [])
2526
* @method static ClientInterface head(string $url, $params = null, array $headers = [], array $options = [])
@@ -59,28 +60,35 @@ class Client
5960
private static $defaultConfig = [];
6061

6162
/**
62-
* @param array $config
63+
* Quick create an client
64+
*
6365
* [
64-
* 'driver' => 'curl', // curl, stream, fsock, fopen, file, co, co2
65-
* // ...
66+
* 'driver' => 'curl', // curl, stream, fsock, fopen, file, co, co2
67+
* // ...
6668
* ]
6769
*
70+
* @param array $config more see {@see AbstractClient::$options}
71+
* @psalm-param array{driver:string, } $config
72+
*
6873
* @return ClientInterface|AbstractClient
6974
*/
7075
public static function factory(array $config): ClientInterface
7176
{
72-
$name = $config['driver'] ?? '';
77+
$name = $config['driver'] ?? '';
78+
$class = self::$drivers[$name] ?? '';
7379

74-
if (!$class = self::$drivers[$name] ?? '') {
80+
if (!$class) {
7581
// auto select
7682
foreach (self::$drivers as $driverClass) {
7783
if ($driverClass::isAvailable()) {
7884
$class = $driverClass;
7985
break;
8086
}
8187
}
82-
} else {
83-
// remove key: 'driver'
88+
}
89+
90+
// remove key: 'driver'
91+
if ($name) {
8492
unset($config['driver']);
8593
}
8694

src/ClientConst.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace PhpComp\Http\Client;
4+
5+
/**
6+
* Class ClientConst
7+
*
8+
* @package PhpComp\Http\Client
9+
*/
10+
class ClientConst
11+
{
12+
public const HTML = 'text/html';
13+
public const JSON = 'application/json';
14+
public const XML = 'application/xml';
15+
16+
public const MULTIPART = 'multipart/form-data';
17+
18+
public const USERAGENT_CURL = 'CURL/7.64.1';
19+
20+
public const USERAGENT_BROWSER = 'Mozilla/5.0 (Linux; Android 9; MI 6 Build/PKQ1.190118.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/86.0.4240.99 XWEB/3121 MMWEBSDK/201201 Mobile Safari/537.36 MMWEBID/356 MicroMessenger/8.0.1.1841(0x2800015D) Process/appbrand2 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android';
21+
}

0 commit comments

Comments
 (0)