Skip to content

Commit 7df5eb3

Browse files
committed
refactor for using TagHandler
1 parent 19bd63d commit 7df5eb3

18 files changed

+128
-112
lines changed

CHANGELOG.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ Changelog
44
1.3.0
55
-----
66

7-
* **2015-02-20** Configured/annotated tags on subrequests (twig `render(controller())`)
8-
are no longer ignored. Additionally, it is now possible to add tags from code before
9-
the response has been created.
7+
* **2015-03-13** Configured/annotated cache tags on subrequests
8+
(twig `render(controller())`) are no longer ignored. Additionally, it is now
9+
possible to add tags from code before the response object has been created,
10+
by using the TagHandler.
11+
If you defined custom services for the ``InvalidateTagCommand``, you now need
12+
to inject the TagHandler instead of the CacheManager.
1013

1114
1.2.0
1215
-----

CacheManager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function setGenerateUrlType($generateUrlType)
6767
* response
6868
*
6969
* @return $this
70+
*
71+
* @deprecated Add tags with TagHandler::addTags and then use TagHandler::tagResponse
7072
*/
7173
public function tagResponse(Response $response, array $tags, $replace = false)
7274
{

Command/InvalidateTagCommand.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111

1212
namespace FOS\HttpCacheBundle\Command;
1313

14+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617
use Symfony\Component\Console\Input\InputArgument;
17-
use FOS\HttpCacheBundle\CacheManager;
18+
use FOS\HttpCache\Handler\TagHandler;
1819

1920
/**
2021
* A command to trigger cache invalidation by tag from the command line.
2122
*
2223
* @author David Buchmann <[email protected]>
2324
*/
24-
class InvalidateTagCommand extends BaseInvalidateCommand
25+
class InvalidateTagCommand extends ContainerAwareCommand
2526
{
27+
/**
28+
* @var TagHandler
29+
*/
30+
private $tagHandler;
31+
2632
/**
2733
* @var string
2834
*/
@@ -32,13 +38,14 @@ class InvalidateTagCommand extends BaseInvalidateCommand
3238
* If no cache manager is specified explicitly, fos_http_cache.cache_manager
3339
* is automatically loaded.
3440
*
35-
* @param CacheManager|null $cacheManager The cache manager to talk to.
36-
* @param string $commandName Name of this command, in case you want to reuse it.
41+
* @param TagHandler|null $tagHandler The tag handlerto talk to.
42+
* @param string $commandName Name of this command, in case you want to reuse it.
3743
*/
38-
public function __construct(CacheManager $cacheManager = null, $commandName = 'fos:httpcache:invalidate:tag')
44+
public function __construct(TagHandler $tagHandler = null, $commandName = 'fos:httpcache:invalidate:tag')
3945
{
4046
$this->commandName = $commandName;
41-
parent::__construct($cacheManager);
47+
$this->tagHandler = $tagHandler;
48+
parent::__construct();
4249
}
4350

4451
/**
@@ -72,6 +79,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
7279
{
7380
$tags = $input->getArgument('tags');
7481

75-
$this->getCacheManager()->invalidateTags($tags);
82+
$this->getTagManager()->invalidateTags($tags);
83+
}
84+
85+
/**
86+
* @return TagHandler
87+
*/
88+
protected function getTagManager()
89+
{
90+
if (!$this->tagHandler) {
91+
$this->tagHandler = $this->getContainer()->get('fos_http_cache.handler.tag_handler');
92+
}
93+
94+
return $this->tagHandler;
7695
}
7796
}

DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function load(array $configs, ContainerBuilder $container)
8080
$container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', $config['tags']['enabled']);
8181
if ($config['tags']['enabled']) {
8282
// true or auto
83+
$container->setParameter($this->getAlias().'.tag_handler.header', $config['tags']['header']);
8384
$loader->load('tag_listener.xml');
8485
if (!empty($config['tags']['rules'])) {
8586
$this->loadTagRules($container, $config['tags']['rules']);

EventListener/TagSubscriber.php

Lines changed: 15 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111

1212
namespace FOS\HttpCacheBundle\EventListener;
1313

14-
use FOS\HttpCacheBundle\CacheManager;
14+
use FOS\HttpCacheBundle\Handler\TagHandler;
1515
use FOS\HttpCacheBundle\Configuration\Tag;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19+
use Symfony\Component\HttpKernel\HttpKernelInterface;
1920
use Symfony\Component\HttpKernel\KernelEvents;
2021
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
2122

@@ -27,52 +28,29 @@
2728
class TagSubscriber extends AbstractRuleSubscriber implements EventSubscriberInterface
2829
{
2930
/**
30-
* @var CacheManager
31+
* @var TagHandler
3132
*/
32-
private $cacheManager;
33+
private $tagHandler;
3334

3435
/**
3536
* @var ExpressionLanguage
3637
*/
3738
private $expressionLanguage;
3839

39-
/**
40-
* List of tags to add to response.
41-
*
42-
* @var string[]
43-
*/
44-
private $tags = array();
45-
4640
/**
4741
* Constructor
4842
*
49-
* @param CacheManager $cacheManager
43+
* @param TagHandler $tagHandler
5044
* @param ExpressionLanguage|null $expressionLanguage
5145
*/
5246
public function __construct(
53-
CacheManager $cacheManager,
47+
TagHandler $tagHandler,
5448
ExpressionLanguage $expressionLanguage = null
5549
) {
56-
$this->cacheManager = $cacheManager;
50+
$this->tagHandler = $tagHandler;
5751
$this->expressionLanguage = $expressionLanguage;
5852
}
5953

60-
/**
61-
* Add tags to set on the response to the current request.
62-
*
63-
* Contrary to CacheManager::tagResponse, this method can be called before
64-
* the response object exists.
65-
*
66-
* Adding a tag during an unsafe request is forbidden and leads to an
67-
* error. Use CacheManager::invalidateTags directly.
68-
*
69-
* @param array $tags List of tags to add.
70-
*/
71-
public function addTags(array $tags)
72-
{
73-
$this->tags = array_merge($this->tags, $tags);
74-
}
75-
7654
/**
7755
* Process the _tags request attribute, which is set when using the Tag
7856
* annotation
@@ -86,43 +64,30 @@ public function onKernelResponse(FilterResponseEvent $event)
8664
{
8765
$request = $event->getRequest();
8866
$response = $event->getResponse();
89-
if ($request->isMethodSafe()) {
90-
$tags = $this->tags;
91-
$this->tags = array();
92-
} else {
93-
$tags = array();
94-
}
9567

68+
$tags = array();
9669
// Only set cache tags or invalidate them if response is successful
9770
if ($response->isSuccessful()) {
98-
$tags = array_merge($tags, $this->getAnnotationTags($request));
71+
$tags = $this->getAnnotationTags($request);
9972
}
10073

10174
$configuredTags = $this->matchRule($request, $response);
10275
if ($configuredTags) {
103-
foreach ($configuredTags['tags'] as $tag) {
104-
$tags[] = $tag;
105-
}
76+
$tags = array_merge($tags, $configuredTags['tags']);
10677
foreach ($configuredTags['expressions'] as $expression) {
10778
$tags[] = $this->evaluateTag($expression, $request);
10879
}
10980
}
11081

111-
if (!count($tags)) {
112-
return;
113-
}
114-
11582
if ($request->isMethodSafe()) {
116-
if ($event->isMasterRequest()) {
83+
$this->tagHandler->addTags($tags);
84+
if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
11785
// For safe requests (GET and HEAD), set cache tags on response
118-
$this->cacheManager->tagResponse($response, $tags);
119-
} else {
120-
// Retain tags to attach to response to master request
121-
$this->tags = $tags;
86+
$this->tagHandler->tagResponse($response);
12287
}
123-
} else {
88+
} elseif (count($tags)) {
12489
// For non-safe methods, invalidate the tags
125-
$this->cacheManager->invalidateTags($tags);
90+
$this->tagHandler->invalidateTags($tags);
12691
}
12792
}
12893

Handler/TagHandler.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace FOS\HttpCacheBundle\Handler;
4+
5+
use FOS\HttpCache\Handler\TagHandler as BaseTagHandler;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
class TagHandler extends BaseTagHandler
9+
{
10+
/**
11+
* Tag response with the previously added tags.
12+
*
13+
* @param Response $response
14+
* @param bool $replace Whether to replace the current tags on the
15+
* response
16+
*
17+
* @return $this
18+
*/
19+
public function tagResponse(Response $response, $replace = false)
20+
{
21+
if (!$replace && $response->headers->has($this->getTagsHeaderName())) {
22+
$header = $response->headers->get($this->getTagsHeaderName());
23+
if ('' !== $header) {
24+
$this->addTags(explode(',', $response->headers->get($this->getTagsHeaderName())));
25+
}
26+
}
27+
28+
if ($this->getTagsHeaderValue()) {
29+
$response->headers->set($this->getTagsHeaderName(), $this->getTagsHeaderValue());
30+
}
31+
32+
return $this;
33+
}
34+
}

Resources/config/cache_manager.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</service>
4646

4747
<service id="fos_http_cache.command.invalidate_tag" class="%fos_http_cache.command.invalidate_tag.class%">
48-
<argument type="service" id="fos_http_cache.cache_manager" />
48+
<argument type="service" id="fos_http_cache.handler.tag_handler" />
4949
<tag name="console.command"/>
5050
</service>
5151

Resources/config/tag_listener.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
</parameters>
1010

1111
<services>
12+
<service id="fos_http_cache.handler.tag_handler" class="FOS\HttpCacheBundle\Handler\TagHandler">
13+
<argument type="service" id="fos_http_cache.cache_manager"/>
14+
<argument>%fos_http_cache.tag_handler.header%</argument>
15+
</service>
16+
1217
<service id="fos_http_cache.event_listener.tag"
1318
class="%fos_http_cache.event_listener.tag.class%">
14-
<argument type="service" id="fos_http_cache.cache_manager" />
19+
<argument type="service" id="fos_http_cache.handler.tag_handler" />
1520
<tag name="kernel.event_subscriber" />
1621
</service>
1722
</services>

Tests/Functional/Command/InvalidatePathCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testExecute()
2222
'fos_http_cache.cache_manager',
2323
'\FOS\HttpCacheBundle\CacheManager'
2424
)
25+
->shouldReceive('supports')->andReturn(true)
2526
->shouldReceive('invalidatePath')->once()->with('http://example.com/my/path')
2627
->shouldReceive('invalidatePath')->once()->with('http://example.com/other/path')
2728
->shouldReceive('flush')->once()->andReturn(2)
@@ -38,6 +39,7 @@ public function testExecuteVerbose()
3839
'fos_http_cache.cache_manager',
3940
'\FOS\HttpCacheBundle\CacheManager'
4041
)
42+
->shouldReceive('supports')->andReturn(true)
4143
->shouldReceive('invalidatePath')->once()->with('http://example.com/my/path')
4244
->shouldReceive('invalidatePath')->once()->with('http://example.com/other/path')
4345
->shouldReceive('flush')->once()->andReturn(2)

Tests/Functional/Command/InvalidateRegexCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testExecute()
2222
'fos_http_cache.cache_manager',
2323
'\FOS\HttpCacheBundle\CacheManager'
2424
)
25+
->shouldReceive('supports')->andReturn(true)
2526
->shouldReceive('invalidateRegex')->once()->with('/my.*/path')
2627
->shouldReceive('flush')->once()->andReturn(1)
2728
;
@@ -37,6 +38,7 @@ public function testExecuteVerbose()
3738
'fos_http_cache.cache_manager',
3839
'\FOS\HttpCacheBundle\CacheManager'
3940
)
41+
->shouldReceive('supports')->andReturn(true)
4042
->shouldReceive('invalidateRegex')->once()->with('/my.*/path')
4143
->shouldReceive('flush')->once()->andReturn(1)
4244
;

Tests/Functional/Command/InvalidateTagCommandTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ public function testExecute()
2222
'fos_http_cache.cache_manager',
2323
'\FOS\HttpCacheBundle\CacheManager'
2424
)
25-
->shouldReceive('invalidateTags')->once()->with(array('my-tag', 'other-tag'))
25+
->shouldReceive('supports')->andReturn(true)
26+
->shouldReceive('invalidate')->once()->with(array('X-Cache-Tags' => '(my\\-tag|other\\-tag)(,.+)?$'))
2627
->shouldReceive('flush')->once()->andReturn(1)
2728
;
2829

@@ -37,7 +38,8 @@ public function testExecuteVerbose()
3738
'fos_http_cache.cache_manager',
3839
'\FOS\HttpCacheBundle\CacheManager'
3940
)
40-
->shouldReceive('invalidateTags')->once()->with(array('my-tag', 'other-tag'))
41+
->shouldReceive('supports')->andReturn(true)
42+
->shouldReceive('invalidate')->once()->with(array('X-Cache-Tags' => '(my\\-tag|other\\-tag)(,.+)?$'))
4143
->shouldReceive('flush')->once()->andReturn(1)
4244
;
4345

Tests/Functional/Command/RefreshPathCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testExecute()
2222
'fos_http_cache.cache_manager',
2323
'\FOS\HttpCacheBundle\CacheManager'
2424
)
25+
->shouldReceive('supports')->andReturn(true)
2526
->shouldReceive('refreshPath')->once()->with('http://example.com/my/path')
2627
->shouldReceive('refreshPath')->once()->with('http://example.com/other/path')
2728
->shouldReceive('flush')->once()->andReturn(2)
@@ -38,6 +39,7 @@ public function testExecuteVerbose()
3839
'fos_http_cache.cache_manager',
3940
'\FOS\HttpCacheBundle\CacheManager'
4041
)
42+
->shouldReceive('supports')->andReturn(true)
4143
->shouldReceive('refreshPath')->once()->with('http://example.com/my/path')
4244
->shouldReceive('refreshPath')->once()->with('http://example.com/other/path')
4345
->shouldReceive('flush')->once()->andReturn(2)

Tests/Functional/EventListener/InvalidationSubscriberTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function testInvalidateRoute()
2323
'fos_http_cache.cache_manager',
2424
'\FOS\HttpCacheBundle\CacheManager'
2525
)
26+
->shouldReceive('supports')->andReturn(true)
2627
->shouldReceive('invalidateRoute')->once()->with('test_noncached', array())
2728
->shouldReceive('invalidateRoute')->once()->with('test_cached', array('id' => 'myhardcodedid'))
2829
->shouldReceive('invalidateRoute')->once()->with('tag_one', array('id' => '42'))
@@ -40,6 +41,7 @@ public function testInvalidatePath()
4041
'fos_http_cache.cache_manager',
4142
'\FOS\HttpCacheBundle\CacheManager'
4243
)
44+
->shouldReceive('supports')->andReturn(true)
4345
->shouldReceive('invalidatePath')->once()->with('/cached')
4446
->shouldReceive('flush')->once()
4547
;
@@ -55,6 +57,7 @@ public function testErrorIsNotInvalidated()
5557
'fos_http_cache.cache_manager',
5658
'\FOS\HttpCacheBundle\CacheManager'
5759
)
60+
->shouldReceive('supports')->andReturn(true)
5861
->shouldReceive('invalidatePath')->never()
5962
->shouldReceive('flush')->once()
6063
;

0 commit comments

Comments
 (0)