From 9d58b8ebf8cad1004e3d13e2da8f2e0b04cdac13 Mon Sep 17 00:00:00 2001 From: Lucas Bartholemy Date: Sat, 7 Dec 2024 16:49:42 +0100 Subject: [PATCH 1/2] Example Files --- .../components/TemplateElementValue.php | 27 ++++++++ modules/template/elements/AbstractElement.php | 43 +++++++++++++ modules/template/elements/Text.php | 61 +++++++++++++++++++ .../services/TemplateInstanceRenderer.php | 51 ++++++++++++++++ modules/template/services/TemplateService.php | 36 +++++++++++ 5 files changed, 218 insertions(+) create mode 100644 modules/template/components/TemplateElementValue.php create mode 100644 modules/template/elements/AbstractElement.php create mode 100644 modules/template/elements/Text.php create mode 100644 modules/template/services/TemplateInstanceRenderer.php create mode 100644 modules/template/services/TemplateService.php diff --git a/modules/template/components/TemplateElementValue.php b/modules/template/components/TemplateElementValue.php new file mode 100644 index 00000000..969abee8 --- /dev/null +++ b/modules/template/components/TemplateElementValue.php @@ -0,0 +1,27 @@ +element = $element; + } + + public function __toString() + { + return $this->value; + } + +} \ No newline at end of file diff --git a/modules/template/elements/AbstractElement.php b/modules/template/elements/AbstractElement.php new file mode 100644 index 00000000..75df3884 --- /dev/null +++ b/modules/template/elements/AbstractElement.php @@ -0,0 +1,43 @@ +content_type !== get_class($this)) { + throw new InvalidArgumentException("Invalid element given!"); + } + } + + abstract public static function getElementTypeTitle(): string; + + abstract public static function getElementTypeDescription(): string; + + abstract public function getTemplateValue(?TemplateInstance $templateInstance): mixed; + + public static function create(TemplateElement $elementModel) + { + return Yii::createObject($elementModel->content_type); + } + + public function getTemplateName() + { + return $this->element->name; + } + + public function isDynamic(): bool + { + return false; + } + +} diff --git a/modules/template/elements/Text.php b/modules/template/elements/Text.php new file mode 100644 index 00000000..acb3c05a --- /dev/null +++ b/modules/template/elements/Text.php @@ -0,0 +1,61 @@ +getDataRecord($templateInstance)->hasValues()) { + $value = $this->getDataRecord($templateInstance)->content; + } else { + $value = $this->getDataRecord(null)->content; + } + + return new TemplateElementValue($this, $value); + } + + + /** + * @param TemplateInstance|null $templateInstance - Set null for default data + * @return TextContent + */ + private function getDataRecord(?TemplateInstance $templateInstance): TextContent + { + $data = TextContent::findOne(['template_instance_id' => $templateInstance->id]); + if ($data === null) { + $definition = new TextContent(); + $definition->template_instance_id = $templateInstance->id; + } + } + + private function getDefinitionRecord(Template $template): ?ImageContentDefinition + { + $definition = ImageContentDefinition::findOne(['template_id' => $template->id]); + if ($definition === null) { + $definition = new ImageContentDefinition(); + $definition->template_id = $template->id; + } + return $definition; + } + + + // More "service" method which are not neccesary be done in the model +} diff --git a/modules/template/services/TemplateInstanceRenderer.php b/modules/template/services/TemplateInstanceRenderer.php new file mode 100644 index 00000000..008915a3 --- /dev/null +++ b/modules/template/services/TemplateInstanceRenderer.php @@ -0,0 +1,51 @@ +templateInstance = $templateInstance; + $this->templateService = new TemplateService($this->templateInstance->template); + } + + public function render() + { + $cache = ($this->isCachable()) ? Yii::$app->cache : new DummyCache(); + + return $cache->getOrSet(static::CACHE_PREFIX . $this->templateInstance->id, function () { + $engine = TemplateEngineFactory::create("twig"); + + $twigVariables = []; + foreach ($this->templateService->getElements() as $element) { + $twigVariables[$element->getTemplateName()] = $element->getTemplateValue($this->templateInstance); + } + + return $engine->render($this->templateInstance->template->source, $twigVariables); + }); + } + + private function isCachable(): bool + { + foreach ($this->templateService->getElements() as $element) { + if ($element->isDynamic()) { + return false; + } + } + + return true; + } + +} \ No newline at end of file diff --git a/modules/template/services/TemplateService.php b/modules/template/services/TemplateService.php new file mode 100644 index 00000000..fb8be142 --- /dev/null +++ b/modules/template/services/TemplateService.php @@ -0,0 +1,36 @@ +template = $template; + } + + private function loadElements() + { + foreach ($this->template->elements as $elementModel) { + $this->elements[] = AbstractElement::create($elementModel); + } + } + + /** + * @return AbstractElement[] + */ + public function getElements(): array + { + return $this->elements; + } +} From 0534d3c30fdb22205c5559c04493825a9416a92f Mon Sep 17 00:00:00 2001 From: luke- Date: Sat, 7 Dec 2024 15:51:44 +0000 Subject: [PATCH 2/2] Autocommit PHP CS Fixer --- modules/template/components/TemplateElementValue.php | 2 +- modules/template/elements/Text.php | 1 - modules/template/services/TemplateInstanceRenderer.php | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/template/components/TemplateElementValue.php b/modules/template/components/TemplateElementValue.php index 969abee8..29c76037 100644 --- a/modules/template/components/TemplateElementValue.php +++ b/modules/template/components/TemplateElementValue.php @@ -24,4 +24,4 @@ public function __toString() return $this->value; } -} \ No newline at end of file +} diff --git a/modules/template/elements/Text.php b/modules/template/elements/Text.php index acb3c05a..9df983eb 100644 --- a/modules/template/elements/Text.php +++ b/modules/template/elements/Text.php @@ -9,7 +9,6 @@ class Text extends AbstractElement { - public static function getElementTypeTitle(): string { return "Text element"; diff --git a/modules/template/services/TemplateInstanceRenderer.php b/modules/template/services/TemplateInstanceRenderer.php index 008915a3..b609fbb9 100644 --- a/modules/template/services/TemplateInstanceRenderer.php +++ b/modules/template/services/TemplateInstanceRenderer.php @@ -9,7 +9,7 @@ class TemplateInstanceRendererService { - const CACHE_PREFIX = 'cp-'; + public const CACHE_PREFIX = 'cp-'; private $templateInstance; @@ -48,4 +48,4 @@ private function isCachable(): bool return true; } -} \ No newline at end of file +}