1111
1212namespace FOS \HttpCacheBundle \EventListener ;
1313
14- use FOS \HttpCacheBundle \CacheManager ;
14+ use FOS \HttpCacheBundle \Handler \ TagHandler ;
1515use FOS \HttpCacheBundle \Configuration \Tag ;
1616use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1717use Symfony \Component \HttpFoundation \Request ;
1818use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
19+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
1920use Symfony \Component \HttpKernel \KernelEvents ;
2021use Symfony \Component \ExpressionLanguage \ExpressionLanguage ;
2122
2728class 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
@@ -39,14 +40,14 @@ class TagSubscriber extends AbstractRuleSubscriber implements EventSubscriberInt
3940 /**
4041 * Constructor
4142 *
42- * @param CacheManager $cacheManager
43+ * @param TagHandler $tagHandler
4344 * @param ExpressionLanguage|null $expressionLanguage
4445 */
4546 public function __construct (
46- CacheManager $ cacheManager ,
47+ TagHandler $ tagHandler ,
4748 ExpressionLanguage $ expressionLanguage = null
4849 ) {
49- $ this ->cacheManager = $ cacheManager ;
50+ $ this ->tagHandler = $ tagHandler ;
5051 $ this ->expressionLanguage = $ expressionLanguage ;
5152 }
5253
@@ -65,32 +66,28 @@ public function onKernelResponse(FilterResponseEvent $event)
6566 $ response = $ event ->getResponse ();
6667
6768 $ tags = array ();
68-
6969 // Only set cache tags or invalidate them if response is successful
7070 if ($ response ->isSuccessful ()) {
7171 $ tags = $ this ->getAnnotationTags ($ request );
7272 }
7373
7474 $ configuredTags = $ this ->matchRule ($ request , $ response );
7575 if ($ configuredTags ) {
76- foreach ($ configuredTags ['tags ' ] as $ tag ) {
77- $ tags [] = $ tag ;
78- }
76+ $ tags = array_merge ($ tags , $ configuredTags ['tags ' ]);
7977 foreach ($ configuredTags ['expressions ' ] as $ expression ) {
8078 $ tags [] = $ this ->evaluateTag ($ expression , $ request );
8179 }
8280 }
8381
84- if (!count ($ tags )) {
85- return ;
86- }
87-
8882 if ($ request ->isMethodSafe ()) {
89- // For safe requests (GET and HEAD), set cache tags on response
90- $ this ->cacheManager ->tagResponse ($ response , $ tags );
91- } else {
83+ $ this ->tagHandler ->addTags ($ tags );
84+ if (HttpKernelInterface::MASTER_REQUEST === $ event ->getRequestType ()) {
85+ // For safe requests (GET and HEAD), set cache tags on response
86+ $ this ->tagHandler ->tagResponse ($ response );
87+ }
88+ } elseif (count ($ tags )) {
9289 // For non-safe methods, invalidate the tags
93- $ this ->cacheManager ->invalidateTags ($ tags );
90+ $ this ->tagHandler ->invalidateTags ($ tags );
9491 }
9592 }
9693
0 commit comments