Skip to content

Commit 79070d6

Browse files
alcoholNyholm
authored andcommitted
expand configuration definition and adjust tests (#155)
1 parent d8a85ac commit 79070d6

File tree

5 files changed

+139
-71
lines changed

5 files changed

+139
-71
lines changed

DependencyInjection/Configuration.php

Lines changed: 119 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
namespace Http\HttplugBundle\DependencyInjection;
44

5+
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator;
6+
use Http\Client\Common\Plugin\Journal;
7+
use Http\Message\CookieJar;
8+
use Http\Message\Formatter;
9+
use Http\Message\StreamFactory;
10+
use Psr\Cache\CacheItemPoolInterface;
11+
use Psr\Log\LoggerInterface;
512
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
613
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
714
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
@@ -352,69 +359,33 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
352359
$children = $pluginNode->children();
353360

354361
$children->append($this->createAuthenticationPluginNode());
362+
$children->append($this->createCachePluginNode());
355363

356-
$children->arrayNode('cache')
357-
->canBeEnabled()
358-
->addDefaultsIfNotSet()
364+
$children
365+
->arrayNode('cookie')
366+
->canBeEnabled()
359367
->children()
360-
->scalarNode('cache_pool')
361-
->info('This must be a service id to a service implementing Psr\Cache\CacheItemPoolInterface')
368+
->scalarNode('cookie_jar')
369+
->info('This must be a service id to a service implementing '.CookieJar::class)
362370
->isRequired()
363371
->cannotBeEmpty()
364372
->end()
365-
->scalarNode('stream_factory')
366-
->info('This must be a service id to a service implementing Http\Message\StreamFactory')
367-
->defaultValue('httplug.stream_factory')
368-
->cannotBeEmpty()
369-
->end()
370-
->arrayNode('config')
371-
->addDefaultsIfNotSet()
372-
->validate()
373-
->ifTrue(function ($config) {
374-
// Cannot set both respect_cache_headers and respect_response_cache_directives
375-
return isset($config['respect_cache_headers'], $config['respect_response_cache_directives']);
376-
})
377-
->thenInvalid('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead.')
378-
->end()
379-
->children()
380-
->scalarNode('default_ttl')->defaultValue(0)->end()
381-
->enumNode('respect_cache_headers')
382-
->values([null, true, false])
383-
->beforeNormalization()
384-
->always(function ($v) {
385-
@trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED);
386-
387-
return $v;
388-
})
389-
->end()
390-
->end()
391-
->variableNode('respect_response_cache_directives')
392-
->validate()
393-
->always(function ($v) {
394-
if (is_null($v) || is_array($v)) {
395-
return $v;
396-
}
397-
throw new InvalidTypeException();
398-
})
399-
->end()
400-
->end()
401-
->end()
402-
->end()
403373
->end()
404374
->end();
405-
// End cache plugin
375+
// End cookie plugin
406376

407-
$children->arrayNode('cookie')
408-
->canBeEnabled()
377+
$children
378+
->arrayNode('history')
379+
->canBeEnabled()
409380
->children()
410-
->scalarNode('cookie_jar')
411-
->info('This must be a service id to a service implementing Http\Message\CookieJar')
381+
->scalarNode('journal')
382+
->info('This must be a service id to a service implementing '.Journal::class)
412383
->isRequired()
413384
->cannotBeEmpty()
414385
->end()
415386
->end()
416387
->end();
417-
// End cookie plugin
388+
// End history plugin
418389

419390
$decoder = $children->arrayNode('decoder');
420391
$disableAll ? $decoder->canBeEnabled() : $decoder->canBeDisabled();
@@ -425,29 +396,17 @@ private function addSharedPluginNodes(ArrayNodeDefinition $pluginNode, $disableA
425396
->end();
426397
// End decoder plugin
427398

428-
$children->arrayNode('history')
429-
->canBeEnabled()
430-
->children()
431-
->scalarNode('journal')
432-
->info('This must be a service id to a service implementing Http\Client\Plugin\Journal')
433-
->isRequired()
434-
->cannotBeEmpty()
435-
->end()
436-
->end()
437-
->end();
438-
// End history plugin
439-
440399
$logger = $children->arrayNode('logger');
441400
$disableAll ? $logger->canBeEnabled() : $logger->canBeDisabled();
442401
$logger->addDefaultsIfNotSet()
443402
->children()
444403
->scalarNode('logger')
445-
->info('This must be a service id to a service implementing Psr\Log\LoggerInterface')
404+
->info('This must be a service id to a service implementing '.LoggerInterface::class)
446405
->defaultValue('logger')
447406
->cannotBeEmpty()
448407
->end()
449408
->scalarNode('formatter')
450-
->info('This must be a service id to a service implementing Http\Message\Formatter')
409+
->info('This must be a service id to a service implementing '.Formatter::class)
451410
->defaultNull()
452411
->end()
453412
->end()
@@ -564,4 +523,101 @@ private function validateAuthenticationType(array $expected, array $actual, $aut
564523
implode(', ', $actual)
565524
));
566525
}
526+
527+
/**
528+
* Create configuration for cache plugin.
529+
*
530+
* @return NodeDefinition Definition for the cache node in the plugins list.
531+
*/
532+
private function createCachePluginNode()
533+
{
534+
$builder = new TreeBuilder();
535+
536+
$config = $builder->root('config');
537+
$config
538+
->fixXmlConfig('method')
539+
->fixXmlConfig('respect_response_cache_directive')
540+
->addDefaultsIfNotSet()
541+
->validate()
542+
->ifTrue(function ($config) {
543+
// Cannot set both respect_cache_headers and respect_response_cache_directives
544+
return isset($config['respect_cache_headers'], $config['respect_response_cache_directives']);
545+
})
546+
->thenInvalid('You can\'t provide config option "respect_cache_headers" and "respect_response_cache_directives" simultaniously. Use "respect_response_cache_directives" instead.')
547+
->end()
548+
->children()
549+
->scalarNode('cache_key_generator')
550+
->info('This must be a service id to a service implementing '.CacheKeyGenerator::class)
551+
->end()
552+
->integerNode('cache_lifetime')
553+
->info('The minimum time we should store a cache item')
554+
->end()
555+
->integerNode('default_ttl')
556+
->info('The default max age of a Response')
557+
->end()
558+
->enumNode('hash_algo')
559+
->info('Hashing algorithm to use')
560+
->values(hash_algos())
561+
->cannotBeEmpty()
562+
->end()
563+
->arrayNode('methods')
564+
->info('Which request methods to cache')
565+
->defaultValue(['GET', 'HEAD'])
566+
->prototype('scalar')
567+
->validate()
568+
->ifTrue(function ($v) {
569+
/* RFC7230 sections 3.1.1 and 3.2.6 except limited to uppercase characters. */
570+
return preg_match('/[^A-Z0-9!#$%&\'*+\-.^_`|~]+/', $v);
571+
})
572+
->thenInvalid('Invalid method: %s')
573+
->end()
574+
->end()
575+
->end()
576+
->enumNode('respect_cache_headers')
577+
->info('Whether we should care about cache headers or not [DEPRECATED]')
578+
->values([null, true, false])
579+
->beforeNormalization()
580+
->always(function ($v) {
581+
@trigger_error('The option "respect_cache_headers" is deprecated since version 1.3 and will be removed in 2.0. Use "respect_response_cache_directives" instead.', E_USER_DEPRECATED);
582+
583+
return $v;
584+
})
585+
->end()
586+
->end()
587+
->variableNode('respect_response_cache_directives')
588+
->info('A list of cache directives to respect when caching responses')
589+
->validate()
590+
->always(function ($v) {
591+
if (is_null($v) || is_array($v)) {
592+
return $v;
593+
}
594+
595+
throw new InvalidTypeException();
596+
})
597+
->end()
598+
->end()
599+
->end()
600+
;
601+
602+
$cache = $builder->root('cache');
603+
$cache
604+
->canBeEnabled()
605+
->addDefaultsIfNotSet()
606+
->children()
607+
->scalarNode('cache_pool')
608+
->info('This must be a service id to a service implementing '.CacheItemPoolInterface::class)
609+
->isRequired()
610+
->cannotBeEmpty()
611+
->end()
612+
->scalarNode('stream_factory')
613+
->info('This must be a service id to a service implementing '.StreamFactory::class)
614+
->defaultValue('httplug.stream_factory')
615+
->cannotBeEmpty()
616+
->end()
617+
->end()
618+
->append($config)
619+
;
620+
621+
return $cache;
622+
}
567623
}

Tests/Resources/Fixtures/config/full.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,12 @@
8080
'cache_pool' => 'my_cache_pool',
8181
'stream_factory' => 'my_other_stream_factory',
8282
'config' => [
83+
'cache_lifetime' => 2592000,
8384
'default_ttl' => 42,
84-
'respect_response_cache_directives' => ['X-Foo', 'X-Bar'],
85+
'hash_algo' => 'sha1',
86+
'methods' => ['GET'],
87+
'cache_key_generator' => null,
88+
'respect_response_cache_directives' => ['X-Foo'],
8589
],
8690
],
8791
'cookie' => [

Tests/Resources/Fixtures/config/full.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
<my_service type="service" service="my_auth_service"/>
4545
</authentication>
4646
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
47-
<config default-ttl="42">
48-
<respect-response-cache-directives>X-Foo</respect-response-cache-directives>
49-
<respect-response-cache-directives>X-Bar</respect-response-cache-directives>
47+
<config default-ttl="42" cache-lifetime="2592000" hash-algo="sha1" cache-key-generator="null">
48+
<respect-response-cache-directive>X-Foo</respect-response-cache-directive>
49+
<method>GET</method>
5050
</config>
5151
</cache>
5252
<cookie cookie-jar="my_cookie_jar"/>

Tests/Resources/Fixtures/config/full.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ httplug:
5555
cache_pool: my_cache_pool
5656
stream_factory: my_other_stream_factory
5757
config:
58+
cache_lifetime: 2592000
5859
default_ttl: 42
60+
hash_algo: sha1
61+
methods:
62+
- GET
63+
cache_key_generator: null
5964
respect_response_cache_directives:
6065
- X-Foo
61-
- X-Bar
6266
cookie:
6367
cookie_jar: my_cookie_jar
6468
decoder:

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ConfigurationTest extends AbstractExtensionConfigurationTestCase
3636
'enabled' => false,
3737
'stream_factory' => 'httplug.stream_factory',
3838
'config' => [
39-
'default_ttl' => 0,
39+
'methods' => ['GET', 'HEAD'],
4040
],
4141
],
4242
'cookie' => [
@@ -194,8 +194,12 @@ public function testSupportsAllConfigFormats()
194194
'cache_pool' => 'my_cache_pool',
195195
'stream_factory' => 'my_other_stream_factory',
196196
'config' => [
197+
'cache_lifetime' => 2592000,
197198
'default_ttl' => 42,
198-
'respect_response_cache_directives' => ['X-Foo', 'X-Bar'],
199+
'hash_algo' => 'sha1',
200+
'methods' => ['GET'],
201+
'cache_key_generator' => null,
202+
'respect_response_cache_directives' => ['X-Foo'],
199203
],
200204
],
201205
'cookie' => [
@@ -317,7 +321,7 @@ public function testCacheConfigDeprecationCompatibility()
317321
'enabled' => true,
318322
'cache_pool' => 'my_cache_pool',
319323
'config' => [
320-
'default_ttl' => 0,
324+
'methods' => ['GET', 'HEAD'],
321325
'respect_cache_headers' => true,
322326
],
323327
]);

0 commit comments

Comments
 (0)