Skip to content

Commit

Permalink
Psr17: prevent usage of deprecated StreamFactory and use StreamFactor…
Browse files Browse the repository at this point in the history
…yInterface instead (#87)
  • Loading branch information
glaubinix committed Nov 15, 2023
1 parent a4d05cf commit 3562529
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

# Version 2

## 2.0.0 - (unreleased)

### Changed
- Drop support of deprecated PHP-HTTP `StreamFactory`, only PSR-17 `StreamFactoryInterface` is now supported.

# Version 1

## 1.8.0 - 2023-04-28
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
"php": "^7.1 || ^8.0",
"psr/cache": "^1.0 || ^2.0 || ^3.0",
"php-http/client-common": "^1.9 || ^2.0",
"php-http/message-factory": "^1.0",
"psr/http-factory-implementation": "^1.0",
"symfony/options-resolver": "^2.6 || ^3.0 || ^4.0 || ^5.0 || ^6.0"
},
"require-dev": {
"phpspec/phpspec": "^5.1 || ^6.0 || ^7.0"
"phpspec/phpspec": "^5.1 || ^6.0 || ^7.0",
"nyholm/psr7": "^1.6.1"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 11 additions & 11 deletions spec/CachePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use Http\Client\Common\Plugin\Cache\Generator\SimpleGenerator;
use PhpSpec\Wrapper\Collaborator;
use Prophecy\Argument;
use Http\Message\StreamFactory;
use Http\Promise\FulfilledPromise;
use PhpSpec\ObjectBehavior;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use Http\Client\Common\Plugin\CachePlugin;
Expand All @@ -20,11 +20,11 @@
class CachePluginSpec extends ObjectBehavior
{
/**
* @var StreamFactory&Collaborator
* @var StreamFactoryInterface&Collaborator
*/
private $streamFactory;

function let(CacheItemPoolInterface $pool, StreamFactory $streamFactory)
function let(CacheItemPoolInterface $pool, StreamFactoryInterface $streamFactory)
{
$this->streamFactory = $streamFactory;
$this->beConstructedWith($pool, $streamFactory, [
Expand Down Expand Up @@ -126,7 +126,7 @@ function it_stores_post_requests_when_allowed(
RequestInterface $request,
UriInterface $uri,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
StreamInterface $stream
) {
$this->beConstructedWith($pool, $streamFactory, [
Expand Down Expand Up @@ -181,7 +181,7 @@ function it_does_not_allow_invalid_request_methods(
CacheItemInterface $item,
RequestInterface $request,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
StreamInterface $stream
) {
$this
Expand Down Expand Up @@ -312,7 +312,7 @@ function it_adds_etag_and_modfied_since_to_request(CacheItemPoolInterface $pool,
$this->handleRequest($request, $next, function () {});
}

function it_serves_a_cached_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, UriInterface $uri, StreamInterface $requestBody, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
function it_serves_a_cached_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, UriInterface $uri, StreamInterface $requestBody, ResponseInterface $response, StreamInterface $stream, StreamFactoryInterface $streamFactory)
{
$httpBody = 'body';

Expand Down Expand Up @@ -343,7 +343,7 @@ function it_serves_a_cached_response(CacheItemPoolInterface $pool, CacheItemInte
$this->handleRequest($request, $next, function () {});
}

function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, UriInterface $uri, StreamInterface $requestStream, ResponseInterface $response, StreamInterface $stream, StreamFactory $streamFactory)
function it_serves_and_resaved_expired_response(CacheItemPoolInterface $pool, CacheItemInterface $item, RequestInterface $request, UriInterface $uri, StreamInterface $requestStream, ResponseInterface $response, StreamInterface $stream, StreamFactoryInterface $streamFactory)
{
$httpBody = 'body';

Expand Down Expand Up @@ -398,7 +398,7 @@ function it_caches_private_responses_when_allowed(
RequestInterface $request,
UriInterface $uri,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
StreamInterface $stream
) {
$this->beConstructedThrough('clientCache', [$pool, $streamFactory, [
Expand Down Expand Up @@ -452,7 +452,7 @@ function it_does_not_store_responses_of_requests_to_blacklisted_paths(
RequestInterface $request,
UriInterface $uri,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
StreamInterface $stream
) {
$this->beConstructedThrough('clientCache', [$pool, $streamFactory, [
Expand Down Expand Up @@ -498,7 +498,7 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
RequestInterface $request,
UriInterface $uri,
ResponseInterface $response,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
StreamInterface $stream
) {
$this->beConstructedThrough('clientCache', [$pool, $streamFactory, [
Expand Down Expand Up @@ -550,7 +550,7 @@ function it_stores_responses_of_requests_not_in_blacklisted_paths(
function it_can_be_initialized_with_custom_cache_key_generator(
CacheItemPoolInterface $pool,
CacheItemInterface $item,
StreamFactory $streamFactory,
StreamFactoryInterface $streamFactory,
RequestInterface $request,
UriInterface $uri,
ResponseInterface $response,
Expand Down
23 changes: 7 additions & 16 deletions src/CachePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
use Http\Client\Common\Plugin\Exception\RewindStreamException;
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator;
use Http\Client\Common\Plugin\Cache\Generator\SimpleGenerator;
use Http\Client\Common\Plugin\Cache\Listener\CacheListener;
use Http\Message\StreamFactory;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use Psr\Cache\CacheItemInterface;
Expand Down Expand Up @@ -35,7 +33,7 @@ final class CachePlugin implements Plugin
private $pool;

/**
* @var StreamFactory|StreamFactoryInterface
* @var StreamFactoryInterface
*/
private $streamFactory;

Expand All @@ -52,8 +50,7 @@ final class CachePlugin implements Plugin
private $noCacheFlags = ['no-cache', 'private', 'no-store'];

/**
* @param StreamFactory|StreamFactoryInterface $streamFactory
* @param mixed[] $config
* @param mixed[] $config
*
* bool respect_cache_headers: Whether to look at the cache directives or ignore them
* int default_ttl: (seconds) If we do not respect cache headers or can't calculate a good ttl, use this value
Expand All @@ -69,12 +66,8 @@ final class CachePlugin implements Plugin
* Defaults to an empty array
* }
*/
public function __construct(CacheItemPoolInterface $pool, $streamFactory, array $config = [])
public function __construct(CacheItemPoolInterface $pool, StreamFactoryInterface $streamFactory, array $config = [])
{
if (!($streamFactory instanceof StreamFactory) && !($streamFactory instanceof StreamFactoryInterface)) {
throw new \TypeError(\sprintf('Argument 2 passed to %s::__construct() must be of type %s|%s, %s given.', self::class, StreamFactory::class, StreamFactoryInterface::class, \is_object($streamFactory) ? \get_class($streamFactory) : \gettype($streamFactory)));
}

$this->pool = $pool;
$this->streamFactory = $streamFactory;

Expand All @@ -95,12 +88,11 @@ public function __construct(CacheItemPoolInterface $pool, $streamFactory, array
* This method will setup the cachePlugin in client cache mode. When using the client cache mode the plugin will
* cache responses with `private` cache directive.
*
* @param StreamFactory|StreamFactoryInterface $streamFactory
* @param mixed[] $config For all possible config options see the constructor docs
* @param mixed[] $config For all possible config options see the constructor docs
*
* @return CachePlugin
*/
public static function clientCache(CacheItemPoolInterface $pool, $streamFactory, array $config = [])
public static function clientCache(CacheItemPoolInterface $pool, StreamFactoryInterface $streamFactory, array $config = [])
{
// Allow caching of private requests
if (\array_key_exists('respect_response_cache_directives', $config)) {
Expand All @@ -118,12 +110,11 @@ public static function clientCache(CacheItemPoolInterface $pool, $streamFactory,
* This method will setup the cachePlugin in server cache mode. This is the default caching behavior it refuses to
* cache responses with the `private`or `no-cache` directives.
*
* @param StreamFactory|StreamFactoryInterface $streamFactory
* @param mixed[] $config For all possible config options see the constructor docs
* @param mixed[] $config For all possible config options see the constructor docs
*
* @return CachePlugin
*/
public static function serverCache(CacheItemPoolInterface $pool, $streamFactory, array $config = [])
public static function serverCache(CacheItemPoolInterface $pool, StreamFactoryInterface $streamFactory, array $config = [])
{
return new self($pool, $streamFactory, $config);
}
Expand Down

0 comments on commit 3562529

Please sign in to comment.