SmartyView.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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  $this->_view->setTemplateDir(WCMF_BASE);
65  $cacheDir = session_save_path().DIRECTORY_SEPARATOR;
66  $this->_view->setCompileDir($cacheDir.'templates_c/');
67  $this->_view->setCacheDir($cacheDir.'cache/');
68  }
69 
70  /**
71  * Set whether the view should check for
72  * template modifications or not
73  * @param $compileCheck Boolean
74  */
75  public function setCompileCheck($compileCheck) {
76  $this->_view->compile_check = $compileCheck;
77  }
78 
79  /**
80  * Set whether views should be cached
81  * @param $caching Boolean
82  */
83  public function setCaching($caching) {
84  $this->_view->caching = $caching;
85  }
86 
87  /**
88  * Set the time a view should be cached
89  * @param $cacheLifeTime Integer (seconds)
90  */
91  public function setCacheLifetime($cacheLifeTime) {
92  $this->_view->cache_lifetime = $cacheLifeTime;
93  }
94 
95  /**
96  * Set the caching directory
97  * If not existing, the directory will be created relative to WCMF_BASE.
98  * @param $cacheDir String
99  */
100  public function setCacheDir($cacheDir) {
101  $this->_view->setCompileDir(WCMF_BASE.$cacheDir.'templates_c/');
102  $this->_view->setCacheDir(WCMF_BASE.$cacheDir.'cache/');
103 
104  $fileUtil = new FileUtil();
105  $fileUtil->mkdirRec($this->_view->getCompileDir());
106  $fileUtil->mkdirRec($this->_view->getCacheDir());
107  }
108 
109  /**
110  * Set a additional plugins directory
111  * @param $pluginsDir Directory relative to WCMF_BASE
112  */
113  public function setPluginsDir($pluginsDir) {
114  $this->_view->addPluginsDir(WCMF_BASE.$pluginsDir);
115  }
116 
117  /**
118  * Set additional output filters
119  * @param $outputFilter
120  */
121  public function setOutputFilter($outputFilter) {
122  foreach ($outputFilter as $filter) {
123  $this->_view->loadFilter('output', $filter);
124  }
125  }
126 
127  /**
128  * @see View::setValue()
129  */
130  public function setValue($name, $value) {
131  $this->_view->assign($name, $value);
132  }
133 
134  /**
135  * @see View::getValue()
136  */
137  public function getValue($name) {
138  return $this->_view->getTemplateVars($name);
139  }
140 
141  /**
142  * @see View::getValues()
143  */
144  public function getValues() {
145  return $this->_view->getTemplateVars();
146  }
147 
148  /**
149  * @see View::clearAllValues()
150  */
151  public function clearAllValues() {
152  $this->_view->clearAllAssign();
153  }
154 
155  /**
156  * @see View::display()
157  */
158  public function render($tplFile, $cacheId=null, $display=true) {
159  if ($display) {
160  $this->_view->display($tplFile, $cacheId);
161  }
162  else {
163  return $this->_view->fetch($tplFile, $cacheId);
164  }
165  }
166 
167  /**
168  * @see View::clearCache()
169  */
170  public static function clearCache() {
171  if (self::$_sharedView == null) {
172  self::$_sharedView = ObjectFactory::getInstance('view');
173  }
174  return self::$_sharedView->_view->clearAllCache();
175  }
176 
177  /**
178  * @see View::isCached()
179  * Returns also false, if caching is disabled to make sure that
180  * views get regenerated every time when expected.
181  */
182  public static function isCached($tplFile, $cacheId=null) {
183  if (self::$_sharedView == null) {
184  self::$_sharedView = ObjectFactory::getInstance('view');
185  }
186  return (self::$_sharedView->_view->caching && self::$_sharedView->_view->isCached($tplFile, $cacheId));
187  }
188 
189  /**
190  * @see View::getTemplate()
191  */
192  public static function getTemplate($controller, $context, $action) {
193  $config = ObjectFactory::getInstance('configuration');
194  if (self::$_actionKeyProvider == null) {
195  self::$_actionKeyProvider = new ConfigActionKeyProvider($config, 'views');
196  }
197 
198  $actionKey = ActionKey::getBestMatch(self::$_actionKeyProvider, $controller, $context, $action);
199  if (self::$_logger->isDebugEnabled()) {
200  self::$_logger->debug('SmartyView::getTemplate: '.$controller."?".$context."?".$action.' -> '.$actionKey);
201  }
202  // get corresponding view
203  try {
204  $view = $config->getValue($actionKey, 'views', false);
205  } catch (\Exception $ex) {
206  throw new ConfigurationException("No view defined for ".$controller."?".$context."?".$action);
207  }
208  return $view;
209  }
210 }
211 ?>
setCaching($caching)
Set whether views should be cached.
Definition: SmartyView.php:83
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
setPluginsDir($pluginsDir)
Set a additional plugins directory.
Definition: SmartyView.php:113
static getLogger($name)
Get the logger with the given name.
Definition: LogManager.php:35
View is used by Controller to handle the view presentation in MVC pattern.
Definition: SmartyView.php:36
View defines the interface for all view implementations.
Definition: View.php:18
static getInstance($name, $dynamicConfiguration=array())
static getTemplate($controller, $context, $action)
Definition: SmartyView.php:192
ConfigActionKeyProvider searches for action keys in the application configuration.
render($tplFile, $cacheId=null, $display=true)
Definition: SmartyView.php:158
static isCached($tplFile, $cacheId=null)
Definition: SmartyView.php:182
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
setCompileCheck($compileCheck)
Set whether the view should check for template modifications or not.
Definition: SmartyView.php:75
setOutputFilter($outputFilter)
Set additional output filters.
Definition: SmartyView.php:121
ConfigurationException signals an exception in the configuration.
setCacheLifetime($cacheLifeTime)
Set the time a view should be cached.
Definition: SmartyView.php:91
setCacheDir($cacheDir)
Set the caching directory If not existing, the directory will be created relative to WCMF_BASE...
Definition: SmartyView.php:100