Skip to content

Upgrade to PSR18 + Httplug 2.0 #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Mar 23, 2019
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

14 changes: 5 additions & 9 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,15 +20,12 @@ 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"

before_install:
- travis_retry composer self-update
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 2.0.0 (unreleased)

* 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
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@
}
],
"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",
"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",
"friendsofphp/php-cs-fixer": "^2.2",
"php-http/client-integration-tests": "^2.0",
"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 +34,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
22 changes: 7 additions & 15 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
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 Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

Expand All @@ -31,38 +30,32 @@ 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,
];

/**
* 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(array $config = [])
{
if (null === $responseFactory) {
$responseFactory = MessageFactoryDiscovery::find();
}

$this->responseFactory = $responseFactory;
$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,7 +97,7 @@ 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;
Expand Down Expand Up @@ -161,7 +154,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 Down
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
{
}
21 changes: 6 additions & 15 deletions src/RequestWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ trait RequestWriter
/**
* Write a request to a socket.
*
* @param resource $socket
* @param RequestInterface $request
* @param int $bufferSize
* @param resource $socket
*
* @throws BrokenPipeException
*/
protected function writeRequest($socket, RequestInterface $request, $bufferSize = 8192)
protected function writeRequest($socket, RequestInterface $request, int $bufferSize = 8192)
{
if (false === $this->fwrite($socket, $this->transformRequestHeadersToString($request))) {
throw new BrokenPipeException('Failed to send request, underlying socket not accessible, (BROKEN EPIPE)', $request);
Expand All @@ -37,13 +35,11 @@ protected function writeRequest($socket, RequestInterface $request, $bufferSize
/**
* Write Body of the request.
*
* @param resource $socket
* @param RequestInterface $request
* @param int $bufferSize
* @param resource $socket
*
* @throws BrokenPipeException
*/
protected function writeBody($socket, RequestInterface $request, $bufferSize = 8192)
protected function writeBody($socket, RequestInterface $request, int $bufferSize = 8192)
{
$body = $request->getBody();

Expand All @@ -62,12 +58,8 @@ protected function writeBody($socket, RequestInterface $request, $bufferSize = 8

/**
* Produce the header of request as a string based on a PSR Request.
*
* @param RequestInterface $request
*
* @return string
*/
protected function transformRequestHeadersToString(RequestInterface $request)
protected function transformRequestHeadersToString(RequestInterface $request): string
{
$message = vsprintf('%s %s HTTP/%s', [
strtoupper($request->getMethod()),
Expand All @@ -90,11 +82,10 @@ protected function transformRequestHeadersToString(RequestInterface $request)
* @see https://secure.phabricator.com/rPHU69490c53c9c2ef2002bc2dd4cecfe9a4b080b497
*
* @param resource $stream The stream resource
* @param string $bytes Bytes written in the stream
*
* @return bool|int false if pipe is broken, number of bytes written otherwise
*/
private function fwrite($stream, $bytes)
private function fwrite($stream, string $bytes)
{
if (!strlen($bytes)) {
return 0;
Expand Down
Loading