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 9 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 @@ -10,6 +10,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
7 changes: 7 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 @@ -739,6 +740,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()->ifNull()->thenEmptyArray()->castToArray()->ifEmpty()->thenUnset()->end()
->prototype('scalar')
->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 Reference($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
4 changes: 4 additions & 0 deletions tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
'stream_factory' => 'httplug.stream_factory',
'config' => [
'methods' => ['GET', 'HEAD'],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down Expand Up @@ -235,6 +236,7 @@ public function testSupportsAllConfigFormats(): void
'methods' => ['GET'],
'cache_key_generator' => null,
'respect_response_cache_directives' => ['X-Foo'],
'cache_listeners' => [],
],
],
'cookie' => [
Expand Down Expand Up @@ -356,6 +358,7 @@ public function testCacheConfigDeprecationCompatibility(): void
'config' => [
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => true,
'cache_listeners' => [],
],
]);
$this->assertProcessedConfigurationEquals($config, [$file]);
Expand All @@ -374,6 +377,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void
'config' => [
'methods' => ['GET', 'HEAD'],
'respect_cache_headers' => false,
'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