Skip to content

Commit 0243e0c

Browse files
committed
Use only psr18 and not httplug anymore, fix psr7 implementation
1 parent ed21563 commit 0243e0c

13 files changed

+58
-74
lines changed

composer.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
],
1111
"require": {
1212
"php": "^7.1",
13-
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0",
14-
"php-http/httplug": "^1.0 || ^2.0",
15-
"php-http/message-factory": "^1.0.2 || ^2.0",
16-
"php-http/discovery": "^1.0"
13+
"nyholm/psr7": "^1.0",
14+
"psr/http-client": "^1.0",
15+
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0"
1716
},
1817
"require-dev": {
1918
"friendsofphp/php-cs-fixer": "^2.2",
20-
"guzzlehttp/psr7": "^1.2",
21-
"php-http/client-integration-tests": "^1.0 || ^2.0",
19+
"php-http/client-integration-tests": "^2.0",
2220
"php-http/message": "^1.0",
23-
"php-http/client-common": "^1.0 || ^2.0"
21+
"php-http/client-common": "^2.0"
2422
},
2523
"provide": {
2624
"php-http/client-implementation": "1.0"

src/Client.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use Http\Client\Socket\Exception\ConnectionException;
77
use Http\Client\Socket\Exception\InvalidRequestException;
88
use Http\Client\Socket\Exception\SSLConnectionException;
9-
use Http\Discovery\MessageFactoryDiscovery;
10-
use Http\Message\ResponseFactory;
119
use Psr\Http\Message\RequestInterface;
1210
use Psr\Http\Message\ResponseInterface;
1311
use Symfony\Component\OptionsResolver\Options;
@@ -38,8 +36,7 @@ class Client implements HttpClient
3836
/**
3937
* Constructor.
4038
*
41-
* @param ResponseFactory $responseFactory Response factory for creating response
42-
* @param array $config {
39+
* @param array $config {
4340
*
4441
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
4542
* @var int $timeout Timeout before canceling request
@@ -50,13 +47,8 @@ class Client implements HttpClient
5047
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
5148
* }
5249
*/
53-
public function __construct(ResponseFactory $responseFactory = null, array $config = [])
50+
public function __construct(array $config = [])
5451
{
55-
if (null === $responseFactory) {
56-
$responseFactory = MessageFactoryDiscovery::find();
57-
}
58-
59-
$this->responseFactory = $responseFactory;
6052
$this->config = $this->configure($config);
6153
}
6254

src/Exception/BrokenPipeException.php

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

33
namespace Http\Client\Socket\Exception;
44

5-
use Http\Client\Exception\NetworkException;
6-
75
class BrokenPipeException extends NetworkException
86
{
97
}

src/Exception/ConnectionException.php

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

33
namespace Http\Client\Socket\Exception;
44

5-
use Http\Client\Exception\NetworkException;
6-
75
class ConnectionException extends NetworkException
86
{
97
}

src/Exception/InvalidRequestException.php

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

33
namespace Http\Client\Socket\Exception;
44

5-
use Http\Client\Exception\NetworkException;
6-
75
class InvalidRequestException extends NetworkException
86
{
97
}

src/Exception/NetworkException.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Http\Client\Socket\Exception;
4+
5+
use Psr\Http\Client\NetworkExceptionInterface;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class NetworkException extends \RuntimeException implements NetworkExceptionInterface
9+
{
10+
private $request;
11+
12+
public function __construct(string $message, RequestInterface $request, \Exception $previous = null)
13+
{
14+
$this->request = $request;
15+
16+
parent::__construct($message, 0, $previous);
17+
}
18+
19+
public function getRequest(): RequestInterface
20+
{
21+
return $this->request;
22+
}
23+
}

src/Exception/SSLConnectionException.php

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

33
namespace Http\Client\Socket\Exception;
44

5-
use Http\Client\Exception\NetworkException;
6-
75
class SSLConnectionException extends NetworkException
86
{
97
}

src/Exception/StreamException.php

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

33
namespace Http\Client\Socket\Exception;
44

5-
use Http\Client\Exception;
6-
use Psr\Http\Message\RequestInterface;
5+
use Psr\Http\Client\ClientExceptionInterface;
76

8-
class StreamException extends \RuntimeException implements Exception
7+
class StreamException extends \RuntimeException implements ClientExceptionInterface
98
{
10-
/**
11-
* The request object.
12-
*
13-
* @var RequestInterface
14-
*/
15-
private $request;
16-
17-
/**
18-
* Accepts an optional request object as 4th param.
19-
*
20-
* @param string $message
21-
* @param int $code
22-
* @param Exception $previous
23-
* @param RequestInterface $request
24-
*/
25-
public function __construct($message = null, $code = null, $previous = null, RequestInterface $request = null)
26-
{
27-
$this->request = $request;
28-
parent::__construct($message, $code, $previous);
29-
}
30-
31-
/**
32-
* @return \Psr\Http\Message\RequestInterface|null
33-
*/
34-
final public function getRequest()
35-
{
36-
return $this->request;
37-
}
389
}

src/Exception/TimeoutException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Http\Client\Socket\Exception;
44

5-
class TimeoutException extends StreamException
5+
class TimeoutException extends NetworkException
66
{
77
}

src/ResponseReader.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Http\Client\Socket\Exception\BrokenPipeException;
66
use Http\Client\Socket\Exception\TimeoutException;
77
use Http\Message\ResponseFactory;
8+
use Nyholm\Psr7\Response;
89
use Psr\Http\Message\RequestInterface;
910
use Psr\Http\Message\ResponseInterface;
1011

@@ -76,8 +77,8 @@ protected function readResponse(RequestInterface $request, $socket): ResponseInt
7677
: '';
7778
}
7879

79-
$response = $this->responseFactory->createResponse($status, $reason, $responseHeaders, null, $protocol);
80-
$stream = $this->createStream($socket, $response);
80+
$response = new Response($status, $responseHeaders, null, $protocol, $reason);
81+
$stream = $this->createStream($socket, $request, $response);
8182

8283
return $response->withBody($stream);
8384
}
@@ -87,14 +88,14 @@ protected function readResponse(RequestInterface $request, $socket): ResponseInt
8788
*
8889
* @param resource $socket
8990
*/
90-
protected function createStream($socket, ResponseInterface $response): Stream
91+
protected function createStream($socket, RequestInterface $request, ResponseInterface $response): Stream
9192
{
9293
$size = null;
9394

9495
if ($response->hasHeader('Content-Length')) {
9596
$size = (int) $response->getHeaderLine('Content-Length');
9697
}
9798

98-
return new Stream($socket, $size);
99+
return new Stream($request, $socket, $size);
99100
}
100101
}

src/Stream.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Http\Client\Socket\Exception\StreamException;
66
use Http\Client\Socket\Exception\TimeoutException;
7+
use Psr\Http\Message\RequestInterface;
78
use Psr\Http\Message\StreamInterface;
89

910
/**
@@ -42,16 +43,21 @@ class Stream implements StreamInterface
4243
*/
4344
private $readed = 0;
4445

46+
/**
47+
* @var RequestInterface request associated to this stream
48+
*/
49+
private $request;
50+
4551
/**
4652
* Create the stream.
4753
*
4854
* @param resource $socket
49-
* @param int $size
5055
*/
51-
public function __construct($socket, ?int $size = null)
56+
public function __construct(RequestInterface $request, $socket, ?int $size = null)
5257
{
5358
$this->socket = $socket;
5459
$this->size = $size;
60+
$this->request = $request;
5561
}
5662

5763
/**
@@ -175,7 +181,7 @@ public function read($length)
175181
$read = fread($this->socket, $length);
176182

177183
if ($this->getMetadata('timed_out')) {
178-
throw new TimeoutException('Stream timed out while reading data');
184+
throw new TimeoutException('Stream timed out while reading data', $this->request);
179185
}
180186

181187
$this->readed += strlen($read);

tests/SocketHttpClientTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function createClient($options = [])
1212
{
1313
$messageFactory = new GuzzleMessageFactory();
1414

15-
return new HttpMethodsClient(new SocketHttpClient($messageFactory, $options), $messageFactory);
15+
return new HttpMethodsClient(new SocketHttpClient($options), $messageFactory);
1616
}
1717

1818
public function testTcpSocketDomain()
@@ -26,7 +26,7 @@ public function testTcpSocketDomain()
2626
}
2727

2828
/**
29-
* @expectedException \Http\Client\Exception\NetworkException
29+
* @expectedException \Http\Client\Socket\Exception\NetworkException
3030
*/
3131
public function testNoRemote()
3232
{
@@ -55,7 +55,7 @@ public function testRemoteInHostHeader()
5555
}
5656

5757
/**
58-
* @expectedException \Http\Client\Exception\NetworkException
58+
* @expectedException \Http\Client\Socket\Exception\NetworkException
5959
*/
6060
public function testBrokenSocket()
6161
{
@@ -95,7 +95,7 @@ public function testUnixSocketDomain()
9595
}
9696

9797
/**
98-
* @expectedException \Http\Client\Exception\NetworkException
98+
* @expectedException \Http\Client\Socket\Exception\NetworkException
9999
*/
100100
public function testNetworkExceptionOnConnectError()
101101
{
@@ -173,7 +173,7 @@ public function testInvalidSslConnectionWithClientCertificate()
173173
}
174174

175175
/**
176-
* @expectedException \Http\Client\Exception\NetworkException
176+
* @expectedException \Http\Client\Socket\Exception\NetworkException
177177
*/
178178
public function testNetworkExceptionOnSslError()
179179
{
@@ -184,7 +184,7 @@ public function testNetworkExceptionOnSslError()
184184
}
185185

186186
/**
187-
* @expectedException \Http\Client\Exception\NetworkException
187+
* @expectedException \Http\Client\Socket\Exception\NetworkException
188188
*/
189189
public function testNetworkExceptionOnTimeout()
190190
{

tests/StreamTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Http\Client\Socket\Tests;
44

55
use Http\Client\Socket\Stream;
6+
use Nyholm\Psr7\Request;
67
use PHPUnit\Framework\TestCase;
78

89
class StreamTest extends TestCase
@@ -13,7 +14,7 @@ public function createSocket($body, $useSize = true)
1314
fwrite($socket, $body);
1415
fseek($socket, 0);
1516

16-
return new Stream($socket, $useSize ? strlen($body) : null);
17+
return new Stream(new Request('GET', '/'), $socket, $useSize ? strlen($body) : null);
1718
}
1819

1920
public function testToString()
@@ -56,7 +57,7 @@ public function testEof()
5657
$socket = fopen('php://memory', 'rw+');
5758
fwrite($socket, 'Body');
5859
fseek($socket, 0);
59-
$stream = new Stream($socket);
60+
$stream = new Stream(new Request('GET', '/'), $socket);
6061

6162
$this->assertEquals('Body', $stream->getContents());
6263
fwrite($socket, "\0");
@@ -116,7 +117,7 @@ public function testTimeout()
116117
$socket = fsockopen('php.net', 80);
117118
socket_set_timeout($socket, 0, 100);
118119

119-
$stream = new Stream($socket, 50);
120+
$stream = new Stream(new Request('GET', '/'), $socket, 50);
120121
$stream->getContents();
121122
}
122123

@@ -138,7 +139,7 @@ public function testClose()
138139
fwrite($socket, 'Body');
139140
fseek($socket, 0);
140141

141-
$stream = new Stream($socket);
142+
$stream = new Stream(new Request('GET', '/'), $socket);
142143
$stream->close();
143144

144145
$this->assertFalse(is_resource($socket));

0 commit comments

Comments
 (0)