Skip to content

Commit eabb68e

Browse files
authored
Merge pull request #361 from nobYsDarling/master
Adding Support for cache-plugins cache_listeners
2 parents 78a0a40 + 1b99d94 commit eabb68e

File tree

7 files changed

+56
-1
lines changed

7 files changed

+56
-1
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Http\HttplugBundle\DependencyInjection;
66

77
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator;
8+
use Http\Client\Common\Plugin\Cache\Listener\CacheListener;
89
use Http\Client\Common\Plugin\CachePlugin;
910
use Http\Client\Common\Plugin\Journal;
1011
use Http\Client\Plugin\Vcr\NamingStrategy\NamingStrategyInterface;
@@ -719,6 +720,7 @@ private function createCachePluginNode()
719720
$config
720721
->fixXmlConfig('method')
721722
->fixXmlConfig('respect_response_cache_directive')
723+
->fixXmlConfig('cache_listener')
722724
->addDefaultsIfNotSet()
723725
->validate()
724726
->ifTrue(function ($config) {
@@ -770,6 +772,12 @@ private function createCachePluginNode()
770772
->end()
771773
->end()
772774
->end()
775+
->arrayNode('cache_listeners')
776+
->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.')
777+
->beforeNormalization()->castToArray()->end()
778+
->prototype('scalar')
779+
->end()
780+
->end()
773781
->scalarNode('respect_cache_headers')
774782
->info('Whether we should care about cache headers or not [DEPRECATED]')
775783
->beforeNormalization()

src/DependencyInjection/HttplugExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ private function configurePluginByName($name, Definition $definition, array $con
213213
unset($options['blacklisted_paths']);
214214
}
215215

216+
$options['cache_listeners'] = array_map(function (string $serviceName): Reference {
217+
return new Reference($serviceName);
218+
}, $options['cache_listeners']);
219+
220+
if (0 === count($options['cache_listeners'])) {
221+
unset($options['cache_listeners']);
222+
}
223+
216224
$definition
217225
->replaceArgument(0, new Reference($config['cache_pool']))
218226
->replaceArgument(1, new Reference($config['stream_factory']))

tests/Resources/Fixtures/config/full.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@
109109
'methods' => ['GET'],
110110
'cache_key_generator' => null,
111111
'respect_response_cache_directives' => ['X-Foo'],
112-
'blacklisted_paths' => ['@/path/not-to-be/cached@']
112+
'blacklisted_paths' => ['@/path/not-to-be/cached@'],
113+
'cache_listeners' => [
114+
'my_cache_listener_0',
115+
'my_cache_listener_1',
116+
],
113117
],
114118
],
115119
'cookie' => [

tests/Resources/Fixtures/config/full.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
</authentication>
6363
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
6464
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null">
65+
<cache-listener>my_cache_listener_0</cache-listener>
66+
<cache-listener>my_cache_listener_1</cache-listener>
6567
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
6668
<method>GET</method>
6769
<blacklisted-paths>@/path/not-to-be/cached@</blacklisted-paths>

tests/Resources/Fixtures/config/full.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ httplug:
8282
- X-Foo
8383
blacklisted_paths:
8484
- '@/path/not-to-be/cached@'
85+
cache_listeners:
86+
- my_cache_listener_0
87+
- my_cache_listener_1
8588
cookie:
8689
cookie_jar: my_cookie_jar
8790
decoder:

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
6363
'config' => [
6464
'methods' => ['GET', 'HEAD'],
6565
'blacklisted_paths' => [],
66+
'cache_listeners' => [],
6667
],
6768
],
6869
'cookie' => [
@@ -268,6 +269,10 @@ public function testSupportsAllConfigFormats(): void
268269
'cache_key_generator' => null,
269270
'respect_response_cache_directives' => ['X-Foo'],
270271
'blacklisted_paths' => ['@/path/not-to-be/cached@'],
272+
'cache_listeners' => [
273+
'my_cache_listener_0',
274+
'my_cache_listener_1',
275+
],
271276
],
272277
],
273278
'cookie' => [
@@ -390,6 +395,7 @@ public function testCacheConfigDeprecationCompatibility(): void
390395
'methods' => ['GET', 'HEAD'],
391396
'respect_cache_headers' => true,
392397
'blacklisted_paths' => [],
398+
'cache_listeners' => [],
393399
],
394400
]);
395401
$this->assertProcessedConfigurationEquals($config, [$file]);
@@ -409,6 +415,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void
409415
'methods' => ['GET', 'HEAD'],
410416
'respect_cache_headers' => false,
411417
'blacklisted_paths' => [],
418+
'cache_listeners' => [],
412419
],
413420
]);
414421
$this->assertProcessedConfigurationEquals($config, [$file]);

tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,29 @@ public function testCachePluginConfigCacheKeyGeneratorReference(): void
263263
$this->assertSame('header_cache_key_generator', (string) $config['cache_key_generator']);
264264
}
265265

266+
public function testCachePluginConfigCacheListenersDefinition(): void
267+
{
268+
$this->load([
269+
'plugins' => [
270+
'cache' => [
271+
'cache_pool' => 'my_cache_pool',
272+
'config' => [
273+
'cache_listeners' => [
274+
'httplug.plugin.cache.listeners.add_header',
275+
],
276+
],
277+
],
278+
],
279+
]);
280+
281+
$cachePlugin = $this->container->findDefinition('httplug.plugin.cache');
282+
283+
$config = $cachePlugin->getArgument(2);
284+
$this->assertArrayHasKey('cache_listeners', $config);
285+
$this->assertContainsOnlyInstancesOf(Reference::class, $config['cache_listeners']);
286+
$this->assertSame('httplug.plugin.cache.listeners.add_header', (string) $config['cache_listeners'][0]);
287+
}
288+
266289
public function testContentTypePluginAllowedOptions(): void
267290
{
268291
$this->load([

0 commit comments

Comments
 (0)