Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Changelog
3.x
===

3.4
---

* Configuration for `generate_url_type` is now on top level instead of on the `cache_manager`, and applies to the InvalidationListener too.
Configuring `cache_manager.generate_url_type` is deprecated and will be removed in version 4.

3.3
---

Expand Down
1 change: 1 addition & 0 deletions Resources/doc/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ for the bundle.
.. toctree::
:maxdepth: 2

configuration/general
configuration/proxy-client
configuration/cache-manager
configuration/headers
Expand Down
22 changes: 2 additions & 20 deletions Resources/doc/reference/configuration/cache-manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,5 @@ your own service that implements ``FOS\HttpCache\ProxyClient``.
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`` **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. 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.

.. code-block:: yaml

# app/config/config.yml
fos_http_cache:
cache_manager:
generate_url_type: !php/const Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH
capabilities of the client. The ``generate_url_type`` defaults to absolute
URL and :doc:`tag support <tags>` is only active if explicitly enabled.
20 changes: 20 additions & 0 deletions Resources/doc/reference/configuration/general.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
General Configuration
=====================

There is one global option you can configure for this bundle.

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

**type**: ``enum`` **options**: ``auto`` or one of the constants in UrlGeneratorInterface

The ``$referenceType`` to be used when generating URLs to invalidate or refresh
from routes. 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 the configuration.

.. code-block:: yaml

# app/config/config.yml
fos_http_cache:
generate_url_type: !php/const Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH
31 changes: 29 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace FOS\HttpCacheBundle\DependencyInjection;

use DateTime;
use FOS\HttpCache\ProxyClient\Varnish;
use FOS\HttpCache\SymfonyCache\PurgeListener;
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
Expand Down Expand Up @@ -194,8 +193,35 @@ function ($v) {

return $v;
})
->end()
->validate()
->ifTrue(
function (array $v): bool {
return !empty($v['cache_manager']['generate_url_type']) && 'auto' !== $v['cache_manager']['generate_url_type'] && array_key_exists('generate_url_type', $v);
}
)
->then(function (array $v) {
throw new InvalidConfigurationException('Only configure "generate_url_type" and do not set the deprecated "cache_manager.generate_url_type" option');
})
->end()
;

$rootNode
->children()
->enumNode('generate_url_type')
->values([
'auto',
UrlGeneratorInterface::ABSOLUTE_PATH,
UrlGeneratorInterface::ABSOLUTE_URL,
UrlGeneratorInterface::NETWORK_PATH,
UrlGeneratorInterface::RELATIVE_PATH,
])
// TODO NEXT MAJOR: remove the cache_manager.generate_url_type and enable this as default
// ->defaultValue('auto')
->info('Set what URLs to generate on CacheManager::invalidate/refresh and InvalidationListener. Auto tries to guess the right mode based on your proxy client.')
->end()
->end()
;
$this->addCacheableResponseSection($rootNode);
$this->addCacheControlSection($rootNode);
$this->addProxyClientSection($rootNode);
Expand Down Expand Up @@ -759,6 +785,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode): void
->cannotBeEmpty()
->end()
->enumNode('generate_url_type')
->setDeprecated('friends-of-symfony/http-cache-bundle', '3.4', 'Configure the url type on top level to also have it apply to the InvalidationListener in addition to the CacheManager')
->values([
'auto',
UrlGeneratorInterface::ABSOLUTE_PATH,
Expand All @@ -767,7 +794,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode): void
UrlGeneratorInterface::RELATIVE_PATH,
])
->defaultValue('auto')
->info('Set what URLs to generate on invalidate/refresh Route. Auto means path if base_url is set on the default proxy client, full URL otherwise.')
->info('Set what URLs to generate on invalidate/refresh Route. Auto tries to guess the right mode based on your proxy client.')
->end()
->end()
;
Expand Down
42 changes: 26 additions & 16 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,9 @@ public function load(array $configs, ContainerBuilder $container): void
'fos_http_cache.default_proxy_client'
);
}
if ('auto' === $config['cache_manager']['generate_url_type']) {
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
} else {
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
if ('noop' !== $defaultClient
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) {
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
} elseif ('cloudfront' === $defaultClient) {
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
} else {
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
}
}
} else {
$generateUrlType = $config['cache_manager']['generate_url_type'];
$generateUrlType = (array_key_exists('generate_url_type', $config)) ? $config['generate_url_type'] : $config['cache_manager']['generate_url_type'];
if ('auto' === $generateUrlType) {
$generateUrlType = $this->determineGenerateUrlType($config);
}
$container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType);
$loader->load('cache_manager.php');
Expand Down Expand Up @@ -129,6 +116,11 @@ public function load(array $configs, ContainerBuilder $container): void
if (!empty($config['invalidation']['rules'])) {
$this->loadInvalidatorRules($container, $config['invalidation']['rules']);
}
$generateUrlType = (array_key_exists('generate_url_type', $config)) ? $config['generate_url_type'] : UrlGeneratorInterface::ABSOLUTE_PATH;
if ('auto' === $generateUrlType) {
$generateUrlType = $this->determineGenerateUrlType($config);
}
$container->setParameter('fos_http_cache.invalidation.generate_url_type', $generateUrlType);
}

if ($config['user_context']['enabled']) {
Expand Down Expand Up @@ -745,4 +737,22 @@ private function getDefaultProxyClient(array $config): string

throw new InvalidConfigurationException('No proxy client configured');
}

private function determineGenerateUrlType(array $config): int
{
if (array_key_exists('cache_manager', $config) && array_key_exists('custom_proxy_client', $config['cache_manager'])) {
return UrlGeneratorInterface::ABSOLUTE_URL;
}

$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
if ('noop' !== $defaultClient
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) {
return UrlGeneratorInterface::ABSOLUTE_PATH;
}
if ('cloudfront' === $defaultClient) {
return UrlGeneratorInterface::ABSOLUTE_PATH;
}

return UrlGeneratorInterface::ABSOLUTE_URL;
}
}
3 changes: 2 additions & 1 deletion src/EventListener/InvalidationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly RuleMatcherInterface $mustInvalidateRule,
private ?ExpressionLanguage $expressionLanguage = null,
private readonly int $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH,
) {
}

Expand Down Expand Up @@ -127,7 +128,7 @@ private function handleInvalidation(Request $request, Response $response): void

$requestParams = $request->attributes->get('_route_params');
foreach ($invalidatorConfigs as $route => $config) {
$path = $this->urlGenerator->generate($route, $requestParams);
$path = $this->urlGenerator->generate($route, $requestParams, $this->generateUrlType);
// If extra route parameters should be ignored, strip the query
// string generated by the Symfony router from the path
if (isset($config['ignore_extra_params'])
Expand Down
1 change: 0 additions & 1 deletion src/Resources/config/cache_manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

return static function (ContainerConfigurator $container) {
$services = $container->services();
$parameters = $container->parameters();

$services->set('fos_http_cache.cache_manager', \FOS\HttpCacheBundle\CacheManager::class)
->public()
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/config/invalidation_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

return static function (ContainerConfigurator $container) {
$services = $container->services();
$parameters = $container->parameters();

$services->set('fos_http_cache.event_listener.invalidation', \FOS\HttpCacheBundle\EventListener\InvalidationListener::class)
->args([
service('fos_http_cache.cache_manager'),
service('router'),
service('fos_http_cache.rule_matcher.must_invalidate'),
service('fos_http_cache.invalidation.expression_language')->ignoreOnInvalid(),
'%fos_http_cache.invalidation.generate_url_type%',
])
->tag('kernel.event_subscriber');
};
3 changes: 3 additions & 0 deletions tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
* file that was distributed with this source code.
*/

use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

$container->loadFromExtension('fos_http_cache', [
'generate_url_type' => UrlGeneratorInterface::ABSOLUTE_URL,
'cacheable' => [
'response' => [
'additional_status' => [100, 500],
Expand Down
4 changes: 3 additions & 1 deletion tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services">

<config xmlns="http://example.org/schema/dic/fos_http_cache">
<config xmlns="http://example.org/schema/dic/fos_http_cache"
generate_url_type="0"
>
<cacheable>
<response>
<additional-status>100</additional-status>
Expand Down
1 change: 1 addition & 0 deletions tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fos_http_cache:
generate_url_type: 0

cacheable:
response:
Expand Down
20 changes: 19 additions & 1 deletion tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class ConfigurationTest extends AbstractExtensionConfigurationTestCase
{
Expand Down Expand Up @@ -57,6 +58,7 @@ public function testEmptyConfiguration(): void
public function testSupportsAllConfigFormats(): void
{
$expectedConfiguration = [
'generate_url_type' => UrlGeneratorInterface::ABSOLUTE_URL,
'cacheable' => [
'response' => [
'additional_status' => [100, 500],
Expand Down Expand Up @@ -120,7 +122,7 @@ public function testSupportsAllConfigFormats(): void
'cache_manager' => [
'enabled' => true,
'custom_proxy_client' => 'acme.proxy_client',
'generate_url_type' => 'auto',
'generate_url_type' => 'auto', // this is ignored by the extension when the top level value is set
],
'tags' => [
'enabled' => 'auto',
Expand Down Expand Up @@ -463,6 +465,22 @@ public function testEmptyServerConfigurationIsNotAllowed(): void
(new Processor())->processConfiguration($configuration, ['fos_http_cache' => $params]);
}

public function testConfiguringGenerateUrlTwiceNotAllowed(): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('Only configure "generate_url_type" and do not set the deprecated "cache_manager.generate_url_type" option');

$params = $this->getEmptyConfig();
$params['generate_url_type'] = 'auto';
$params['cache_manager'] = [
'custom_proxy_client' => 'noop',
'generate_url_type' => UrlGeneratorInterface::ABSOLUTE_PATH,
];

$configuration = new Configuration(false);
(new Processor())->processConfiguration($configuration, ['fos_http_cache' => $params]);
}

public function testDefaultIsNotConsideredAsServerConfig(): void
{
$params = $this->getEmptyConfig();
Expand Down
Loading
Loading