SmartyView.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 
20 
21 if (!class_exists('Smarty')) {
22  throw new ConfigurationException(
23  'wcmf\lib\presentation\view\impl\SmartyView requires '.
24  'Smarty. If you are using composer, add smarty/smarty '.
25  'as dependency to your project');
26 }
27 
28 /**
29  * View is used by Controller to handle the view presentation in MVC pattern.
30  * View is a subclass of Smarty that is customized for use with the wCMF.
31  *
32  * @note This class requires Smarty
33  *
34  * @author ingo herwig <ingo@wemove.com>
35  */
36 class SmartyView implements View {
37 
38  private static $sharedView = null;
39  private static $actionKeyProvider = null;
40  private static $logger = null;
41 
42  protected $view = null;
43 
44  /**
45  * Constructor
46  */
47  public function __construct() {
48  if (self::$logger == null) {
49  self::$logger = LogManager::getLogger(__CLASS__);
50  }
51  $this->view = new \Smarty();
52  $this->view->error_reporting = E_ALL;
53 
54  // set plugins directories
55  $this->view->addPluginsDir(
56  dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/application/views/plugins/'
57  );
58 
59  // load filter
60  $this->view->loadFilter('pre', 'removeprids');
61  $this->view->loadFilter('output', 'trimwhitespace');
62 
63  // setup default smarty directories
64  // will be overridden by call to setCacheDir
65  $this->view->setTemplateDir(WCMF_BASE);
66  $cacheDir = WCMF_BASE.'/smarty/';
67  $this->view->setCompileDir($cacheDir.'templates_c/');
68  $this->view->setCacheDir($cacheDir.'cache/');
69  }
70 
71  /**
72  * Set whether the view should check for
73  * template modifications or not
74  * @param $compileCheck Boolean
75  */
76  public function setCompileCheck($compileCheck) {
77  $this->view->compile_check = $compileCheck;
78  }
79 
80  /**
81  * Set whether views should be cached
82  * @param $caching Boolean
83  */
84  public function setCaching($caching) {
85  $this->view->caching = $caching ? \Smarty::CACHING_LIFETIME_CURRENT : \Smarty::CACHING_OFF;
86  }
87 
88  /**
89  * Set the time a view should be cached
90  * @param $cacheLifeTime Integer (seconds)
91  */
92  public function setCacheLifetime($cacheLifeTime) {
93  $this->view->cache_lifetime = $cacheLifeTime;
94  }
95 
96  /**
97  * Set the caching directory
98  * If not existing, the directory will be created relative to WCMF_BASE.
99  * @param $cacheDir String
100  */
101  public function setCacheDir($cacheDir) {
102  $this->view->setCompileDir(WCMF_BASE.$cacheDir.'templates_c/');
103  $this->view->setCacheDir(WCMF_BASE.$cacheDir.'cache/');
104 
105  $fileUtil = new FileUtil();
106  $fileUtil->mkdirRec($this->view->getCompileDir());
107  $fileUtil->mkdirRec($this->view->getCacheDir());
108  }
109 
110  /**
111  * Set a additional plugins directory
112  * @param $pluginsDir Directory relative to WCMF_BASE
113  */
114  public function setPluginsDir($pluginsDir) {
115  $this->view->addPluginsDir(WCMF_BASE.$pluginsDir);
116  }
117 
118  /**
119  * Set additional output filters
120  * @param $outputFilter
121  */
122  public function setOutputFilter($outputFilter) {
123  foreach ($outputFilter as $filter) {
124  $this->view->loadFilter('output', $filter);
125  }
126  }
127 
128  /**
129  * @see View::setValue()
130  */
131  public function setValue($name, $value) {
132  $this->view->assign($name, $value);
133  }
134 
135  /**
136  * @see View::getValue()
137  */
138  public function getValue($name) {
139  return $this->view->getTemplateVars($name);
140  }
141 
142  /**
143  * @see View::getValues()
144  */
145  public function getValues() {
146  return $this->view->getTemplateVars();
147  }
148 
149  /**
150  * @see View::clearAllValues()
151  */
152  public function clearAllValues() {
153  $this->view->clearAllAssign();
154  }
155 
156  /**
157  * @see View::display()
158  */
159  public function render($tplFile, $cacheId=null, $cacheLifetime=null, $display=true) {
160  $oldCacheLifetime = $this->view->cache_lifetime;
161  if ($cacheLifetime !== null) {
162  $this->view->cache_lifetime = intval($cacheLifetime);
163  }
164  if ($display) {
165  $result = $this->view->display($tplFile, $cacheId);
166  }
167  else {
168  $result = $this->view->fetch($tplFile, $cacheId);
169  }
170  if ($cacheLifetime !== null) {
171  $this->view->cache_lifetime = $oldCacheLifetime;
172  }
173  return $result;
174  }
175 
176  /**
177  * @see View::clearCache()
178  */
179  public static function clearCache($tplFile=null, $cacheId=null) {
180  if (self::$sharedView == null) {
181  self::$sharedView = ObjectFactory::getInstance('view');
182  }
183  if ($tplFile) {
184  $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
185  return $tpl->smarty->clearCache($tplFile, $cacheId);
186  }
187  else {
188  $fileUtil = new FileUtil();
189  $fileUtil->emptyDir(self::$sharedView->view->getCompileDir());
190  return self::$sharedView->view->clearAllCache();
191  }
192  }
193 
194  /**
195  * @see View::isCached()
196  * Returns also false, if caching is disabled to make sure that
197  * views get regenerated every time when expected.
198  */
199  public static function isCached($tplFile, $cacheId=null) {
200  if (self::$sharedView == null) {
201  self::$sharedView = ObjectFactory::getInstance('view');
202  }
203  $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
204  return $tpl->isCached();
205  }
206 
207  /**
208  * @see View::getCacheDate()
209  */
210  public static function getCacheDate($tplFile, $cacheId=null) {
211  if (!self::isCached($tplFile, $cacheId)) {
212  return null;
213  }
214  $tpl = self::$sharedView->view->createTemplate($tplFile, $cacheId);
215  return (new \DateTime())->setTimeStamp($tpl->cached->timestamp);
216  }
217 
218  /**
219  * @see View::getTemplate()
220  */
221  public static function getTemplate($controller, $context, $action) {
222  $config = ObjectFactory::getInstance('configuration');
223  if (self::$actionKeyProvider == null) {
224  self::$actionKeyProvider = new ConfigActionKeyProvider($config, 'views');
225  }
226 
227  $actionKey = ActionKey::getBestMatch(self::$actionKeyProvider, $controller, $context, $action);
228  if (self::$logger->isDebugEnabled()) {
229  self::$logger->debug('SmartyView::getTemplate: '.$controller."?".$context."?".$action.' -> '.$actionKey);
230  }
231  // get corresponding view
232  try {
233  $view = $config->getValue($actionKey, 'views', false);
234  }
235  catch (\Exception $ex) {
236  return false;
237  }
238  return $view;
239  }
240 }
241 ?>
setCacheDir($cacheDir)
Set the caching directory If not existing, the directory will be created relative to WCMF_BASE.
Definition: SmartyView.php:101
setCacheLifetime($cacheLifeTime)
Set the time a view should be cached.
Definition: SmartyView.php:92
setPluginsDir($pluginsDir)
Set a additional plugins directory.
Definition: SmartyView.php:114
static getBestMatch(ActionKeyProvider $actionKeyProvider, $resource, $context, $action)
Get an action key that matches a given combination of resource, context, action best.
Definition: ActionKey.php:55
static getTemplate($controller, $context, $action)
Definition: SmartyView.php:221
View is used by Controller to handle the view presentation in MVC pattern.
Definition: SmartyView.php:36
setCompileCheck($compileCheck)
Set whether the view should check for template modifications or not.
Definition: SmartyView.php:76
static isCached($tplFile, $cacheId=null)
Definition: SmartyView.php:199
ConfigurationException signals an exception in the configuration.
static getCacheDate($tplFile, $cacheId=null)
Definition: SmartyView.php:210
setCaching($caching)
Set whether views should be cached.
Definition: SmartyView.php:84
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:37
ConfigActionKeyProvider searches for action keys in the application configuration.
static getInstance($name, $dynamicConfiguration=[])
View defines the interface for all view implementations.
Definition: View.php:18
An action key is a combination of a resource, context and action that is represented as a string.
Definition: ActionKey.php:22
setOutputFilter($outputFilter)
Set additional output filters.
Definition: SmartyView.php:122
static clearCache($tplFile=null, $cacheId=null)
Definition: SmartyView.php:179
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...
render($tplFile, $cacheId=null, $cacheLifetime=null, $display=true)
Definition: SmartyView.php:159