Skip to content

Commit f83ab03

Browse files
Wrapped php-http/cache-plugin option blacklisted_paths (#362)
* Wrapped php-http/cache-plugin option blacklisted_paths - list of regular expressions to match paths not to be cached. * Allow any regex delimiter Co-authored-by: Tobias Nyholm <tobias.nyholm@gmail.com>
1 parent b579f1b commit f83ab03

File tree

8 files changed

+29
-1
lines changed

8 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
1010
- Configured clients are now tagged with `'httplug.client'`
1111
- Adds a link to profiler page when response is from a Symfony application with
1212
profiler enabled
13+
- Adding `blacklisted_paths` option of `php-http/cache-plugin`
1314

1415
### Changed
1516

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"guzzlehttp/psr7": "^1.0",
5050
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
5151
"nyholm/nsa": "^1.1",
52-
"php-http/cache-plugin": "^1.6",
52+
"php-http/cache-plugin": "^1.7",
5353
"php-http/guzzle6-adapter": "^1.1.1 || ^2.0.1",
5454
"php-http/mock-client": "^1.2",
5555
"php-http/promise": "^1.0",

src/DependencyInjection/Configuration.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,21 @@ private function createCachePluginNode()
721721
->integerNode('default_ttl')
722722
->info('The default max age of a Response')
723723
->end()
724+
->arrayNode('blacklisted_paths')
725+
->info('An array of regular expression patterns for paths not to be cached. Defaults to an empty array.')
726+
->defaultValue([])
727+
->beforeNormalization()
728+
->castToArray()
729+
->end()
730+
->prototype('scalar')
731+
->validate()
732+
->ifTrue(function ($v) {
733+
return false === @preg_match($v, '');
734+
})
735+
->thenInvalid('Invalid regular expression for a blacklisted path: %s')
736+
->end()
737+
->end()
738+
->end()
724739
->enumNode('hash_algo')
725740
->info('Hashing algorithm to use')
726741
->values(hash_algos())

src/DependencyInjection/HttplugExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ private function configurePluginByName($name, Definition $definition, array $con
207207
$options['cache_key_generator'] = new Reference($options['cache_key_generator']);
208208
}
209209

210+
if (empty($options['blacklisted_paths'])) {
211+
unset($options['blacklisted_paths']);
212+
}
213+
210214
$definition
211215
->replaceArgument(0, new Reference($config['cache_pool']))
212216
->replaceArgument(1, new Reference($config['stream_factory']))

tests/Resources/Fixtures/config/full.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
'methods' => ['GET'],
103103
'cache_key_generator' => null,
104104
'respect_response_cache_directives' => ['X-Foo'],
105+
'blacklisted_paths' => ['@/path/not-to-be/cached@']
105106
],
106107
],
107108
'cookie' => [

tests/Resources/Fixtures/config/full.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null">
5858
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
5959
<method>GET</method>
60+
<blacklisted-paths>@/path/not-to-be/cached@</blacklisted-paths>
6061
</config>
6162
</cache>
6263
<cookie cookie-jar="my_cookie_jar"/>

tests/Resources/Fixtures/config/full.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ httplug:
7373
cache_key_generator: null
7474
respect_response_cache_directives:
7575
- X-Foo
76+
blacklisted_paths:
77+
- '@/path/not-to-be/cached@'
7678
cookie:
7779
cookie_jar: my_cookie_jar
7880
decoder:

tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
4545
'stream_factory' => 'httplug.stream_factory',
4646
'config' => [
4747
'methods' => ['GET', 'HEAD'],
48+
'blacklisted_paths' => [],
4849
],
4950
],
5051
'cookie' => [
@@ -235,6 +236,7 @@ public function testSupportsAllConfigFormats(): void
235236
'methods' => ['GET'],
236237
'cache_key_generator' => null,
237238
'respect_response_cache_directives' => ['X-Foo'],
239+
'blacklisted_paths' => ['@/path/not-to-be/cached@'],
238240
],
239241
],
240242
'cookie' => [
@@ -356,6 +358,7 @@ public function testCacheConfigDeprecationCompatibility(): void
356358
'config' => [
357359
'methods' => ['GET', 'HEAD'],
358360
'respect_cache_headers' => true,
361+
'blacklisted_paths' => [],
359362
],
360363
]);
361364
$this->assertProcessedConfigurationEquals($config, [$file]);
@@ -374,6 +377,7 @@ public function testCacheConfigDeprecationCompatibilityIssue166(): void
374377
'config' => [
375378
'methods' => ['GET', 'HEAD'],
376379
'respect_cache_headers' => false,
380+
'blacklisted_paths' => [],
377381
],
378382
]);
379383
$this->assertProcessedConfigurationEquals($config, [$file]);

0 commit comments

Comments
 (0)