nyroFwk  0.2
module/abstract.class.php
Go to the documentation of this file.
1 <?php
10 abstract class module_abstract extends object {
11 
17  protected $prmExec;
18 
24  protected $tpl;
25 
26  protected function afterInit() {
27  if ($this->cfg->forceSecure)
29 
30  if (!$this->cfg->enabled)
31  throw new module_exception('Module '.$this->getName().' disabled.');
32  }
33 
40  final public function exec(array $prm=array()) {
41  $this->prmExec = array_merge(array(
42  'module'=>$this->getName(),
43  'action'=>'index',
44  'param'=>'',
45  'paramA'=>null,
46  'prefix'=>null),
47  $prm);
48 
49  $this->prmExec['prefix'] = null;
50 
51  if (array_key_exists(NYROENV, $this->cfg->basicPrefixExec) &&
52  in_array($this->prmExec['action'], $this->cfg->getInArray('basicPrefixExec', NYROENV)))
53  $this->prmExec['prefix'] = ucfirst(NYROENV);
54  else if ($this->cfg->prefixExec && !in_array($this->prmExec['action'], $this->cfg->noPrefixExec))
55  $this->prmExec['prefix'] = $this->cfg->prefixExec;
56 
57  $this->beforeExec($prm);
58 
59  if (!$this->cfg->render)
60  security::getInstance()->check($this->prmExec);
61 
62  $fctName = ($this->cfg->render? 'render' : 'exec').$this->prmExec['prefix'].ucfirst($this->prmExec['action']);
63  if (!method_exists($this, $fctName))
64  response::getInstance()->error();
65 
66  $this->setViewAction($this->prmExec['action']);
67 
68  $param = is_array($this->prmExec['paramA'])? $this->prmExec['paramA'] : request::parseParam($this->prmExec['param']);
69 
70  $tags = $this->cfg->cacheTags;
71  $search = array('/', '<', '>');
72  $replace = array('', '', '');
73  if (is_array($this->prmExec['paramA']))
74  foreach($this->prmExec['paramA'] as $k=>$v) {
75  if (is_object($v)) {
76  if (is_callable(array($v, '__toString')))
77  $tags[] = $k.'='.$v;
78  else
79  $tags[] = $k.'='.get_class($v);
80  } elseif (!is_numeric($k))
81  $tags[] = $k.'='.str_replace($search, $replace, $v);
82  else
83  $tags[] = str_replace($search, $replace, $v);
84  }
85 
86  $paramTpl = array();
87  foreach($param as $k=>$v) {
88  if (is_object($v)) {
89  if (is_callable(array($v, '__toString')))
90  $paramTpl[] = $k.'='.$v;
91  else
92  $paramTpl[] = $k.'='.get_class($v);
93  } else
94  $paramTpl[] = $k.':'.$v;
95  }
96 
97  $conf = array(
98  'layout'=>$this->cfg->layout,
99  'module'=>$this->getName(),
100  'action'=>$this->cfg->viewAction,
101  'param'=>implode(',', $paramTpl),
102  'cache'=>array_merge(array(
103  'enabled'=>$this->isCacheEnabled(),
104  'serialize'=>false,
105  'tags'=>$tags,
106  'request'=>array('uri'=>false, 'meth'=>array())
107  ), $this->cfg->cache)
108  );
109  $this->tpl = factory::get('tpl', array_merge_recursive($conf, $this->cfg->tplPrm));
110  $this->tpl->getCfg()->layout = $this->cfg->layout;
111  $this->tpl->getCfg()->module = $this->getName();
112  $this->tpl->getCfg()->action = $this->cfg->viewAction;
113  $this->tpl->getCfg()->default = $this->cfg->viewAction;
114 
115  $this->prmExec['callbackPrm'] = array(
116  'fctName'=>$fctName,
117  'fctNameParam'=>$param,
118  'prm'=>$prm
119  );
120  $this->middleExec($prm);
121  }
122 
123  public function callbackTpl(array $prm) {
124  $this->{$prm['fctName']}($prm['fctNameParam']);
125  $this->afterExec($prm['prm']);
126  return $this->getViewAction();
127  }
128 
129  public function getResponse() {
130  return $this->tpl->getResponseProxy();
131  }
132 
136  protected function beforeExec($realExec) {}
137 
141  protected function middleExec($realExec) {}
142 
146  protected function afterExec($realExec) {}
147 
153  protected function getViewAction() {
154  return $this->cfg->viewAction;
155  }
156 
162  protected function setViewAction($action) {
163  $this->cfg->viewAction = $action;
164  if ($this->tpl) {
165  $this->tpl->getCfg()->action = $this->cfg->viewAction;
166  $this->tpl->getCfg()->default = $this->cfg->viewAction;
167  }
168  }
169 
176  protected function getViewVar($name) {
177  return $this->tpl->get($name);
178  }
179 
186  protected function setViewVar($name, $value) {
187  $this->tpl->set($name, $value);
188  }
189 
195  protected function setViewVars(array $values) {
196  $this->tpl->setA($values);
197  }
198 
204  public function publish(array $prm = array()) {
205  if (!$this->cfg->viewAction)
206  return null;
207  $this->preFetch();
208  return $this->tpl->fetch(array_merge(array(
209  'callback'=>array($this, 'callbackTpl'),
210  'callbackPrm'=>$this->prmExec['callbackPrm'],
211  ), $prm));
212  }
213 
218  protected function preFetch() {}
219 
225  protected function isCacheEnabled() {
226  $url = $this->prmExec;
227  if (utils::isContained($url, $this->cfg->noCache))
228  return false;
229  if (utils::isContained($url, $this->cfg->forceCache))
230  return true;
231  return $this->cfg->defaultCache;
232  }
233 
239  protected function addCacheTag($val) {
240  $this->cfg->setInArrayA('cacheTags', array($val));
241  if ($this->tpl) {
242  $this->tpl->getCfg()->setInarray('cache', 'tags', $this->cfg->cacheTags);
243  }
244  }
245 
251  public function getName() {
252  return utils::getModuleName(get_class($this));
253  }
254 
255 }
static getInstance()
set($name, $value)
Definition: tpl.class.php:55
exec(array $prm=array())
static parseParam($param)
setA(array $values)
Definition: tpl.class.php:65
publish(array $prm=array())
getResponseProxy()
Definition: tpl.class.php:35
fetch(array $prm=array())
Definition: tpl.class.php:83
setViewVar($name, $value)
static isContained(array $url, array $checks)
static getInstance()
static getModuleName($className)
get($name)
Definition: tpl.class.php:45
setViewVars(array $values)
static forceSecure()
static get($className, array $cfg=array())
const NYROENV
Definition: admin.php:8
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13