Skip to content

Commit 5df1859

Browse files
authored
Merge pull request #41 from php-http/2.x
Upgrade to PSR18 + Httplug 2.0
2 parents e2833e0 + 6717d0c commit 5df1859

31 files changed

+276
-231
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ vendor/
44
composer.lock
55
phpspec.yml
66
phpunit.xml
7+
/.php_cs.cache
8+
/tests/server/ssl/*.pem
9+
/tests/server/ssl/*.key
10+
/tests/server/ssl/*.req

.php_cs

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
<?php
22

3-
/*
4-
* In order to make it work, fabpot/php-cs-fixer and sllh/php-cs-fixer-styleci-bridge must be installed globally
5-
* with composer.
6-
*
7-
* @link https://github.com/Soullivaneuh/php-cs-fixer-styleci-bridge
8-
* @link https://github.com/FriendsOfPHP/PHP-CS-Fixer
9-
*/
3+
$config = PhpCsFixer\Config::create();
4+
$config->setRules([
5+
'@PSR2' => true,
6+
'@Symfony' => true,
7+
'array_syntax' => [
8+
'syntax' => 'short',
9+
],
10+
'no_empty_phpdoc' => true,
11+
'no_superfluous_phpdoc_tags' => true,
12+
]);
1013

11-
use SLLH\StyleCIBridge\ConfigBridge;
14+
$finder = PhpCsFixer\Finder::create();
15+
$finder->in([
16+
'src',
17+
'tests'
18+
]);
1219

13-
return ConfigBridge::create();
20+
$config->setFinder($finder);
21+
22+
return $config;

.styleci.yml

-10
This file was deleted.

.travis.yml

+11-10
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ cache:
77
- $HOME/.composer/cache
88

99
php:
10-
- 5.5
11-
- 5.6
12-
- 7.0
1310
- 7.1
11+
- 7.2
12+
- 7.3
1413

1514
env:
1615
global:
@@ -21,21 +20,23 @@ branches:
2120
- /^analysis-.*$/
2221

2322
matrix:
24-
allow_failures:
25-
- php: hhvm
26-
dist: trusty
2723
fast_finish: true
2824
include:
29-
- php: 5.5
25+
- php: 7.1
3026
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
31-
- php: hhvm
32-
dist: trusty
27+
- php: 7.2
28+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
29+
- php: 7.3
30+
env: USE_ASYNC=true
3331

3432
before_install:
3533
- travis_retry composer self-update
34+
- if [[ "$USE_ASYNC" = true ]]; then git clone https://github.com/concurrent-php/ext-async.git /tmp/async; fi
35+
- if [[ "$USE_ASYNC" = true ]]; then cd /tmp/async && phpize && ./configure && sudo make install; fi
36+
- if [[ "$USE_ASYNC" = true ]]; then echo "extension = async.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini && php -m && cd $TRAVIS_BUILD_DIR; fi
3637

3738
install:
38-
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
39+
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction --ignore-platform-reqs
3940

4041
before_script:
4142
- vendor/bin/http_test_server > /dev/null 2>&1 &

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Change Log
22

3+
## 2.0.0 (unreleased)
4+
5+
* Remove response and stream factory, use direct implementation of nyholm/psr7
6+
* Async support with ext-async extension: see https://github.com/concurrent-php/ext-async
7+
* PSR18 and HTTPlug 2 support
8+
* Remove support for php 5.5, 5.6 and 7.0
9+
* SSL Method now defaults to `STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT`
10+
311
## 1.4.0
412

513
* Support for Symfony 4

composer.json

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
}
1010
],
1111
"require": {
12-
"php": "^5.5 || ^7.0",
13-
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0",
14-
"php-http/httplug": "^1.0",
15-
"php-http/message-factory": "^1.0.2",
16-
"php-http/discovery": "^1.0"
12+
"php": "^7.1",
13+
"nyholm/psr7": "^1.0",
14+
"php-http/httplug": "^2.0",
15+
"psr/http-client": "^1.0",
16+
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0"
1717
},
1818
"require-dev": {
19-
"guzzlehttp/psr7": "^1.2",
20-
"php-http/client-integration-tests": "^0.6",
19+
"concurrent-php/async-api": "dev-master",
20+
"friendsofphp/php-cs-fixer": "^2.2",
21+
"php-http/client-integration-tests": "dev-master",
2122
"php-http/message": "^1.0",
22-
"php-http/client-common": "^1.0"
23+
"php-http/client-common": "^2.0"
2324
},
2425
"provide": {
2526
"php-http/client-implementation": "1.0"
@@ -35,12 +36,14 @@
3536
}
3637
},
3738
"scripts": {
39+
"cs-check": "vendor/bin/php-cs-fixer fix --dry-run",
40+
"cs-fix": "vendor/bin/php-cs-fixer fix",
3841
"test": "vendor/bin/phpunit",
3942
"test-ci": "vendor/bin/phpunit --coverage-clover build/coverage.xml"
4043
},
4144
"extra": {
4245
"branch-alias": {
43-
"dev-master": "1.1-dev"
46+
"dev-master": "2.0-dev"
4447
}
4548
},
4649
"prefer-stable": true,

src/Client.php

+26-13
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
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;
9+
use Http\Client\Socket\Exception\TimeoutException;
1110
use Psr\Http\Message\RequestInterface;
11+
use Psr\Http\Message\ResponseInterface;
1212
use Symfony\Component\OptionsResolver\Options;
1313
use Symfony\Component\OptionsResolver\OptionsResolver;
1414

@@ -31,38 +31,44 @@ class Client implements HttpClient
3131
'stream_context_param' => [],
3232
'ssl' => null,
3333
'write_buffer_size' => 8192,
34-
'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
34+
'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
3535
];
3636

37+
private $hasAsync;
38+
3739
/**
3840
* Constructor.
3941
*
40-
* @param ResponseFactory $responseFactory Response factory for creating response
41-
* @param array $config {
42+
* @param array $config {
4243
*
4344
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
4445
* @var int $timeout Timeout before canceling request
4546
* @var array $stream_context_options Context options as defined in the PHP documentation
4647
* @var array $stream_context_param Context params as defined in the PHP documentation
4748
* @var bool $ssl Use ssl, default to scheme from request, false if not present
4849
* @var int $write_buffer_size Buffer when writing the request body, defaults to 8192
49-
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLS_CLIENT
50+
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
5051
* }
5152
*/
52-
public function __construct(ResponseFactory $responseFactory = null, array $config = [])
53+
public function __construct($config1 = [], $config2 = null, array $config = [])
5354
{
54-
if (null === $responseFactory) {
55-
$responseFactory = MessageFactoryDiscovery::find();
55+
$this->hasAsync = PHP_VERSION_ID >= 70300 && \extension_loaded('async');
56+
57+
if (\is_array($config1)) {
58+
$this->config = $this->configure($config1);
59+
60+
return;
5661
}
5762

58-
$this->responseFactory = $responseFactory;
63+
@trigger_error(E_USER_DEPRECATED, 'Passing a Psr\Http\Message\ResponseFactoryInterface and a Psr\Http\Message\StreamFactoryInterface to SocketClient is deprecated, and will be removed in 3.0, you should only pass config options.');
64+
5965
$this->config = $this->configure($config);
6066
}
6167

6268
/**
6369
* {@inheritdoc}
6470
*/
65-
public function sendRequest(RequestInterface $request)
71+
public function sendRequest(RequestInterface $request): ResponseInterface
6672
{
6773
$remote = $this->config['remote_socket'];
6874
$useSsl = $this->config['ssl'];
@@ -104,13 +110,17 @@ public function sendRequest(RequestInterface $request)
104110
*
105111
* @return resource Socket resource
106112
*/
107-
protected function createSocket(RequestInterface $request, $remote, $useSsl)
113+
protected function createSocket(RequestInterface $request, string $remote, bool $useSsl)
108114
{
109115
$errNo = null;
110116
$errMsg = null;
111117
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);
112118

113119
if (false === $socket) {
120+
if (110 === $errNo) {
121+
throw new TimeoutException($errMsg, $request);
122+
}
123+
114124
throw new ConnectionException($errMsg, $request);
115125
}
116126

@@ -161,7 +171,6 @@ protected function configure(array $config = [])
161171
/**
162172
* Return remote socket from the request.
163173
*
164-
* @param RequestInterface $request
165174
*
166175
* @throws InvalidRequestException When no remote can be determined from the request
167176
*
@@ -182,6 +191,10 @@ private function determineRemoteFromRequest(RequestInterface $request)
182191
$endpoint = $request->getHeaderLine('Host');
183192
}
184193

194+
if ($this->hasAsync) {
195+
return sprintf('async-tcp://%s', $endpoint);
196+
}
197+
185198
return sprintf('tcp://%s', $endpoint);
186199
}
187200
}

src/Exception/BrokenPipeException.php

-2
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

-2
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

-2
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

+23
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

-2
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

+2-31
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

+1-1
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
}

0 commit comments

Comments
 (0)