AbstractContentModule.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * AbstractContentModule is the base class for content module implementations.
20  *
21  * Each content module is supposed to be associated with a template file that defines the output.
22  *
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.
26  */
27 abstract class AbstractContentModule {
28 
29  private $name = null;
30  private $view = null;
31  private $tpl = null;
32  private $params = [];
33  private $logger = null;
34 
35  /**
36  * @see ContentModule::initialize()
37  */
38  public function initialize(\Smarty_Internal_Template $parentTemplate, array $params) {
39  $this->name = $params['name'];
40  $this->tpl = $this->getTemplateFile();
41  if (!file_exists($this->tpl)) {
42  throw new IllegalArgumentException('The template file \''.$this->tpl.'\' does not exist.');
43  }
44  $this->view = ObjectFactory::getInstance('view');
45  $this->logger = LogManager::getLogger(get_class($this));
46 
47  $isCaching = !$parentTemplate->cached->has_nocache_code;
48 
49  // calculate cache id
50  $this->cacheId = $isCaching ? $parentTemplate->cache_id.'-'.$this->name.(isset($params['cacheId']) ? $params['cacheId'] : '') : null;
51 
52  // handle parameters depending on the cache state of the parent template
53  $cache = ObjectFactory::getInstance('dynamicCache');
54  if ($isCaching && $parentTemplate->isCached()) {
55  // get cached parameters
56  $parentParams = $cache->exists('module-cache', $parentTemplate->cache_id) ? $cache->get('module-cache', $parentTemplate->cache_id)['params'] : [];
57  $this->params = $cache->exists('module-cache', $this->cacheId) ? $cache->get('module-cache', $this->cacheId)['params'] : array_merge($parentParams, $params);
58  }
59  else {
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]);
67  }
68  }
69 
70  // check parameters
71  foreach ($this->getRequiredTemplateVars() as $var) {
72  if (!isset($this->params[$var])) {
73  $this->logger->error('Parameter \''.$var.'\' is undefined.');
74  }
75  }
76 
77  // assign paramters to the view
78  foreach (array_keys($this->params) as $var) {
79  $this->view->setValue($var, $this->params[$var]);
80  }
81  }
82 
83  /**
84  * @see ContentModule::render()
85  */
86  public function render() {
87  if (!$this->view->isCached($this->tpl, $this->cacheId)) {
88  $this->assignContent($this->view, $this->params);
89  }
90  return $this->view->render($this->tpl, $this->cacheId, null, false);
91  }
92 
93  /**
94  * Get the names of the scalar template variables required from the parent template,
95  * object variables must be created and assigned in the assignContent() method
96  * @return Array of string
97  */
98  protected abstract function getRequiredTemplateVars();
99 
100  /**
101  * Get the template file name
102  * @return String
103  */
104  protected abstract function getTemplateFile();
105 
106  /**
107  * Assign the content to the view
108  * @param $view
109  * @param $params Parameter array passed to the module plugin
110  */
111  protected abstract function assignContent(View $view, array $params);
112 }
AbstractContentModule is the base class for content module implementations.
IllegalArgumentException signals an exception in method arguments.
getTemplateFile()
Get the template file name.
initialize(\Smarty_Internal_Template $parentTemplate, array $params)
View defines the interface for all view implementations.
Definition: View.php:18
LogManager is used to retrieve Logger instances.
Definition: LogManager.php:20
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...