From 6e7fa7de881a14882bc2a33577168f80627f37fa Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Mon, 21 Mar 2016 17:51:14 +1100 Subject: [PATCH] Allow CmsObjects to define their own Twig cache key --- modules/backend/behaviors/FormController.php | 2 +- modules/cms/classes/CmsObject.php | 15 +++++++++++++++ modules/cms/classes/ComponentPartial.php | 9 +++++++++ modules/cms/contracts/CmsObject.php | 8 +++++++- modules/cms/twig/Loader.php | 2 +- 5 files changed, 33 insertions(+), 3 deletions(-) diff --git a/modules/backend/behaviors/FormController.php b/modules/backend/behaviors/FormController.php index b7e30f245..fe14ddc6d 100644 --- a/modules/backend/behaviors/FormController.php +++ b/modules/backend/behaviors/FormController.php @@ -373,7 +373,7 @@ class FormController extends ControllerBehavior protected function createModel() { $class = $this->config->modelClass; - $model = new $class(); + $model = new $class; return $model; } diff --git a/modules/cms/classes/CmsObject.php b/modules/cms/classes/CmsObject.php index f17ae5c50..45e9f3c3d 100644 --- a/modules/cms/classes/CmsObject.php +++ b/modules/cms/classes/CmsObject.php @@ -224,6 +224,21 @@ class CmsObject extends HalcyonModel implements CmsObjectContract return $this->content; } + /** + * Returns the key used by the Twig cache. + * @return string + */ + public function getTwigCacheKey() + { + $key = $this->getFilePath(); + + if ($event = $this->fireEvent('cmsObject.getTwigCacheKey', compact('key'), true)) { + $key = $event; + } + + return $key; + } + // // Internals // diff --git a/modules/cms/classes/ComponentPartial.php b/modules/cms/classes/ComponentPartial.php index 2129b1e49..9457ef8ab 100644 --- a/modules/cms/classes/ComponentPartial.php +++ b/modules/cms/classes/ComponentPartial.php @@ -167,6 +167,15 @@ class ComponentPartial extends Extendable implements CmsObjectContract return $this->content; } + /** + * Returns the key used by the Twig cache. + * @return string + */ + public function getTwigCacheKey() + { + return $this->getFilePath(); + } + /** * Returns the file name. * @return string diff --git a/modules/cms/contracts/CmsObject.php b/modules/cms/contracts/CmsObject.php index 7abfeb9ad..8b79a8174 100644 --- a/modules/cms/contracts/CmsObject.php +++ b/modules/cms/contracts/CmsObject.php @@ -54,4 +54,10 @@ interface CmsObject */ public function getTwigContent(); -} \ No newline at end of file + /** + * Returns the key used by the Twig cache. + * @return string + */ + public function getTwigCacheKey(); + +} diff --git a/modules/cms/twig/Loader.php b/modules/cms/twig/Loader.php index bf6c19086..eee3bb4dd 100644 --- a/modules/cms/twig/Loader.php +++ b/modules/cms/twig/Loader.php @@ -49,7 +49,7 @@ class Loader implements Twig_LoaderInterface */ public function getCacheKey($name) { - return $this->obj->getFilePath(); + return $this->obj->getTwigCacheKey(); } /**