nyroFwk  0.2
tpl.class.php
Go to the documentation of this file.
1 <?php
10 class tpl extends object {
11 
17  protected $vars = array();
18 
24  protected $responseProxy;
25 
26  protected function afterInit() {
27  $this->responseProxy = response::getInstance()->getProxy();
28  }
29 
35  public function getResponseProxy() {
36  return $this->responseProxy;
37  }
38 
45  public function get($name) {
46  return isset($this->vars[$name]) ? $this->vars[$name] : null;
47  }
48 
55  public function set($name, $value) {
56  $this->vars[$name] = $value;
57  }
58 
65  public function setA(array $values) {
66  $this->vars = array_merge($this->vars, $values);
67  }
68 
72  public function reset() {
73  $this->vars = array();
74  }
75 
83  public function fetch(array $prm=array()) {
84  $content = null;
85  $cachedContent = false;
86  $cachedLayout = false;
87 
88  $oldProxy = response::getProxy();
89  response::setProxy($this->responseProxy);
90 
91  $cacheResp = null;
92 
93  if ($this->cfg->cache['auto']) {
94  $cache = cache::getInstance(array_merge(array('serialize'=>false), $this->cfg->cache));
95  $cache->get($content, array(
96  'id'=>$this->cfg->module.'-'.$this->cfg->action.'-'.str_replace(':', '..', $this->cfg->param)
97  ));
98  $cacheResp = cache::getInstance($this->cfg->cache);
99  $cacheResp->get($callResp, array(
100  'id'=>$this->cfg->module.'-'.$this->cfg->action.'-'.str_replace(':', '..', $this->cfg->param).'-callResp'
101  ));
102  if (!empty($content)) {
103  $cachedContent = true;
104  $cachedLayout = $this->cfg->cache['layout'];
105  if (!empty($callResp)) {
106  $this->responseProxy->doCalls($callResp);
107  $this->responseProxy->initCall();
108  }
109  }
110  }
111 
112  if (!$cachedContent) {
113  // Nothing was cached
114  $action = $this->cfg->action;
115  if (array_key_exists('callback', $prm))
116  $action = call_user_func($prm['callback'], $prm['callbackPrm']);
117  $file = $this->findTpl($prm, array(
118  'module_'.$this->cfg->module.'_view_'.$action,
119  'module_'.$this->cfg->defaultModule.'_view_'.$this->cfg->default
120  ));
121 
122  if (file::exists($file))
123  $content = $this->_fetch($file);
124  }
125 
126  if ($this->cfg->layout && !$cachedLayout) {
127  // Action layout
128  $file = $this->findTpl($prm, array(
129  'module_'.$this->cfg->module.'_view_'.$this->cfg->action.'Layout',
130  'module_'.$this->cfg->module.'_view_layout'
131  ));
132  if (file::exists($file)) {
133  $this->content = $content;
134  $content = $this->_fetch($file);
135  }
136  if ($this->cfg->cache['auto'] && $this->cfg->cache['layout'])
137  $cache->save();
138  }
139 
140  if ($cacheResp && $this->responseProxy->hasCall()) {
141  $callResp = $this->responseProxy->getCall();
142  $cacheResp->save();
143  }
144 
145  response::setProxy($oldProxy);
146  return $content;
147  }
148 
155  protected function _fetch($file) {
156  $this->set('response', $this->responseProxy);
157  extract($this->vars, EXTR_REFS OR EXTR_OVERWRITE);
158  ob_start();
159  include($file);
160  $contents = ob_get_contents();
161  ob_end_clean();
162  return $contents;
163  }
164 
172  protected function findTpl(array $prm, array $name) {
173  foreach($name as $n) {
174  if ($file = file::nyroExists(array_merge($prm, array('name'=>$n,'type'=>'tpl'))))
175  return $file;
176  }
177  return null;
178  }
179 
188  public function render($prm) {
189  if (!is_array($prm)){
190  $tmp = explode('/', $prm);
191  $prm = array();
192  $prm['module'] = isset($tmp[0]) ? $tmp[0] : null;
193  $prm['action'] = isset($tmp[1]) ? $tmp[1] : null;
194  $prm['param'] = isset($tmp[2]) ? $tmp[2] : null;
195  }
196  $prm = array_merge(array('module'=>$this->cfg->module), $prm);
197  $module = factory::getModule($prm['module'], array('render'=>true));
198  $module->exec($prm);
199  return $module->publish($prm);
200  }
201 
208  function __toString() {
209  return $this->fetch();
210  }
211 
219  public function __get($name) {
220  return $this->get($name);
221  }
222 
230  public function __set($name, $val) {
231  $this->set($name, $val);
232  }
233 
234 }
static getModule($name, array $cfg=array(), &$scaffold=false, $allowScaffold=true)
static getInstance(array $cfg=array())
Definition: cache.class.php:30
static setProxy($proxy)
setA(array $values)
Definition: tpl.class.php:65
getResponseProxy()
Definition: tpl.class.php:35
static exists($file)
Definition: file.class.php:97
__set($name, $val)
Definition: tpl.class.php:230
afterInit()
Definition: tpl.class.php:26
fetch(array $prm=array())
Definition: tpl.class.php:83
__get($name)
Definition: tpl.class.php:219
static getProxy()
render($prm)
Definition: tpl.class.php:188
$responseProxy
Definition: tpl.class.php:24
findTpl(array $prm, array $name)
Definition: tpl.class.php:172
static getInstance()
__toString()
Definition: tpl.class.php:208
static nyroExists($prm)
Definition: file.class.php:137
$vars
Definition: tpl.class.php:17
_fetch($file)
Definition: tpl.class.php:155
reset()
Definition: tpl.class.php:72
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13