From 6cf5e2daac21510a66f6c17074aa456def9f9d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20R?= Date: Mon, 25 Jun 2018 21:27:03 +0200 Subject: [PATCH] Inital version of Twig ez_http_tag_location() function (#67) 1. Refactors Response taggers to get rid of (by now) unused configurator and reponse, in order to: 2. Expose Twig extension to have function for tagging locations Reason for picking location: It has all data needed, we need lcoation in order to be able to know which subtree to tag for (to make sure cache is cleared on subtree operations). If needed there could be a helper for contentInfo as well which loads main for you. --- src/Resources/config/view_cache.yml | 7 ++++ src/Twig/ContentTaggingExtension.php | 55 ++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/Twig/ContentTaggingExtension.php diff --git a/src/Resources/config/view_cache.yml b/src/Resources/config/view_cache.yml index ad4225e0..5beac9a6 100644 --- a/src/Resources/config/view_cache.yml +++ b/src/Resources/config/view_cache.yml @@ -77,3 +77,10 @@ services: parent: ezplatform.view_cache.response_tagger.abstract_value tags: - {name: ezplatform.cache_response_tagger} + + # Twig + ezplatform.view_cache.twig_extension: + class: EzSystems\PlatformHttpCacheBundle\Twig\ContentTaggingExtension + arguments: ['@ezplatform.view_cache.response_tagger.dispatcher'] + tags: + - {name: twig.extension} diff --git a/src/Twig/ContentTaggingExtension.php b/src/Twig/ContentTaggingExtension.php new file mode 100644 index 00000000..517459f1 --- /dev/null +++ b/src/Twig/ContentTaggingExtension.php @@ -0,0 +1,55 @@ +responseTagger = $responseTagger; + } + + /** + * @return array|\Twig_Function[] + */ + public function getFunctions() + { + return [ + new Twig_SimpleFunction( + 'ez_http_tag_location', + [$this, 'tagHttpCacheForLocation'] + ), + ]; + } + + /** + * Adds tags to current response. + * + * @internal Function is only for use within this class (and implicit by Twig). + * + * @param Location $location + */ + public function tagHttpCacheForLocation(Location $location) + { + $this->responseTagger->tag($location); + $this->responseTagger->tag($location->getContentInfo()); + } +}