Skip to content

Commit

Permalink
Merge pull request #312 from FriendsOfSymfony/handle-only-custom-client
Browse files Browse the repository at this point in the history
[1.3] handle configuration where we only have a custom proxy client and no proxies configured
  • Loading branch information
dbu committed Sep 6, 2016
2 parents 5ffcbf5 + 63de3a3 commit 0424781
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode)
->info('Allows to disable the invalidation manager. Enabled by default if you configure a proxy client.')
->end()
->scalarNode('custom_proxy_client')
->info('Service name of a custom proxy client to use. If you configure a proxy client, that client will be used by default.')
->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.')
->end()
->enumNode('generate_url_type')
->values(array(
Expand Down
29 changes: 21 additions & 8 deletions DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,23 @@ public function load(array $configs, ContainerBuilder $container)
}

if ($config['cache_manager']['enabled']) {
if (!empty($config['cache_manager']['custom_proxy_client'])) {
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
// overwrite the previously set alias, if a proxy client was also configured
$container->setAlias(
$this->getAlias().'.default_proxy_client',
$config['cache_manager']['custom_proxy_client']
);
}
if ('auto' === $config['cache_manager']['generate_url_type']) {
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
$generateUrlType = empty($config['cache_manager']['custom_proxy_client']) && isset($config['proxy_client'][$defaultClient]['base_url'])
? UrlGeneratorInterface::ABSOLUTE_PATH
: UrlGeneratorInterface::ABSOLUTE_URL
;
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
} else {
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
$generateUrlType = array_key_exists('base_url', $config['proxy_client'][$defaultClient])
? UrlGeneratorInterface::ABSOLUTE_PATH
: UrlGeneratorInterface::ABSOLUTE_URL
;
}
} else {
$generateUrlType = $config['cache_manager']['generate_url_type'];
}
Expand All @@ -89,7 +93,9 @@ public function load(array $configs, ContainerBuilder $container)
$container,
$loader,
$config['tags'],
$this->getDefaultProxyClient($config['proxy_client'])
array_key_exists('proxy_client', $config)
? $this->getDefaultProxyClient($config['proxy_client'])
: 'custom'
);
} else {
$container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false);
Expand Down Expand Up @@ -313,14 +319,21 @@ private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader,
}
}

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

return;
}
if ('varnish' !== $client) {
if (!in_array($client, array('varnish', 'custom'))) {
throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with %s', $client));
}

Expand Down
14 changes: 9 additions & 5 deletions Resources/doc/reference/configuration/cache-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,17 @@ your own service that implements ``FOS\HttpCache\ProxyClientInterface``.
cache_manager:
custom_proxy_client: acme.caching.proxy_client
When you specify a custom proxy client, the bundle does not know about the
capabilities of the client. The ``generate_url_type`` defaults to true and
:doc:`tag support <tags>` is only active if explicitly enabled.

``generate_url_type``
---------------------

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

The ``$referenceType`` to be used when generating URLs in the ``invalidateRoute()``
and ``refreshRoute()`` calls. True results in absolute URLs including the current domain,
``false`` generates a path without domain, needing a ``base_url`` to be configured
on the proxy client. When set to ``auto``, the value is determined based on ``base_url``
of the default proxy client.
and ``refreshRoute()`` calls. If you use ``ABSOLUTE_PATH`` to only generate
paths, you need to configure the ``base_url`` on the proxy client. When set to
``auto``, the value is determined based on whether ``base_url`` is set on the
default proxy client.
19 changes: 19 additions & 0 deletions Tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ public function testConfigLoadSymfony()
$this->assertFalse($container->hasDefinition('fos_http_cache.handler.tag_handler'));
}

public function testConfigCustomClient()
{
$container = $this->createContainer();
$this->extension->load(array(
array(
'cache_manager' => array(
'custom_proxy_client' => 'app.cache.client',
),
),
), $container);

$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.varnish'));
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.nginx'));
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.symfony'));
$this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client'));
$this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation'));
$this->assertFalse($container->hasDefinition('fos_http_cache.handler.tag_handler'));
}

public function testEmptyConfig()
{
$config = array();
Expand Down

0 comments on commit 0424781

Please sign in to comment.