Skip to content

Commit

Permalink
Merge pull request #41 from php-http/2.x
Browse files Browse the repository at this point in the history
Upgrade to PSR18  + Httplug 2.0
  • Loading branch information
joelwurtz authored Mar 23, 2019
2 parents e2833e0 + 6717d0c commit 5df1859
Show file tree
Hide file tree
Showing 31 changed files with 276 additions and 231 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ vendor/
composer.lock
phpspec.yml
phpunit.xml
/.php_cs.cache
/tests/server/ssl/*.pem
/tests/server/ssl/*.key
/tests/server/ssl/*.req
27 changes: 18 additions & 9 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
<?php

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

use SLLH\StyleCIBridge\ConfigBridge;
$finder = PhpCsFixer\Finder::create();
$finder->in([
'src',
'tests'
]);

return ConfigBridge::create();
$config->setFinder($finder);

return $config;
10 changes: 0 additions & 10 deletions .styleci.yml

This file was deleted.

21 changes: 11 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ cache:
- $HOME/.composer/cache

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3

env:
global:
Expand All @@ -21,21 +20,23 @@ branches:
- /^analysis-.*$/

matrix:
allow_failures:
- php: hhvm
dist: trusty
fast_finish: true
include:
- php: 5.5
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
- php: hhvm
dist: trusty
- php: 7.2
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.3
env: USE_ASYNC=true

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

install:
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction
- travis_retry composer update ${COMPOSER_FLAGS} --prefer-source --no-interaction --ignore-platform-reqs

before_script:
- vendor/bin/http_test_server > /dev/null 2>&1 &
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 2.0.0 (unreleased)

* Remove response and stream factory, use direct implementation of nyholm/psr7
* Async support with ext-async extension: see https://github.com/concurrent-php/ext-async
* PSR18 and HTTPlug 2 support
* Remove support for php 5.5, 5.6 and 7.0
* SSL Method now defaults to `STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT`

## 1.4.0

* Support for Symfony 4
Expand Down
21 changes: 12 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
}
],
"require": {
"php": "^5.5 || ^7.0",
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/discovery": "^1.0"
"php": "^7.1",
"nyholm/psr7": "^1.0",
"php-http/httplug": "^2.0",
"psr/http-client": "^1.0",
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0"
},
"require-dev": {
"guzzlehttp/psr7": "^1.2",
"php-http/client-integration-tests": "^0.6",
"concurrent-php/async-api": "dev-master",
"friendsofphp/php-cs-fixer": "^2.2",
"php-http/client-integration-tests": "dev-master",
"php-http/message": "^1.0",
"php-http/client-common": "^1.0"
"php-http/client-common": "^2.0"
},
"provide": {
"php-http/client-implementation": "1.0"
Expand All @@ -35,12 +36,14 @@
}
},
"scripts": {
"cs-check": "vendor/bin/php-cs-fixer fix --dry-run",
"cs-fix": "vendor/bin/php-cs-fixer fix",
"test": "vendor/bin/phpunit",
"test-ci": "vendor/bin/phpunit --coverage-clover build/coverage.xml"
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
"dev-master": "2.0-dev"
}
},
"prefer-stable": true,
Expand Down
39 changes: 26 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Http\Client\Socket\Exception\ConnectionException;
use Http\Client\Socket\Exception\InvalidRequestException;
use Http\Client\Socket\Exception\SSLConnectionException;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\ResponseFactory;
use Http\Client\Socket\Exception\TimeoutException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -31,38 +31,44 @@ class Client implements HttpClient
'stream_context_param' => [],
'ssl' => null,
'write_buffer_size' => 8192,
'ssl_method' => STREAM_CRYPTO_METHOD_TLS_CLIENT,
'ssl_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT,
];

private $hasAsync;

/**
* Constructor.
*
* @param ResponseFactory $responseFactory Response factory for creating response
* @param array $config {
* @param array $config {
*
* @var string $remote_socket Remote entrypoint (can be a tcp or unix domain address)
* @var int $timeout Timeout before canceling request
* @var array $stream_context_options Context options as defined in the PHP documentation
* @var array $stream_context_param Context params as defined in the PHP documentation
* @var bool $ssl Use ssl, default to scheme from request, false if not present
* @var int $write_buffer_size Buffer when writing the request body, defaults to 8192
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLS_CLIENT
* @var int $ssl_method Crypto method for ssl/tls, see PHP doc, defaults to STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
* }
*/
public function __construct(ResponseFactory $responseFactory = null, array $config = [])
public function __construct($config1 = [], $config2 = null, array $config = [])
{
if (null === $responseFactory) {
$responseFactory = MessageFactoryDiscovery::find();
$this->hasAsync = PHP_VERSION_ID >= 70300 && \extension_loaded('async');

if (\is_array($config1)) {
$this->config = $this->configure($config1);

return;
}

$this->responseFactory = $responseFactory;
@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.');

$this->config = $this->configure($config);
}

/**
* {@inheritdoc}
*/
public function sendRequest(RequestInterface $request)
public function sendRequest(RequestInterface $request): ResponseInterface
{
$remote = $this->config['remote_socket'];
$useSsl = $this->config['ssl'];
Expand Down Expand Up @@ -104,13 +110,17 @@ public function sendRequest(RequestInterface $request)
*
* @return resource Socket resource
*/
protected function createSocket(RequestInterface $request, $remote, $useSsl)
protected function createSocket(RequestInterface $request, string $remote, bool $useSsl)
{
$errNo = null;
$errMsg = null;
$socket = @stream_socket_client($remote, $errNo, $errMsg, floor($this->config['timeout'] / 1000), STREAM_CLIENT_CONNECT, $this->config['stream_context']);

if (false === $socket) {
if (110 === $errNo) {
throw new TimeoutException($errMsg, $request);
}

throw new ConnectionException($errMsg, $request);
}

Expand Down Expand Up @@ -161,7 +171,6 @@ protected function configure(array $config = [])
/**
* Return remote socket from the request.
*
* @param RequestInterface $request
*
* @throws InvalidRequestException When no remote can be determined from the request
*
Expand All @@ -182,6 +191,10 @@ private function determineRemoteFromRequest(RequestInterface $request)
$endpoint = $request->getHeaderLine('Host');
}

if ($this->hasAsync) {
return sprintf('async-tcp://%s', $endpoint);
}

return sprintf('tcp://%s', $endpoint);
}
}
2 changes: 0 additions & 2 deletions src/Exception/BrokenPipeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class BrokenPipeException extends NetworkException
{
}
2 changes: 0 additions & 2 deletions src/Exception/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class ConnectionException extends NetworkException
{
}
2 changes: 0 additions & 2 deletions src/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class InvalidRequestException extends NetworkException
{
}
23 changes: 23 additions & 0 deletions src/Exception/NetworkException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Http\Client\Socket\Exception;

use Psr\Http\Client\NetworkExceptionInterface;
use Psr\Http\Message\RequestInterface;

class NetworkException extends \RuntimeException implements NetworkExceptionInterface
{
private $request;

public function __construct(string $message, RequestInterface $request, \Exception $previous = null)
{
$this->request = $request;

parent::__construct($message, 0, $previous);
}

public function getRequest(): RequestInterface
{
return $this->request;
}
}
2 changes: 0 additions & 2 deletions src/Exception/SSLConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Http\Client\Socket\Exception;

use Http\Client\Exception\NetworkException;

class SSLConnectionException extends NetworkException
{
}
33 changes: 2 additions & 31 deletions src/Exception/StreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,8 @@

namespace Http\Client\Socket\Exception;

use Http\Client\Exception;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Client\ClientExceptionInterface;

class StreamException extends \RuntimeException implements Exception
class StreamException extends \RuntimeException implements ClientExceptionInterface
{
/**
* The request object.
*
* @var RequestInterface
*/
private $request;

/**
* Accepts an optional request object as 4th param.
*
* @param string $message
* @param int $code
* @param Exception $previous
* @param RequestInterface $request
*/
public function __construct($message = null, $code = null, $previous = null, RequestInterface $request = null)
{
$this->request = $request;
parent::__construct($message, $code, $previous);
}

/**
* @return \Psr\Http\Message\RequestInterface|null
*/
final public function getRequest()
{
return $this->request;
}
}
2 changes: 1 addition & 1 deletion src/Exception/TimeoutException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Http\Client\Socket\Exception;

class TimeoutException extends StreamException
class TimeoutException extends NetworkException
{
}
Loading

0 comments on commit 5df1859

Please sign in to comment.