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 all 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
8 changes: 8 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 Down Expand Up @@ -717,6 +718,7 @@ private function createCachePluginNode()
$config
->fixXmlConfig('method')
->fixXmlConfig('respect_response_cache_directive')
->fixXmlConfig('cache_listener')
->addDefaultsIfNotSet()
->validate()
->ifTrue(function ($config) {
Expand Down Expand Up @@ -768,6 +770,12 @@ private function createCachePluginNode()
->end()
->end()
->end()
->arrayNode('cache_listeners')
->info('A list of service ids to act on the response based on the results of the cache check. Must implement '.CacheListener::class.'. Defaults to an empty array.')
->beforeNormalization()->castToArray()->end()
->prototype('scalar')
->end()
->end()
->scalarNode('respect_cache_headers')
->info('Whether we should care about cache headers or not [DEPRECATED]')
->beforeNormalization()
Expand Down
8 changes: 8 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ private function configurePluginByName($name, Definition $definition, array $con
unset($options['blacklisted_paths']);
}

$options['cache_listeners'] = array_map(function (string $serviceName): Reference {
return new Reference($serviceName);
}, $options['cache_listeners']);

if (0 === 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
6 changes: 5 additions & 1 deletion tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@
'methods' => ['GET'],
'cache_key_generator' => null,
'respect_response_cache_directives' => ['X-Foo'],
'blacklisted_paths' => ['@/path/not-to-be/cached@']
'blacklisted_paths' => ['@/path/not-to-be/cached@'],
'cache_listeners' => [
'my_cache_listener_0',
'my_cache_listener_1',
],
],
],
'cookie' => [
Expand Down
2 changes: 2 additions & 0 deletions tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
</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">
<cache-listener>my_cache_listener_0</cache-listener>
<cache-listener>my_cache_listener_1</cache-listener>
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
<method>GET</method>
<blacklisted-paths>@/path/not-to-be/cached@</blacklisted-paths>
Expand Down
3 changes: 3 additions & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ httplug:
- X-Foo
blacklisted_paths:
- '@/path/not-to-be/cached@'
cache_listeners:
- my_cache_listener_0
- my_cache_listener_1
cookie:
cookie_jar: my_cookie_jar
decoder:
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
'config' => [
'methods' => ['GET', 'HEAD'],
'blacklisted_paths' => [],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down Expand Up @@ -266,6 +267,10 @@ public function testSupportsAllConfigFormats(): void
'cache_key_generator' => null,
'respect_response_cache_directives' => ['X-Foo'],
'blacklisted_paths' => ['@/path/not-to-be/cached@'],
'cache_listeners' => [
'my_cache_listener_0',
'my_cache_listener_1',
],
],
],
'cookie' => [
Expand Down Expand Up @@ -388,6 +393,7 @@ public function testCacheConfigDeprecationCompatibility(): void
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => true,
'blacklisted_paths' => [],
'cache_listeners' => [],
],
]);
$this->assertProcessedConfigurationEquals($config, [$file]);
Expand All @@ -407,6 +413,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => false,
'blacklisted_paths' => [],
'cache_listeners' => [],
],
]);
$this->assertProcessedConfigurationEquals($config, [$file]);
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,29 @@ 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' => [
'httplug.plugin.cache.listeners.add_header',
],
],
],
],
]);

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

$config = $cachePlugin->getArgument(2);
$this->assertArrayHasKey('cache_listeners', $config);
$this->assertContainsOnlyInstancesOf(Reference::class, $config['cache_listeners']);
$this->assertSame('httplug.plugin.cache.listeners.add_header', (string) $config['cache_listeners'][0]);
}

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