Skip to content

Commit 4b004e6

Browse files
dbuddeboer
authored andcommitted
adding symfony cache proxy client
1 parent d269c3f commit 4b004e6

File tree

11 files changed

+129
-30
lines changed

11 files changed

+129
-30
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/symfony-http-cache.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Instead of extending ``Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache``, you
3636
.. tip::
3737

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

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

4646
If you do not need all subscribers, or need to register some yourself to
4747
customize their behavior, overwrite ``getOptions`` and return the right bitmap
48-
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``::
4950

5051
public function getOptions()
5152
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ specify the ``custom_proxy_client`` field.
2222
Whether the cache manager service should be enabled. By default, it is enabled
2323
if a proxy client is configured. It can not be enabled without a proxy client.
2424

25+
.. _custom_proxy_client:
26+
2527
``custom_proxy_client``
2628
-----------------------
2729

Resources/doc/reference/configuration/proxy-client.rst

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ to work.
1010

1111
The proxy client is also directly available as a service
1212
(``fos_http_cache.proxy_client.default`` and ``fos_http_cache.proxy_client.varnish``
13-
or ``fos_http_cache.proxy_client.nginx``) that you can use directly.
13+
, ``fos_http_cache.proxy_client.nginx`` or ``fos_http_cache.proxy_client.symfony``)
14+
that you can use directly.
15+
16+
If you need to adjust the proxy client, you can also configure the ``CacheManager``
17+
with a :ref:`custom proxy client <custom_proxy_client>` that you defined as a
18+
service. In that case, you do not need to configure anything in the
19+
``proxy_client`` configuration section.
1420

1521
varnish
1622
-------
@@ -33,6 +39,10 @@ Comma-separated list of IP addresses or host names of your
3339
caching proxy servers. The port those servers will be contacted
3440
defaults to 80; you can specify a different port with ``:<port>``.
3541

42+
When using a multi-server setup, make sure to include **all** proxy servers in
43+
this list. Invalidation must happen on all systems or you will end up with
44+
inconsistent caches.
45+
3646
``base_url``
3747
""""""""""""
3848

@@ -83,10 +93,24 @@ Separate location that purge requests will be sent to.
8393
See the :ref:`FOSHttpCache library docs <foshttpcache:nginx configuration>`
8494
on how to configure Nginx.
8595

96+
symfony
97+
-------
98+
99+
.. code-block:: yaml
100+
101+
# app/config/config.yml
102+
fos_http_cache:
103+
proxy_client:
104+
symfony:
105+
servers: 123.123.123.1:6060, 123.123.123.2
106+
base_url: yourwebsite.com
107+
108+
For ``servers`` and ``base_url``, see above.
109+
86110
default
87111
-------
88112

89-
**type**: ``enum`` **options**: ``varnish``, ``nginx``
113+
**type**: ``enum`` **options**: ``varnish``, ``nginx``, ``symfony``
90114

91115
.. code-block:: yaml
92116
@@ -95,27 +119,29 @@ default
95119
proxy_client:
96120
default: varnish
97121
98-
The default proxy client that will be used by the cache manager.
99-
You can *use Nginx and Varnish in parallel*. If you need to cache and
100-
invalidate pages in both, you can configure both in this bundle.
101-
The cache manager however will only use the default client.
122+
If there is only one proxy client, it is automatically the default. Only
123+
configure this if you configured more than one proxy client.
124+
125+
The default proxy client that will be used by the cache manager. You can
126+
*configure Nginx, Varnish and Symfony proxy clients in parallel*. There is
127+
however only one cache manager and it will only use the default client.
102128

103129
Custom Guzzle Client
104130
--------------------
105131

106132
By default, the proxy client instantiates a `Guzzle client`_ to talk with the
107133
caching proxy. If you need to customize the requests, for example to send a
108134
basic authentication header, you can configure a service and specify that in
109-
the ``guzzle_client`` option. A sample service definition for using basic
110-
authentication looks like this:
135+
the ``guzzle_client`` option of any of the cache proxy clients. A sample
136+
service definition for using basic authentication looks like this:
111137

112138
.. code-block:: yaml
113139
114140
# app/config/config.yml
115141
acme.varnish.guzzle.client:
116142
class: Guzzle\Service\Client
117143
calls:
118-
- [setDefaultOption, [auth, [%varnish.username%, %varnish.password%, basic ]]]
144+
- [setDefaultOption, [auth, [%caching_proxy.username%, %caching_proxy.password%, basic ]]]
119145
120146
Caching Proxy Configuration
121147
---------------------------

Tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public function testConfigLoadVarnish()
3838
$this->assertFalse($container->hasDefinition('fos_http_cache.proxy_client.nginx'));
3939
$this->assertTrue($container->hasAlias('fos_http_cache.default_proxy_client'));
4040
$this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.invalidation'));
41+
42+
$this->assertFalse($container->hasParameter('fos_http_cache.proxy_client.varnish.guzzle_client'));
4143
}
4244

4345
public function testConfigLoadVarnishCustomGuzzle()
@@ -50,7 +52,11 @@ public function testConfigLoadVarnishCustomGuzzle()
5052

5153
$this->assertTrue($container->hasDefinition('fos_http_cache.proxy_client.varnish'));
5254
$def = $container->getDefinition('fos_http_cache.proxy_client.varnish');
53-
$this->assertEquals('my_guzzle', $def->getArgument(2));
55+
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Reference', $def->getArgument(2));
56+
$this->assertEquals('fos_http_cache.proxy_client.varnish.guzzle_client', $def->getArgument(2)->__toString());
57+
58+
$this->assertTrue($container->hasParameter('fos_http_cache.proxy_client.varnish.guzzle_client'));
59+
$this->assertEquals('my_guzzle', $container->getParameter('fos_http_cache.proxy_client.varnish.guzzle_client'));
5460
}
5561

5662
/**
@@ -150,7 +156,7 @@ public function testConfigLoadInvalidatorRules()
150156

151157
$this->assertMatcherCreated($container, array('_route' => 'my_route'));
152158
$this->assertListenerHasRule($container, 'fos_http_cache.event_listener.invalidation');
153-
159+
154160
// Test for runtime errors
155161
$container->compile();
156162
}
@@ -289,13 +295,13 @@ protected function createContainer()
289295
$container = new ContainerBuilder(
290296
new ParameterBag(array('kernel.debug' => false,))
291297
);
292-
298+
293299
// The cache_manager service depends on the router service
294300
$container->setDefinition(
295301
'router',
296302
new Definition('\Symfony\Component\Routing\Router')
297303
);
298-
304+
299305
return $container;
300306
}
301307

0 commit comments

Comments
 (0)