Skip to content

Commit db5d4b3

Browse files
committed
Merge pull request #209 from FriendsOfSymfony/symfony-cache-purge-refresh
load purge and refresh handlers by default
2 parents 227ac2d + 4b004e6 commit db5d4b3

File tree

16 files changed

+200
-37
lines changed

16 files changed

+200
-37
lines changed

CHANGELOG.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ Changelog
44
1.3.0
55
-----
66

7-
* Configured/annotated cache tags on subrequests (in twig: `render(controller())`)
7+
* Added configuration for Symfony HttpCache client and HttpCache now loads
8+
purge and refresh handlers by default.
9+
* Configured/annotated cache tags on subrequests (in Twig: `render(controller())`)
810
are no longer ignored. Additionally, it is now possible to add tags from code
911
before the response object has been created, by using the TagHandler, and from
10-
twig with the `fos_httpcache_tag` function.
11-
12+
Twig with the `fos_httpcache_tag` function.
1213
If you defined custom services for the `InvalidateTagCommand`, you should
1314
now inject the TagHandler instead of the CacheManager.
14-
15-
**deprecated** `CacheManager::tagResponse` in favor of `TagHandler::addTags`
16-
17-
* **2015-05-08** Added configuration option for custom proxy client (#208)
15+
* **deprecated** `CacheManager::tagResponse` in favor of `TagHandler::addTags`
16+
* Added configuration option for custom proxy client (#208)
1817
* Added support for a simple Etag header in the header configuration (#207)
1918

2019
1.2.0

DependencyInjection/Configuration.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
305305
->arrayNode('proxy_client')
306306
->children()
307307
->enumNode('default')
308-
->values(array('varnish', 'nginx'))
308+
->values(array('varnish', 'nginx', 'symfony'))
309309
->info('If you configure more than one proxy client, specify which client is the default.')
310310
->end()
311311
->arrayNode('varnish')
@@ -356,6 +356,28 @@ private function addProxyClientSection(ArrayNodeDefinition $rootNode)
356356
->end()
357357
->end()
358358

359+
->arrayNode('symfony')
360+
->fixXmlConfig('server')
361+
->children()
362+
->arrayNode('servers')
363+
->beforeNormalization()->ifString()->then(function ($v) { return preg_split('/\s*,\s*/', $v); })->end()
364+
->useAttributeAsKey('name')
365+
->isRequired()
366+
->requiresAtLeastOneElement()
367+
->prototype('scalar')->end()
368+
->info('Addresses of the hosts Symfony HttpCache is running on. May be hostname or ip, and with :port if not the default port 80.')
369+
->end()
370+
->scalarNode('base_url')
371+
->defaultNull()
372+
->info('Default host name and optional path for path based invalidation.')
373+
->end()
374+
->scalarNode('guzzle_client')
375+
->defaultNull()
376+
->info('Guzzle service to use for customizing the requests.')
377+
->end()
378+
->end()
379+
->end()
380+
359381
->end()
360382
->end()
361383
->end();

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loa
235235
if (isset($config['nginx'])) {
236236
$this->loadNginx($container, $loader, $config['nginx']);
237237
}
238+
if (isset($config['symfony'])) {
239+
$this->loadSymfony($container, $loader, $config['symfony']);
240+
}
238241

239242
$container->setAlias(
240243
$this->getAlias().'.default_proxy_client',
@@ -256,12 +259,8 @@ private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader,
256259
}
257260
$container->setParameter($this->getAlias().'.proxy_client.varnish.servers', $config['servers']);
258261
$container->setParameter($this->getAlias().'.proxy_client.varnish.base_url', $baseUrl);
259-
if ($config['guzzle_client']) {
260-
$container->getDefinition($this->getAlias().'.proxy_client.varnish')
261-
->addArgument(
262-
new Reference($config['guzzle_client'])
263-
)
264-
;
262+
if (!empty($config['guzzle_client'])) {
263+
$container->setParameter($this->getAlias().'.proxy_client.varnish.guzzle_client', $config['guzzle_client']);
265264
}
266265
}
267266

@@ -279,6 +278,28 @@ private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, a
279278
$container->setParameter($this->getAlias().'.proxy_client.nginx.servers', $config['servers']);
280279
$container->setParameter($this->getAlias().'.proxy_client.nginx.base_url', $baseUrl);
281280
$container->setParameter($this->getAlias().'.proxy_client.nginx.purge_location', $config['purge_location']);
281+
if (!empty($config['guzzle_client'])) {
282+
$container->setParameter($this->getAlias().'.proxy_client.nginx.guzzle_client', $config['guzzle_client']);
283+
}
284+
}
285+
286+
private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config)
287+
{
288+
$loader->load('symfony-client.xml');
289+
foreach ($config['servers'] as $url) {
290+
$this->validateUrl($url, 'Not a valid web server address: "%s"');
291+
}
292+
if (!empty($config['base_url'])) {
293+
$baseUrl = $this->prefixSchema($config['base_url'], 'Not a valid base path: "%s"');
294+
$this->validateUrl($baseUrl, 'Not a valid base path: "%s"');
295+
} else {
296+
$baseUrl = null;
297+
}
298+
$container->setParameter($this->getAlias().'.proxy_client.symfony.servers', $config['servers']);
299+
$container->setParameter($this->getAlias().'.proxy_client.symfony.base_url', $baseUrl);
300+
if (!empty($config['guzzle_client'])) {
301+
$container->setParameter($this->getAlias().'.proxy_client.symfony.guzzle_client', $config['guzzle_client']);
302+
}
282303
}
283304

284305
private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config)

Resources/config/nginx.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<argument>%fos_http_cache.proxy_client.nginx.servers%</argument>
1515
<argument>%fos_http_cache.proxy_client.nginx.base_url%</argument>
1616
<argument>%fos_http_cache.proxy_client.nginx.purge_location%</argument>
17+
<argument type="service" id="fos_http_cache.proxy_client.nginx.guzzle_client" on-invalid="ignore"/>
1718
</service>
1819

1920
<service id="fos_http_cache.test.client.nginx"

Resources/config/symfony.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<parameters>
8+
<parameter key="fos_http_cache.proxy_client.symfony.class">FOS\HttpCache\ProxyClient\Symfony</parameter>
9+
</parameters>
10+
11+
<services>
12+
<service id="fos_http_cache.proxy_client.symfony"
13+
class="%fos_http_cache.proxy_client.symfony.class%">
14+
<argument>%fos_http_cache.proxy_client.symfony.servers%</argument>
15+
<argument>%fos_http_cache.proxy_client.symfony.base_url%</argument>
16+
<argument type="service" id="fos_http_cache.proxy_client.symfony.guzzle_client" on-invalid="ignore"/>
17+
</service>
18+
</services>
19+
20+
</container>

Resources/config/varnish.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
class="%fos_http_cache.proxy_client.varnish.class%">
1414
<argument>%fos_http_cache.proxy_client.varnish.servers%</argument>
1515
<argument>%fos_http_cache.proxy_client.varnish.base_url%</argument>
16+
<argument type="service" id="fos_http_cache.proxy_client.varnish.guzzle_client" on-invalid="ignore"/>
1617
</service>
1718

1819
<service id="fos_http_cache.test.client.varnish"

Resources/doc/features/invalidation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ Invalidation
44
**Works with**:
55

66
* :ref:`Varnish <foshttpcache:varnish configuration>`
7-
* :ref:`Nginx <foshttpcache:nginx configuration>`
7+
* :ref:`Nginx <foshttpcache:nginx configuration>` (except regular expressions)
8+
* :doc:`symfony-http-cache` (except regular expressions)
89

910
**Preparation**:
1011

Resources/doc/features/symfony-http-cache.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ concept is to use event subscribers on the HttpCache class.
1515

1616
Symfony HttpCache support is currently limited to following features:
1717

18+
* Purge
19+
* Refresh
1820
* User context
1921

2022
Extending the correct HttpCache
@@ -34,7 +36,7 @@ Instead of extending ``Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache``, you
3436
.. tip::
3537

3638
If your class already needs to extend a different class, simply copy the event
37-
handling code from the EventDispatchingHttpCache into your ``AppCache`` class.
39+
handling code from the ``EventDispatchingHttpCache`` into your ``AppCache`` class.
3840
The drawback is that you need to manually check whether you need to adjust your
3941
``AppCache`` each time you update the FOSHttpCache library.
4042

@@ -43,7 +45,8 @@ about. You can disable subscribers, or customize how they are instantiated.
4345

4446
If you do not need all subscribers, or need to register some yourself to
4547
customize their behavior, overwrite ``getOptions`` and return the right bitmap
46-
in ``fos_default_subscribers``. Use the constants provided by the cache kernel::
48+
in ``fos_default_subscribers``. Use the constants provided by the
49+
``EventDispatchingHttpCache``::
4750

4851
public function getOptions()
4952
{

Resources/doc/features/tagging.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
Tagging
22
=======
33

4-
**Works with**: :ref:`Varnish <foshttpcache:varnish_tagging>`
4+
**Works with**:
5+
6+
* :ref:`Varnish <foshttpcache:varnish_tagging>`
57

68
If your application has many intricate relationships between cached items,
79
which makes it complex to invalidate them by route, cache tagging will be

Resources/doc/features/user-context.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ User Context
33

44
**Works with**:
55

6-
- :ref:`Varnish <foshttpcache:varnish user context>`
7-
- :doc:`symfony-http-cache`
6+
* :ref:`Varnish <foshttpcache:varnish user context>`
7+
* :doc:`symfony-http-cache`
88

99
If your application serves different content depending on the user's group
1010
or context (guest, editor, admin), you can cache that content per user context.

0 commit comments

Comments
 (0)