diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index e200ad81..d3f44f13 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -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; @@ -717,6 +718,7 @@ private function createCachePluginNode() $config ->fixXmlConfig('method') ->fixXmlConfig('respect_response_cache_directive') + ->fixXmlConfig('cache_listener') ->addDefaultsIfNotSet() ->validate() ->ifTrue(function ($config) { @@ -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() diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 965e3390..f6aacb05 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -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'])) diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 8168d405..14573391 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -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' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 9405491e..48449964 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -62,6 +62,8 @@ + my_cache_listener_0 + my_cache_listener_1 X-Foo GET @/path/not-to-be/cached@ diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index 3074b6c3..1190f9c9 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -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: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index c773c797..8d07b144 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -61,6 +61,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase 'config' => [ 'methods' => ['GET', 'HEAD'], 'blacklisted_paths' => [], + 'cache_listeners' => [], ], ], 'cookie' => [ @@ -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' => [ @@ -388,6 +393,7 @@ public function testCacheConfigDeprecationCompatibility(): void 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => true, 'blacklisted_paths' => [], + 'cache_listeners' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); @@ -407,6 +413,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => false, 'blacklisted_paths' => [], + 'cache_listeners' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); diff --git a/tests/Unit/DependencyInjection/HttplugExtensionTest.php b/tests/Unit/DependencyInjection/HttplugExtensionTest.php index d4b8731c..f7248992 100644 --- a/tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -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([