From 008de9a2f989cbb9fc7fc03fb458d68cc59899b1 Mon Sep 17 00:00:00 2001 From: Till Hildebrandt Date: Thu, 28 Nov 2019 10:20:22 +0100 Subject: [PATCH 1/2] Wrapped php-http/cache-plugin option blacklisted_paths - list of regular expressions to match paths not to be cached. --- CHANGELOG.md | 1 + src/DependencyInjection/Configuration.php | 15 +++++++++++++++ src/DependencyInjection/HttplugExtension.php | 4 ++++ tests/Resources/Fixtures/config/full.php | 1 + tests/Resources/Fixtures/config/full.xml | 1 + tests/Resources/Fixtures/config/full.yml | 2 ++ .../DependencyInjection/ConfigurationTest.php | 4 ++++ 7 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b72402ff..d3f36c22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 `blacklisted_paths` option of `php-http/cache-plugin` ### Changed diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index f2982d35..8f146d2f 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -721,6 +721,21 @@ private function createCachePluginNode() ->integerNode('default_ttl') ->info('The default max age of a Response') ->end() + ->arrayNode('blacklisted_paths') + ->info('An array of regular expression patterns for paths not to be cached. Defaults to an empty array.') + ->defaultValue([]) + ->beforeNormalization() + ->castToArray() + ->end() + ->prototype('scalar') + ->validate() + ->ifTrue(function ($v) { + return false === @preg_match('/'.$v.'/', ''); + }) + ->thenInvalid('Invalid regular expression for a blacklisted path: %s') + ->end() + ->end() + ->end() ->enumNode('hash_algo') ->info('Hashing algorithm to use') ->values(hash_algos()) diff --git a/src/DependencyInjection/HttplugExtension.php b/src/DependencyInjection/HttplugExtension.php index 61a734b5..965e3390 100644 --- a/src/DependencyInjection/HttplugExtension.php +++ b/src/DependencyInjection/HttplugExtension.php @@ -207,6 +207,10 @@ private function configurePluginByName($name, Definition $definition, array $con $options['cache_key_generator'] = new Reference($options['cache_key_generator']); } + if (empty($options['blacklisted_paths'])) { + unset($options['blacklisted_paths']); + } + $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 8c9def8a..062c0614 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -102,6 +102,7 @@ 'methods' => ['GET'], 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], + 'blacklisted_paths' => ['\/path\/not-to-be\/cached'] ], ], 'cookie' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 607438de..694e7b6d 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -57,6 +57,7 @@ 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 03c97d9a..64b7c2ea 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -73,6 +73,8 @@ httplug: cache_key_generator: null respect_response_cache_directives: - X-Foo + blacklisted_paths: + - \/path\/not-to-be\/cached cookie: cookie_jar: my_cookie_jar decoder: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index a9fd4ba4..d6a2e299 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -43,6 +43,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase 'stream_factory' => 'httplug.stream_factory', 'config' => [ 'methods' => ['GET', 'HEAD'], + 'blacklisted_paths' => [], ], ], 'cookie' => [ @@ -233,6 +234,7 @@ public function testSupportsAllConfigFormats(): void 'methods' => ['GET'], 'cache_key_generator' => null, 'respect_response_cache_directives' => ['X-Foo'], + 'blacklisted_paths' => ['\/path\/not-to-be\/cached'], ], ], 'cookie' => [ @@ -354,6 +356,7 @@ public function testCacheConfigDeprecationCompatibility(): void 'config' => [ 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => true, + 'blacklisted_paths' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); @@ -372,6 +375,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void 'config' => [ 'methods' => ['GET', 'HEAD'], 'respect_cache_headers' => false, + 'blacklisted_paths' => [], ], ]); $this->assertProcessedConfigurationEquals($config, [$file]); From 079812ab43d774f14049f2810e5da44d09b7bb17 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Thu, 26 Dec 2019 17:33:35 +0100 Subject: [PATCH 2/2] Allow any regex delimiter --- composer.json | 2 +- src/DependencyInjection/Configuration.php | 2 +- tests/Resources/Fixtures/config/full.php | 2 +- tests/Resources/Fixtures/config/full.xml | 2 +- tests/Resources/Fixtures/config/full.yml | 2 +- tests/Unit/DependencyInjection/ConfigurationTest.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 3639041f..3c5c3755 100644 --- a/composer.json +++ b/composer.json @@ -49,7 +49,7 @@ "guzzlehttp/psr7": "^1.0", "matthiasnoback/symfony-dependency-injection-test": "^3.0", "nyholm/nsa": "^1.1", - "php-http/cache-plugin": "^1.6", + "php-http/cache-plugin": "^1.7", "php-http/guzzle6-adapter": "^1.1.1 || ^2.0.1", "php-http/mock-client": "^1.2", "php-http/promise": "^1.0", diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 8f146d2f..424fac67 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -730,7 +730,7 @@ private function createCachePluginNode() ->prototype('scalar') ->validate() ->ifTrue(function ($v) { - return false === @preg_match('/'.$v.'/', ''); + return false === @preg_match($v, ''); }) ->thenInvalid('Invalid regular expression for a blacklisted path: %s') ->end() diff --git a/tests/Resources/Fixtures/config/full.php b/tests/Resources/Fixtures/config/full.php index 062c0614..c12643fb 100644 --- a/tests/Resources/Fixtures/config/full.php +++ b/tests/Resources/Fixtures/config/full.php @@ -102,7 +102,7 @@ '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@'] ], ], 'cookie' => [ diff --git a/tests/Resources/Fixtures/config/full.xml b/tests/Resources/Fixtures/config/full.xml index 694e7b6d..65fe8dd1 100644 --- a/tests/Resources/Fixtures/config/full.xml +++ b/tests/Resources/Fixtures/config/full.xml @@ -57,7 +57,7 @@ X-Foo GET - \/path\/not-to-be\/cached + @/path/not-to-be/cached@ diff --git a/tests/Resources/Fixtures/config/full.yml b/tests/Resources/Fixtures/config/full.yml index 64b7c2ea..66f9188f 100644 --- a/tests/Resources/Fixtures/config/full.yml +++ b/tests/Resources/Fixtures/config/full.yml @@ -74,7 +74,7 @@ httplug: respect_response_cache_directives: - X-Foo blacklisted_paths: - - \/path\/not-to-be\/cached + - '@/path/not-to-be/cached@' cookie: cookie_jar: my_cookie_jar decoder: diff --git a/tests/Unit/DependencyInjection/ConfigurationTest.php b/tests/Unit/DependencyInjection/ConfigurationTest.php index d6a2e299..cc593af4 100644 --- a/tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/tests/Unit/DependencyInjection/ConfigurationTest.php @@ -234,7 +234,7 @@ public function testSupportsAllConfigFormats(): void '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@'], ], ], 'cookie' => [