Skip to content

Commit 889df5a

Browse files
committed
Derive caching status of content modules from smarty {nocache} tag, refactoring
1 parent a48448d commit 889df5a

File tree

3 files changed

+40
-26
lines changed

3 files changed

+40
-26
lines changed

src/wcmf/application/views/plugins/function.module.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020
*
2121
* [ContentModule]
2222
* next_events = \app\src\lib\content\NextEvents
23+
* form = \app\src\lib\content\Form
24+
*
25+
* Module output is cached by default. To prevent it from being cached it must be
26+
* wrapped within a {nocache} tag.
2327
*
2428
* Usage example:
2529
* @code
26-
* {module name='next_events' ...}
30+
* {module name='next_events' ...} {* cache module output *}
31+
* {nocache}{module name='form' ...}{/nocache} {* don't cache module output *}
2732
* @endcode
2833
*
2934
* @param $params Array with keys:
@@ -42,7 +47,8 @@ function smarty_function_module($params, Smarty_Internal_Template $template) {
4247
$name = $params['name'];
4348
$moduleClass = isset($modules[$name]) ? $modules[$name] : null;
4449
if ($moduleClass && class_exists($moduleClass) && in_array($requiredInterface, class_implements($moduleClass))) {
45-
$contentModule = new $moduleClass($name, $template, $params);
50+
$contentModule = new $moduleClass();
51+
$contentModule->initialize($template, $params);
4652
}
4753
else {
4854
LogManager::getLogger(__FILE__)->error('Content class \''.$moduleClass.'\' for content module \''.$name.'\' does not exist or does not implement interface \''.$requiredInterface.'\'');

src/wcmf/lib/presentation/ContentModule.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@
1111
namespace wcmf\lib\presentation;
1212

1313
/**
14-
* Interface for content modules.
14+
* Interface for smarty content modules.
1515
*
1616
* @author ingo herwig <[email protected]>
1717
*/
1818
interface ContentModule {
19+
/**
20+
* Initialize the instance
21+
* @param $parentTemplate Template object that includes this content module
22+
* @param $params Associative array of parameters passed to the smarty {module} tag
23+
*/
24+
public function initialize(\Smarty_Internal_Template $parentTemplate, array $params);
1925

2026
/**
2127
* Render the content

src/wcmf/lib/presentation/impl/AbstractContentModule.php

+25-23
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
*
2121
* Each content module is supposed to be associated with a template file that defines the output.
2222
*
23-
* The cache id of the template is calculated from the cacheId of the parent template and the name of the module
24-
* and an optional "cacheId" plugin parameter that is only necessary, if the same module is used several times in
25-
* the same parent template.
23+
* If output caching is activated, the cache id of the template is calculated from the cacheId of the parent template
24+
* and the name of the module and an optional "cacheId" plugin parameter that is only necessary, if the same module is used
25+
* several times in the same parent template.
2626
*/
2727
abstract class AbstractContentModule {
2828

@@ -33,53 +33,55 @@ abstract class AbstractContentModule {
3333
private $logger = null;
3434

3535
/**
36-
* Constructor
37-
* @param $name Template filename name without .tpl extension (must exist in templates/modules)
38-
* @param $parentTemplate Template object that includes this content module
39-
* @param $params Associative array of parameters passed to the smarty plugin
36+
* @see ContentModule::initialize()
4037
*/
41-
public function __construct($name, \Smarty_Internal_Template $parentTemplate, array $params) {
42-
$this->name = $name;
38+
public function initialize(\Smarty_Internal_Template $parentTemplate, array $params) {
39+
$this->name = $params['name'];
4340
$this->tpl = $this->getTemplateFile();
4441
if (!file_exists($this->tpl)) {
4542
throw new IllegalArgumentException('The template file \''.$this->tpl.'\' does not exist.');
4643
}
4744
$this->view = ObjectFactory::getInstance('view');
4845
$this->logger = LogManager::getLogger(get_class($this));
4946

47+
$isCaching = !$parentTemplate->cached->has_nocache_code;
48+
5049
// calculate cache id
51-
$this->cacheId = $parentTemplate->cache_id.'-'.$name.(isset($params['cacheId']) ? $params['cacheId'] : '');
50+
$this->cacheId = $isCaching ? $parentTemplate->cache_id.'-'.$this->name.(isset($params['cacheId']) ? $params['cacheId'] : '') : null;
5251

5352
// handle parameters depending on the cache state of the parent template
5453
$cache = ObjectFactory::getInstance('dynamicCache');
55-
if ($parentTemplate->isCached()) {
54+
if ($isCaching && $parentTemplate->isCached()) {
5655
// get cached parameters
5756
$parentParams = $cache->exists('module-cache', $parentTemplate->cache_id) ? $cache->get('module-cache', $parentTemplate->cache_id)['params'] : [];
5857
$this->params = $cache->exists('module-cache', $this->cacheId) ? $cache->get('module-cache', $this->cacheId)['params'] : array_merge($parentParams, $params);
5958
}
6059
else {
61-
// use fresh parameters
62-
$this->params = $params;
63-
foreach ($this->getRequiredTemplateVars() as $var) {
64-
$this->params[$var] = $parentTemplate->getTemplateVars($var);
60+
// use fresh parameters (including parent template variables)
61+
$this->params = array_merge($params, $parentTemplate->getTemplateVars());
62+
63+
if ($isCaching) {
64+
// store parameters for later use
65+
$cache->put('module-cache', $this->cacheId, ['params' => $this->params]);
66+
$cache->put('module-cache', $parentTemplate->cache_id, ['params' => $this->params]);
6567
}
66-
// store parameters for later use
67-
$cache->put('module-cache', $this->cacheId, ['params' => $this->params]);
68-
$cache->put('module-cache', $parentTemplate->cache_id, ['params' => $this->params]);
6968
}
70-
// check parameters and assign them to the view
69+
70+
// check parameters
7171
foreach ($this->getRequiredTemplateVars() as $var) {
7272
if (!isset($this->params[$var])) {
7373
$this->logger->error('Parameter \''.$var.'\' is undefined.');
7474
}
75-
else {
76-
$this->view->setValue($var, $this->params[$var]);
77-
}
75+
}
76+
77+
// assign paramters to the view
78+
foreach (array_keys($this->params) as $var) {
79+
$this->view->setValue($var, $this->params[$var]);
7880
}
7981
}
8082

8183
/**
82-
* Render the module
84+
* @see ContentModule::render()
8385
*/
8486
public function render() {
8587
if (!$this->view->isCached($this->tpl, $this->cacheId)) {

0 commit comments

Comments
 (0)