Skip to content

Commit f1906cf

Browse files
committed
move generate url type configuration to top and apply to invalidation listener too
1 parent 0f7e604 commit f1906cf

File tree

14 files changed

+253
-90
lines changed

14 files changed

+253
-90
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Changelog
44
3.x
55
===
66

7+
3.4
8+
---
9+
10+
* Configuration for `generate_url_type` is now on top level instead of on the `cache_manager`, and applies to the InvalidationListener too.
11+
Configuring `cache_manager.generate_url_type` is deprecated and will be removed in version 4.
12+
713
3.3
814
---
915

Resources/doc/reference/configuration.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ for the bundle.
77
.. toctree::
88
:maxdepth: 2
99

10+
configuration/general
1011
configuration/proxy-client
1112
configuration/cache-manager
1213
configuration/headers

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,5 @@ your own service that implements ``FOS\HttpCache\ProxyClient``.
3838
custom_proxy_client: acme.caching.proxy_client
3939
4040
When you specify a custom proxy client, the bundle does not know about the
41-
capabilities of the client. The ``generate_url_type`` defaults to true and
42-
:doc:`tag support <tags>` is only active if explicitly enabled.
43-
44-
``generate_url_type``
45-
---------------------
46-
47-
**type**: ``enum`` **Symfony 2 options**: ``auto`` or one of the constants in UrlGeneratorInterface
48-
49-
The ``$referenceType`` to be used when generating URLs in the ``invalidateRoute()``
50-
and ``refreshRoute()`` calls. If you use ``ABSOLUTE_PATH`` to only generate
51-
paths, you need to configure the ``base_url`` on the proxy client. When set to
52-
``auto``, the value is determined based on whether ``base_url`` is set on the
53-
default proxy client.
54-
55-
.. code-block:: yaml
56-
57-
# app/config/config.yml
58-
fos_http_cache:
59-
cache_manager:
60-
generate_url_type: !php/const Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH
41+
capabilities of the client. The ``generate_url_type`` defaults to absolute
42+
URL and :doc:`tag support <tags>` is only active if explicitly enabled.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
General Configuration
2+
=====================
3+
4+
There is one global option you can configure for this bundle.
5+
6+
``generate_url_type``
7+
---------------------
8+
9+
**type**: ``enum`` **options**: ``auto`` or one of the constants in UrlGeneratorInterface
10+
11+
The ``$referenceType`` to be used when generating URLs to invalidate or refresh
12+
from routes. If you use ``ABSOLUTE_PATH`` to only generate
13+
paths, you need to configure the ``base_url`` on the proxy client. When set to
14+
``auto``, the value is determined based on the configuration.
15+
16+
.. code-block:: yaml
17+
18+
# app/config/config.yml
19+
fos_http_cache:
20+
generate_url_type: !php/const Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_PATH

src/DependencyInjection/Configuration.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace FOS\HttpCacheBundle\DependencyInjection;
1313

14-
use DateTime;
1514
use FOS\HttpCache\ProxyClient\Varnish;
1615
use FOS\HttpCache\SymfonyCache\PurgeListener;
1716
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
@@ -194,8 +193,35 @@ function ($v) {
194193

195194
return $v;
196195
})
196+
->end()
197+
->validate()
198+
->ifTrue(
199+
function (array $v): bool {
200+
return !empty($v['cache_manager']['generate_url_type']) && 'auto' !== $v['cache_manager']['generate_url_type'] && array_key_exists('generate_url_type', $v);
201+
}
202+
)
203+
->then(function (array $v) {
204+
throw new InvalidConfigurationException('Only configure "generate_url_type" and do not set the deprecated "cache_manager.generate_url_type" option');
205+
})
206+
->end()
197207
;
198208

209+
$rootNode
210+
->children()
211+
->enumNode('generate_url_type')
212+
->values([
213+
'auto',
214+
UrlGeneratorInterface::ABSOLUTE_PATH,
215+
UrlGeneratorInterface::ABSOLUTE_URL,
216+
UrlGeneratorInterface::NETWORK_PATH,
217+
UrlGeneratorInterface::RELATIVE_PATH,
218+
])
219+
// TODO NEXT MAJOR: remove the cache_manager.generate_url_type and enable this as default
220+
// ->defaultValue('auto')
221+
->info('Set what URLs to generate on CacheManager::invalidate/refresh and InvalidationListener. Auto tries to guess the right mode based on your proxy client.')
222+
->end()
223+
->end()
224+
;
199225
$this->addCacheableResponseSection($rootNode);
200226
$this->addCacheControlSection($rootNode);
201227
$this->addProxyClientSection($rootNode);
@@ -759,6 +785,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode): void
759785
->cannotBeEmpty()
760786
->end()
761787
->enumNode('generate_url_type')
788+
->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')
762789
->values([
763790
'auto',
764791
UrlGeneratorInterface::ABSOLUTE_PATH,
@@ -767,7 +794,7 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode): void
767794
UrlGeneratorInterface::RELATIVE_PATH,
768795
])
769796
->defaultValue('auto')
770-
->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.')
797+
->info('Set what URLs to generate on invalidate/refresh Route. Auto tries to guess the right mode based on your proxy client.')
771798
->end()
772799
->end()
773800
;

src/DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,9 @@ public function load(array $configs, ContainerBuilder $container): void
8181
'fos_http_cache.default_proxy_client'
8282
);
8383
}
84-
if ('auto' === $config['cache_manager']['generate_url_type']) {
85-
if (array_key_exists('custom_proxy_client', $config['cache_manager'])) {
86-
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
87-
} else {
88-
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
89-
if ('noop' !== $defaultClient
90-
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) {
91-
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
92-
} elseif ('cloudfront' === $defaultClient) {
93-
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
94-
} else {
95-
$generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL;
96-
}
97-
}
98-
} else {
99-
$generateUrlType = $config['cache_manager']['generate_url_type'];
84+
$generateUrlType = (array_key_exists('generate_url_type', $config)) ? $config['generate_url_type'] : $config['cache_manager']['generate_url_type'];
85+
if ('auto' === $generateUrlType) {
86+
$generateUrlType = $this->determineGenerateUrlType($config);
10087
}
10188
$container->setParameter('fos_http_cache.cache_manager.generate_url_type', $generateUrlType);
10289
$loader->load('cache_manager.php');
@@ -129,6 +116,11 @@ public function load(array $configs, ContainerBuilder $container): void
129116
if (!empty($config['invalidation']['rules'])) {
130117
$this->loadInvalidatorRules($container, $config['invalidation']['rules']);
131118
}
119+
$generateUrlType = (array_key_exists('generate_url_type', $config)) ? $config['generate_url_type'] : UrlGeneratorInterface::ABSOLUTE_PATH;
120+
if ('auto' === $generateUrlType) {
121+
$generateUrlType = $this->determineGenerateUrlType($config);
122+
}
123+
$container->setParameter('fos_http_cache.invalidation.generate_url_type', $generateUrlType);
132124
}
133125

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

746738
throw new InvalidConfigurationException('No proxy client configured');
747739
}
740+
741+
private function determineGenerateUrlType(array $config): int
742+
{
743+
if (array_key_exists('cache_manager', $config) && array_key_exists('custom_proxy_client', $config['cache_manager'])) {
744+
return UrlGeneratorInterface::ABSOLUTE_URL;
745+
}
746+
747+
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
748+
if ('noop' !== $defaultClient
749+
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) {
750+
return UrlGeneratorInterface::ABSOLUTE_PATH;
751+
}
752+
if ('cloudfront' === $defaultClient) {
753+
return UrlGeneratorInterface::ABSOLUTE_PATH;
754+
}
755+
756+
return UrlGeneratorInterface::ABSOLUTE_URL;
757+
}
748758
}

src/EventListener/InvalidationListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function __construct(
4040
private readonly UrlGeneratorInterface $urlGenerator,
4141
private readonly RuleMatcherInterface $mustInvalidateRule,
4242
private ?ExpressionLanguage $expressionLanguage = null,
43+
private readonly int $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH,
4344
) {
4445
}
4546

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

128129
$requestParams = $request->attributes->get('_route_params');
129130
foreach ($invalidatorConfigs as $route => $config) {
130-
$path = $this->urlGenerator->generate($route, $requestParams);
131+
$path = $this->urlGenerator->generate($route, $requestParams, $this->generateUrlType);
131132
// If extra route parameters should be ignored, strip the query
132133
// string generated by the Symfony router from the path
133134
if (isset($config['ignore_extra_params'])

src/Resources/config/cache_manager.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
return static function (ContainerConfigurator $container) {
66
$services = $container->services();
7-
$parameters = $container->parameters();
87

98
$services->set('fos_http_cache.cache_manager', \FOS\HttpCacheBundle\CacheManager::class)
109
->public()

src/Resources/config/invalidation_listener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
return static function (ContainerConfigurator $container) {
66
$services = $container->services();
7-
$parameters = $container->parameters();
87

98
$services->set('fos_http_cache.event_listener.invalidation', \FOS\HttpCacheBundle\EventListener\InvalidationListener::class)
109
->args([
1110
service('fos_http_cache.cache_manager'),
1211
service('router'),
1312
service('fos_http_cache.rule_matcher.must_invalidate'),
1413
service('fos_http_cache.invalidation.expression_language')->ignoreOnInvalid(),
14+
'%fos_http_cache.invalidation.generate_url_type%',
1515
])
1616
->tag('kernel.event_subscriber');
1717
};

tests/Resources/Fixtures/config/full.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
* file that was distributed with this source code.
1010
*/
1111

12+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13+
1214
$container->loadFromExtension('fos_http_cache', [
15+
'generate_url_type' => UrlGeneratorInterface::ABSOLUTE_URL,
1316
'cacheable' => [
1417
'response' => [
1518
'additional_status' => [100, 500],

0 commit comments

Comments
 (0)