Skip to content

Commit c961c97

Browse files
authored
Merge pull request #42 from shulard/dependency/react-1.0
Upgrade to react/http instead of react/http-client which is deprecated.
2 parents 412356b + c8d7056 commit c961c97

File tree

11 files changed

+139
-425
lines changed

11 files changed

+139
-425
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/phpspec.yml
55
/phpunit.xml
66
/vendor/
7+
.phpunit.result.cache

.styleci.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ finder:
77
- "src"
88
- "tests"
99

10-
enabled:
11-
- short_array_syntax
12-
1310
disabled:
1411
- phpdoc_annotation_without_dot # This is still buggy: https://github.com/symfony/symfony/pull/19198

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Changed
1313

1414
- Work with HTTPlug 2, drop HTTPlug 1 support
15+
- Move to `react/http` library instead of `react/http-client`
1516

1617
## [2.3.0] - 2019-07-30
1718

composer.json

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
77
"authors": [
88
{
99
"name": "Stéphane HULARD",
10-
"email": "s.hulard@gmail.com"
10+
"email": "s.hulard@chstudio.fr"
1111
}
1212
],
1313
"require": {
1414
"php": "^7.1",
1515
"php-http/httplug": "^2.0",
16-
"react/http-client": "~0.5.9",
17-
"react/dns": "^1.0|^0.4.15",
18-
"react/socket": "^1.0",
19-
"react/stream": "^1.0",
16+
"react/http": "^1.0",
2017
"react/event-loop": "^1.0",
2118
"php-http/discovery": "^1.0"
2219
},
2320
"require-dev": {
24-
"php-http/client-integration-tests": "^2.0",
21+
"php-http/client-integration-tests": "^3.0",
2522
"php-http/message": "^1.0",
26-
"phpunit/phpunit": "^4.8.36 || ^5.4"
23+
"nyholm/psr7": "^1.3"
2724
},
2825
"provide": {
2926
"php-http/client-implementation": "1.0",
@@ -45,7 +42,7 @@
4542
},
4643
"extra": {
4744
"branch-alias": {
48-
"dev-master": "2.4-dev"
45+
"dev-master": "3.x-dev"
4946
}
5047
}
5148
}

src/Client.php

Lines changed: 15 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,22 @@
44

55
use Http\Client\HttpClient;
66
use Http\Client\HttpAsyncClient;
7-
use Http\Client\Exception\HttpException;
8-
use Http\Client\Exception\RequestException;
9-
use Http\Discovery\MessageFactoryDiscovery;
10-
use Http\Discovery\StreamFactoryDiscovery;
11-
use Http\Message\ResponseFactory;
12-
use Http\Message\StreamFactory;
137
use Psr\Http\Message\RequestInterface;
148
use Psr\Http\Message\ResponseInterface;
15-
use Psr\Http\Message\StreamInterface;
169
use React\EventLoop\LoopInterface;
17-
use React\HttpClient\Client as ReactClient;
18-
use React\HttpClient\Request as ReactRequest;
19-
use React\HttpClient\Response as ReactResponse;
10+
use React\Http\Browser as ReactBrowser;
2011

2112
/**
2213
* Client for the React promise implementation.
2314
*
24-
* @author Stéphane Hulard <stephane@hlrd.me>
15+
* @author Stéphane Hulard <s.hulard@chstudio.fr>
2516
*/
2617
class Client implements HttpClient, HttpAsyncClient
2718
{
2819
/**
2920
* React HTTP client.
3021
*
31-
* @var Client
22+
* @var ReactBrowser
3223
*/
3324
private $client;
3425

@@ -39,41 +30,19 @@ class Client implements HttpClient, HttpAsyncClient
3930
*/
4031
private $loop;
4132

42-
/**
43-
* @var ResponseFactory
44-
*/
45-
private $responseFactory;
46-
47-
/**
48-
* @var StreamFactory
49-
*/
50-
private $streamFactory;
51-
5233
/**
5334
* Initialize the React client.
54-
*
55-
* @param ResponseFactory|null $responseFactory
56-
* @param LoopInterface|null $loop
57-
* @param ReactClient|null $client
58-
* @param StreamFactory|null $streamFactory
5935
*/
6036
public function __construct(
61-
ResponseFactory $responseFactory = null,
6237
LoopInterface $loop = null,
63-
ReactClient $client = null,
64-
StreamFactory $streamFactory = null
38+
ReactBrowser $client = null
6539
) {
6640
if (null !== $client && null === $loop) {
67-
throw new \RuntimeException(
68-
'You must give a LoopInterface instance with the Client'
69-
);
41+
throw new \RuntimeException('You must give a LoopInterface instance with the Client');
7042
}
7143

7244
$this->loop = $loop ?: ReactFactory::buildEventLoop();
7345
$this->client = $client ?: ReactFactory::buildHttpClient($this->loop);
74-
75-
$this->responseFactory = $responseFactory ?: MessageFactoryDiscovery::find();
76-
$this->streamFactory = $streamFactory ?: StreamFactoryDiscovery::find();
7746
}
7847

7948
/**
@@ -91,91 +60,17 @@ public function sendRequest(RequestInterface $request): ResponseInterface
9160
*/
9261
public function sendAsyncRequest(RequestInterface $request)
9362
{
94-
$reactRequest = $this->buildReactRequest($request);
95-
$promise = new Promise($this->loop);
96-
97-
$reactRequest->on('error', function (\Exception $error) use ($promise, $request) {
98-
$promise->reject(new RequestException(
99-
$error->getMessage(),
100-
$request,
101-
$error
102-
));
103-
});
104-
105-
$reactRequest->on('response', function (ReactResponse $reactResponse = null) use ($promise, $request) {
106-
$bodyStream = $this->streamFactory->createStream();
107-
$reactResponse->on('data', function ($data) use (&$bodyStream) {
108-
$bodyStream->write((string) $data);
109-
});
110-
111-
$reactResponse->on('end', function (\Exception $error = null) use ($promise, $request, $reactResponse, &$bodyStream) {
112-
$response = $this->buildResponse(
113-
$reactResponse,
114-
$bodyStream
115-
);
116-
if (null !== $error) {
117-
$promise->reject(new HttpException(
118-
$error->getMessage(),
119-
$request,
120-
$response,
121-
$error
122-
));
123-
} else {
124-
$promise->resolve($response);
125-
}
126-
});
127-
});
128-
129-
$reactRequest->end((string) $request->getBody());
130-
131-
return $promise;
132-
}
133-
134-
/**
135-
* Build a React request from the PSR7 RequestInterface.
136-
*
137-
* @param RequestInterface $request
138-
*
139-
* @return ReactRequest
140-
*/
141-
private function buildReactRequest(RequestInterface $request)
142-
{
143-
$headers = [];
144-
145-
foreach ($request->getHeaders() as $name => $value) {
146-
$headers[$name] = (is_array($value) ? $value[0] : $value);
147-
}
148-
149-
$reactRequest = $this->client->request(
150-
$request->getMethod(),
151-
(string) $request->getUri(),
152-
$headers,
153-
$request->getProtocolVersion()
63+
$promise = new Promise(
64+
$this->client->request(
65+
$request->getMethod(),
66+
$request->getUri(),
67+
$request->getHeaders(),
68+
$request->getBody()
69+
),
70+
$this->loop,
71+
$request
15472
);
15573

156-
return $reactRequest;
157-
}
158-
159-
/**
160-
* Transform a React Response to a valid PSR7 ResponseInterface instance.
161-
*
162-
* @param ReactResponse $response
163-
* @param StreamInterface $body
164-
*
165-
* @return ResponseInterface
166-
*/
167-
private function buildResponse(
168-
ReactResponse $response,
169-
StreamInterface $body
170-
) {
171-
$body->rewind();
172-
173-
return $this->responseFactory->createResponse(
174-
$response->getCode(),
175-
$response->getReasonPhrase(),
176-
$response->getHeaders(),
177-
$body,
178-
$response->getVersion()
179-
);
74+
return $promise;
18075
}
18176
}

0 commit comments

Comments
 (0)