Skip to content

Adding Support for cache-plugins cache_listeners #361

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 13 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Configured clients are now tagged with `'httplug.client'`
- Adds a link to profiler page when response is from a Symfony application with
profiler enabled
- Adding `cache_listeners` option of `php-http/cache-plugin`

### Changed

Expand Down
23 changes: 23 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Http\HttplugBundle\DependencyInjection;

use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator;
use Http\Client\Common\Plugin\Cache\Listener\CacheListener;
use Http\Client\Common\Plugin\CachePlugin;
use Http\Client\Common\Plugin\Journal;
use Http\Client\Plugin\Vcr\NamingStrategy\NamingStrategyInterface;
Expand All @@ -13,6 +14,7 @@
use Http\Message\StreamFactory;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use ReflectionClass;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand Down Expand Up @@ -739,6 +741,27 @@ private function createCachePluginNode()
->end()
->end()
->end()
->arrayNode('cache_listeners')
->info('An array of classes to act on the response based on the results of the cache check. Must implement '.CacheListener::class.'. Defaults to an empty array.')
->beforeNormalization()->castToArray()->ifEmpty()->thenUnset()->end()
->defaultValue([])
->prototype('scalar')
->validate()
->ifTrue(function ($v) {
$vs = is_array($v) ? $v : (is_null($v) ? [] : [$v]);

return empty($vs) || array_reduce($vs, function ($r, $e) {
return empty($e) || !class_exists($e) || !(new ReflectionClass($e))->implementsInterface(CacheListener::class);
}, false);
})
->thenInvalid('A given listener class does not implement '.CacheListener::class)
->end()
->end()
->validate()
->ifEmpty()
->thenUnset()
->end()
->end()
->scalarNode('respect_cache_headers')
->info('Whether we should care about cache headers or not [DEPRECATED]')
->beforeNormalization()
Expand Down
12 changes: 12 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,22 @@ private function configurePluginByName($name, Definition $definition, array $con
switch ($name) {
case 'cache':
$options = $config['config'];

if (!empty($options['cache_key_generator'])) {
$options['cache_key_generator'] = new Reference($options['cache_key_generator']);
}

if (!empty($options['cache_listeners'])) {
foreach ($options['cache_listeners'] as $i => $listener) {
if (!empty($listener)) {
$options['cache_listeners'][$i] = new Definition($listener);
}
}
}
if (!count($options['cache_listeners'])) {
unset($options['cache_listeners']);
}

$definition
->replaceArgument(0, new Reference($config['cache_pool']))
->replaceArgument(1, new Reference($config['stream_factory']))
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
'methods' => ['GET'],
'cache_key_generator' => null,
'respect_response_cache_directives' => ['X-Foo'],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down
2 changes: 1 addition & 1 deletion tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<my_service type="service" service="my_auth_service"/>
</authentication>
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null">
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null" cache-listeners="">
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
<method>GET</method>
</config>
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ httplug:
cache_key_generator: null
respect_response_cache_directives:
- X-Foo
cache_listeners: []
cookie:
cookie_jar: my_cookie_jar
decoder:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
httplug:
plugins:
cache:
cache_pool: my_cache_pool
config:
cache_listeners: ['NotExistentClass']
15 changes: 15 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Http\HttplugBundle\Tests\Unit\DependencyInjection;

use Http\Client\Common\Plugin\Cache\Listener\CacheListener;
use Http\HttplugBundle\DependencyInjection\Configuration;
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionConfigurationTestCase;
Expand Down Expand Up @@ -43,6 +44,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
'stream_factory' => 'httplug.stream_factory',
'config' => [
'methods' => ['GET', 'HEAD'],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down Expand Up @@ -233,6 +235,7 @@ public function testSupportsAllConfigFormats(): void
'methods' => ['GET'],
'cache_key_generator' => null,
'respect_response_cache_directives' => ['X-Foo'],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down Expand Up @@ -354,6 +357,7 @@ public function testCacheConfigDeprecationCompatibility(): void
'config' => [
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => true,
'cache_listeners' => [],
],
]);
$this->assertProcessedConfigurationEquals($config, [$file]);
Expand All @@ -372,6 +376,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void
'config' => [
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => false,
'cache_listeners' => [],
],
]);
$this->assertProcessedConfigurationEquals($config, [$file]);
Expand Down Expand Up @@ -420,4 +425,14 @@ public function testInvalidCapturedBodyLengthString(): void
$this->expectExceptionMessage('The child node "captured_body_length" at path "httplug.profiling" must be an integer or null');
$this->assertProcessedConfigurationEquals([], [$file]);
}

public function testInvalidCacheConfigCacheListeners(): void
{
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_cache_listener_config.yml';

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('A given listener class does not implement '.CacheListener::class);

$this->assertProcessedConfigurationEquals($this->emptyConfig, [$file]);
}
}
42 changes: 42 additions & 0 deletions tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Http\HttplugBundle\Collector\PluginClientFactoryListener;
use Http\HttplugBundle\DependencyInjection\HttplugExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Kernel;
use Http\Adapter\Guzzle6\Client;
Expand Down Expand Up @@ -261,6 +262,47 @@ public function testCachePluginConfigCacheKeyGeneratorReference(): void
$this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
}

public function testCachePluginConfigCacheListenersDefinition(): void
{
$this->load([
'plugins' => [
'cache' => [
'cache_pool' => 'my_cache_pool',
'config' => [
'cache_listeners' => [
'\Http\Client\Common\Plugin\Cache\Listener\AddHeaderCacheListener',
],
],
],
],
]);

$cachePlugin = $this->container->findDefinition('httplug.plugin.cache');

$config = $cachePlugin->getArgument(2);
$this->assertArrayHasKey('cache_listeners', $config);
$this->assertContainsOnlyInstancesOf(Definition::class, $config['cache_listeners']);
}

public function testCachePluginInvalidConfigCacheListenersDefinition(): void
{
$this->load([
'plugins' => [
'cache' => [
'cache_pool' => 'my_cache_pool',
'config' => [
'cache_listeners' => [],
],
],
],
]);

$cachePlugin = $this->container->findDefinition('httplug.plugin.cache');

$config = $cachePlugin->getArgument(2);
$this->assertArrayNotHasKey('cache_listeners', $config);
}

public function testContentTypePluginAllowedOptions(): void
{
$this->load([
Expand Down