Skip to content

Commit c30e0e6

Browse files
committed
Merge pull request #148 from FriendsOfSymfony/invalidate-route-absolute
invalide/refresh Route should generate absolute urls
2 parents fb1c680 + 1ff3512 commit c30e0e6

File tree

8 files changed

+87
-6
lines changed

8 files changed

+87
-6
lines changed

CacheManager.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class CacheManager extends CacheInvalidator
2929
*/
3030
private $urlGenerator;
3131

32+
/**
33+
* What type of urls to generate.
34+
*
35+
* @var bool|string
36+
*/
37+
private $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
38+
3239
/**
3340
* Constructor
3441
*
@@ -41,6 +48,16 @@ public function __construct(ProxyClientInterface $cache, UrlGeneratorInterface $
4148
$this->urlGenerator = $urlGenerator;
4249
}
4350

51+
/**
52+
* Set what type of URLs to generate.
53+
*
54+
* @param bool|string $generateUrlType One of the constants in UrlGeneratorInterface
55+
*/
56+
public function setGenerateUrlType($generateUrlType)
57+
{
58+
$this->generateUrlType = $generateUrlType;
59+
}
60+
4461
/**
4562
* Assign cache tags to a response
4663
*
@@ -80,7 +97,7 @@ public function tagResponse(Response $response, array $tags, $replace = false)
8097
*/
8198
public function invalidateRoute($name, array $parameters = array(), array $headers = array())
8299
{
83-
$this->invalidatePath($this->urlGenerator->generate($name, $parameters), $headers);
100+
$this->invalidatePath($this->urlGenerator->generate($name, $parameters, $this->generateUrlType), $headers);
84101

85102
return $this;
86103
}
@@ -96,7 +113,7 @@ public function invalidateRoute($name, array $parameters = array(), array $heade
96113
*/
97114
public function refreshRoute($route, array $parameters = array(), array $headers = array())
98115
{
99-
$this->refreshPath($this->urlGenerator->generate($route, $parameters), $headers);
116+
$this->refreshPath($this->urlGenerator->generate($route, $parameters, $this->generateUrlType), $headers);
100117

101118
return $this;
102119
}

DependencyInjection/Configuration.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1717
use Symfony\Component\Config\Definition\ConfigurationInterface;
1818
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
19+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1920

2021
/**
2122
* This class contains the configuration information for the bundle
@@ -450,6 +451,17 @@ private function addCacheManagerSection(ArrayNodeDefinition $rootNode)
450451
->defaultValue('auto')
451452
->info('Allows to disable the invalidation manager. Enabled by default if you configure a proxy client.')
452453
->end()
454+
->enumNode('generate_url_type')
455+
->values(array(
456+
'auto',
457+
UrlGeneratorInterface::ABSOLUTE_PATH,
458+
UrlGeneratorInterface::ABSOLUTE_URL,
459+
UrlGeneratorInterface::NETWORK_PATH,
460+
UrlGeneratorInterface::RELATIVE_PATH,
461+
))
462+
->defaultValue('auto')
463+
->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.')
464+
->end()
453465
->end()
454466
;
455467
}

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2020
use Symfony\Component\DependencyInjection\Reference;
2121
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2223

2324
/**
2425
* {@inheritdoc}
@@ -62,6 +63,16 @@ public function load(array $configs, ContainerBuilder $container)
6263
}
6364

6465
if ($config['cache_manager']['enabled']) {
66+
if ('auto' === $config['cache_manager']['generate_url_type']) {
67+
$defaultClient = $this->getDefault($config['proxy_client']);
68+
$generateUrlType = isset($config['proxy_client'][$defaultClient]['base_url'])
69+
? UrlGeneratorInterface::ABSOLUTE_PATH
70+
: UrlGeneratorInterface::ABSOLUTE_URL
71+
;
72+
} else {
73+
$generateUrlType = $config['cache_manager']['generate_url_type'];
74+
}
75+
$container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType);
6576
$loader->load('cache_manager.xml');
6677
}
6778

Resources/config/cache_manager.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
<call method="setEventDispatcher">
2222
<argument id="event_dispatcher" type="service" on-invalid="ignore" />
2323
</call>
24+
<call method="setGenerateUrlType">
25+
<argument>%fos_http_cache.cache_manager.generate_url_type%</argument>
26+
</call>
2427
</service>
2528

2629
<service id="fos_http_cache.event_listener.log"

Resources/doc/reference/configuration.rst

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

1010
configuration/proxy-client
11+
configuration/cache-manager
1112
configuration/headers
1213
configuration/invalidation
1314
configuration/tags
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
cache_manager
2+
=============
3+
4+
The cache manager is the primary interface to invalidate caches. It is enabled
5+
by default if a :doc:`Proxy Client <proxy-client>` is configured.
6+
7+
.. code-block:: yaml
8+
9+
# app/config/config.yml
10+
fos_http_cache:
11+
cache_manager:
12+
enabled: true
13+
generate_url_type: true
14+
15+
enabled
16+
-------
17+
18+
**type**: ``enum`` **options**: ``auto``, ``true``, ``false``
19+
20+
Whether the cache manager service should be enabled. By default, it is enabled
21+
if a proxy client is configured. It can not be enabled without a proxy client.
22+
23+
generate_url_type
24+
-----------------
25+
26+
**type**: ``enum`` **options**: ``auto``, ``true``, ``false``, ``relative``, ``network``
27+
28+
The ``$referenceType`` to be used when generating URLs in the invalidateRoute and
29+
refreshRoute calls. True results in absolute URLs including the current domain,
30+
``false`` generates a path without domain, needing a ``base_url`` to be configured
31+
on the proxy client. When set to ``auto``, the value is determined based on ``base_url``
32+
of the default proxy client.

Tests/Unit/CacheManagerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use FOS\HttpCacheBundle\CacheManager;
1515
use \Mockery;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1718

1819
class CacheManagerTest extends \PHPUnit_Framework_TestCase
1920
{
@@ -35,11 +36,11 @@ public function testInvalidateRoute()
3536

3637
$router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface')
3738
->shouldReceive('generate')
38-
->with('my_route', array())
39+
->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH)
3940
->andReturn('/my/route')
4041

4142
->shouldReceive('generate')
42-
->with('route_with_params', array('id' => 123))
43+
->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH)
4344
->andReturn('/route/with/params/id/123')
4445
->getMock();
4546

@@ -62,11 +63,11 @@ public function testRefreshRoute()
6263

6364
$router = \Mockery::mock('\Symfony\Component\Routing\Generator\UrlGeneratorInterface')
6465
->shouldReceive('generate')
65-
->with('my_route', array())
66+
->with('my_route', array(), UrlGeneratorInterface::ABSOLUTE_PATH)
6667
->andReturn('/my/route')
6768

6869
->shouldReceive('generate')
69-
->with('route_with_params', array('id' => 123))
70+
->with('route_with_params', array('id' => 123), UrlGeneratorInterface::ABSOLUTE_PATH)
7071
->andReturn('/route/with/params/id/123')
7172
->getMock();
7273

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function testSupportsAllConfigFormats()
9393
),
9494
'cache_manager' => array(
9595
'enabled' => true,
96+
'generate_url_type' => 'auto',
9697
),
9798
'tags' => array(
9899
'enabled' => 'auto',
@@ -193,6 +194,7 @@ public function testSupportsNginx()
193194
),
194195
);
195196
$expectedConfiguration['cache_manager']['enabled'] = 'auto';
197+
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
196198
$expectedConfiguration['tags']['enabled'] = 'auto';
197199
$expectedConfiguration['invalidation']['enabled'] = 'auto';
198200
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
@@ -250,6 +252,7 @@ public function testSplitOptions()
250252
),
251253
);
252254
$expectedConfiguration['cache_manager']['enabled'] = 'auto';
255+
$expectedConfiguration['cache_manager']['generate_url_type'] = 'auto';
253256
$expectedConfiguration['tags']['enabled'] = 'auto';
254257
$expectedConfiguration['invalidation']['enabled'] = 'auto';
255258
$expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto';
@@ -418,6 +421,7 @@ private function getEmptyConfig()
418421
return array(
419422
'cache_manager' => array(
420423
'enabled' => false,
424+
'generate_url_type' => 'auto',
421425
),
422426
'tags' => array(
423427
'enabled' => false,

0 commit comments

Comments
 (0)