php class view protected dir templates directory protected lang langua

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
class view {
protected $dir; //templates directory
protected $lang; //language
protected $authorized;
protected $user;
public $executeTime;
protected function getCache($template) {
return false; //uncomment for developing
if (!isset($_SESSION['cache_' . $template])) return false;
return $_SESSION['cache_' . $template];
}
protected function addCache($template, $content) {
$_SESSION['cache_' . $template] = $content;
}
public function __construct($dir, localization $lang, user $user) {
$this->dir = $dir;
$this->authorized = (bool) $user->authorized;
$this->user = $user;
$this->lang = $lang;
}
public function invoke($template, $params = [], $return = false, $quests = []) { //can be called w/o params
$startTime = microtime(true);
$filename = ROOT . '/' . $this->dir . '/tpl/' . $template . '.tpl';
$lang = $this->lang->getData();
$content = $this->getCache($template);
if (!$content) {
$f = fopen($filename, 'a+');
$content = fread($f, (filesize($filename) > 0 ? filesize($filename) : 1));
$this->addCache($template, $content);
}
$params['dir'] = '/' . $this->dir;
$params['uri'] = urlencode(other::filter($_SERVER['REQUEST_URI']));
$params['http_host'] = $_SERVER['HTTP_HOST'];
foreach ($params as $key => $value) {
$content = str_ireplace('{{' . $key . '}}', $value, $content);
} //applying params
preg_match_all("@{{:([a-z0-9_]+?)}}@sui", $content, $localization);
$localization = $localization[1];
foreach ($localization as $value) {
$content = str_ireplace('{{:' . $value . '}}', $lang[$value], $content);
} //applying lang
$quests['authorized'] = (int) $this->authorized;
preg_match_all("@{\?([a-z0-9_\-]+?)=([a-z0-9_\-]+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
while (!empty($matches[0])) {
for ($i = 0; $i < count($matches[0]); $i++) {
if ($matches[1][$i] == 'access') {
$content = preg_replace("@{\?access=" . $matches[2][$i] . "\?}(((?!{\?.+\?}).)*?){\?\?}@sui", ($this->user->canAccess($matches[2][$i]) ? "$1" : ""), $content);
continue;
}
$content = preg_replace("@{\?" . $matches[1][$i] . "=" . (string) @$quests[$matches[1][$i]] . "\?}(((?!{\?.+\?}).)*?){\?\?}@sui", "$1", $content);
$content = preg_replace("@{\?" . $matches[1][$i] . "=(.+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", "", $content);
}
preg_match_all("@{\?([a-z0-9_\-]+?)=([a-z0-9_\-]+?)\?}(((?!{\?.+\?}).)*?){\?\?}@sui", $content, $matches);
}
if (!$return) echo $content;
$this->executeTime += (microtime(true) - $startTime);
return $content;
}
}
?>