Skip to content

Commit 0424781

Browse files
authored
Merge pull request #312 from FriendsOfSymfony/handle-only-custom-client
[1.3] handle configuration where we only have a custom proxy client and no proxies configured
2 parents 5ffcbf5 + 63de3a3 commit 0424781

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode)
507507
->info('Allows to disable the invalidation manager. Enabled by default if you configure a proxy client.')
508508
->end()
509509
->scalarNode('custom_proxy_client')
510-
->info('Service name of a custom proxy client to use. If you configure a proxy client, that client will be used by default.')
510+
->info('Service name of a custom proxy client to use. With a custom client, generate_url_type defaults to ABSOLUTE_URL and tag support needs to be explicitly enabled. If no custom proxy client is specified, the first proxy client you configured is used.')
511511
->end()
512512
->enumNode('generate_url_type')
513513
->values(array(

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,23 @@ public function load(array $configs, ContainerBuilder $container)
6464
}
6565

6666
if ($config['cache_manager']['enabled']) {
67-
if (!empty($config['cache_manager']['custom_proxy_client'])) {
67+
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
6868
// overwrite the previously set alias, if a proxy client was also configured
6969
$container->setAlias(
7070
$this->getAlias().'.default_proxy_client',
7171
$config['cache_manager']['custom_proxy_client']
7272
);
7373
}
7474
if ('auto' === $config['cache_manager']['generate_url_type']) {
75-
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
76-
$generateUrlType = empty($config['cache_manager']['custom_proxy_client']) && isset($config['proxy_client'][$defaultClient]['base_url'])
77-
? UrlGeneratorInterface::ABSOLUTE_PATH
78-
: UrlGeneratorInterface::ABSOLUTE_URL
79-
;
75+
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
76+
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
77+
} else {
78+
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
79+
$generateUrlType = array_key_exists('base_url', $config['proxy_client'][$defaultClient])
80+
? UrlGeneratorInterface::ABSOLUTE_PATH
81+
: UrlGeneratorInterface::ABSOLUTE_URL
82+
;
83+
}
8084
} else {
8185
$generateUrlType = $config['cache_manager']['generate_url_type'];
8286
}
@@ -89,7 +93,9 @@ public function load(array $configs, ContainerBuilder $container)
8993
$container,
9094
$loader,
9195
$config['tags'],
92-
$this->getDefaultProxyClient($config['proxy_client'])
96+
array_key_exists('proxy_client', $config)
97+
? $this->getDefaultProxyClient($config['proxy_client'])
98+
: 'custom'
9399
);
94100
} else {
95101
$container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false);
@@ -313,14 +319,21 @@ private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader,
313319
}
314320
}
315321

322+
/**
323+
* @param ContainerBuilder $container
324+
* @param XmlFileLoader $loader
325+
* @param array $config Configuration section for the tags node
326+
* @param string $client Name of the client used with the cache manager,
327+
* "custom" when a custom client is used
328+
*/
316329
private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client)
317330
{
318331
if ('auto' === $config['enabled'] && 'varnish' !== $client) {
319332
$container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false);
320333

321334
return;
322335
}
323-
if ('varnish' !== $client) {
336+
if (!in_array($client, array('varnish', 'custom'))) {
324337
throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with %s', $client));
325338
}
326339

Resources/doc/reference/configuration/cache-manager.rst

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ your own service that implements ``FOS\HttpCache\ProxyClientInterface``.
3939
cache_manager:
4040
custom_proxy_client: acme.caching.proxy_client
4141
42+
When you specify a custom proxy client, the bundle does not know about the
43+
capabilities of the client. The ``generate_url_type`` defaults to true and
44+
:doc:`tag support <tags>` is only active if explicitly enabled.
45+
4246
``generate_url_type``
4347
---------------------
4448

45-
**type**: ``enum`` **options**: ``auto``, ``true``, ``false``, ``relative``, ``network``
49+
**type**: ``enum`` **Symfony 2 options**: ``auto`` or one of the constants in UrlGeneratorInterface
4650

4751
The ``$referenceType`` to be used when generating URLs in the ``invalidateRoute()``
48-
and ``refreshRoute()`` calls. True results in absolute URLs including the current domain,
49-
``false`` generates a path without domain, needing a ``base_url`` to be configured
50-
on the proxy client. When set to ``auto``, the value is determined based on ``base_url``
51-
of the default proxy client.
52+
and ``refreshRoute()`` calls. If you use ``ABSOLUTE_PATH`` to only generate
53+
paths, you need to configure the ``base_url`` on the proxy client. When set to
54+
``auto``, the value is determined based on whether ``base_url`` is set on the
55+
default proxy client.

Tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ public function testConfigLoadSymfony()
119119
$this->assertFalse($container->hasDefinition('fos_http_cache.handler.tag_handler'));
120120
}
121121

122+
public function testConfigCustomClient()
123+
{
124+
$container = $this->createContainer();
125+
$this->extension->load(array(
126+
array(
127+
'cache_manager' => array(
128+
'custom_proxy_client' => 'app.cache.client',
129+
),
130+
),
131+
), $container);
132+
133+
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.varnish'));
134+
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.nginx'));
135+
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.symfony'));
136+
$this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client'));
137+
$this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation'));
138+
$this->assertFalse($container->hasDefinition('fos_http_cache.handler.tag_handler'));
139+
}
140+
122141
public function testEmptyConfig()
123142
{
124143
$config = array();

0 commit comments

Comments
 (0)