-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDreawTemplateParser.php
157 lines (120 loc) · 3.68 KB
/
DreawTemplateParser.php
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
class DreawTemplateParser {
private $_DB, $_SESS;
protected $_template;
protected $_loop_cache;
protected $_tags_1;
protected $_tags_2;
protected $_tags_3;
protected $_url = 'http://www.kat.dreaw.cz/template';
protected $_suffix = '.phtml';
protected $_only_HTML_parse;
public function __construct($template_file, $alternative_url = null) {
//$this->_DB = new DreawDB();
//$this->_SESS = new DreawSession();
/*if (file_exists($template_file . $this->_suffix)) {*/
$this->_template = file_get_contents($this->_url . '/' . $template_file . $this->_suffix);
/*}
else {
throw new DreawHandler('Master template not found!');
}*/
}
public function addTags_1($tags = array()) {
$this->_tags_1 = $tags;
return $this;
}
public function addTags_2($tags = array()) {
$this->_tags_2 = $tags;
return $this;
}
public function addTags_3($tags = array()) {
$this->_tags_3 = $tags;
return $this;
}
public function parse() {
if ($this->_isData()) {
if ($this->_isData(3)) {
foreach ($this->_tags_3 as $key => $data_value) {
if (strpos($this->_template, $key)) { // if is tag in template
$repeat = $this->_command($this->_template, '{' . $key . '|foreach}', '{' . $key . '|/foreach}');
$foreach_in_template = '{' . $key . '|foreach}' . $repeat . '{' . $key . '|/foreach}';
foreach ($data_value as $row) {
$loop_row = $repeat;
foreach ($row as $data => $value) {
$loop_row = $this->_commandEvaluation($data, $value, $loop_row);
}
$this->_loop_cache .= $loop_row;
}
$this->_template = str_replace($foreach_in_template, $this->_loop_cache, $this->_template);
}
}
}
}
return $this;
}
public function view() {
print $this->_template;
return $this;
}
protected function _isData($num = 4) {
if ($num == 1) {
if (!empty($this->_tags_1)) {
return true;
}
return false;
}
else if ($num == 2) {
if (!empty($this->_tags_2)) {
return true;
}
return false;
}
else if ($num == 3) {
if (!empty($this->_tags_3)) {
return true;
}
return false;
}
else if ($num == 4) {
if (!empty($this->_tags_1) || !empty($this->_tags_2) || !empty($this->_tags_3)) {
return true;
}
return false;
}
else {
throw new ErrorHandler('ATTACK!');
}
}
protected function _commandEvaluation($tag, $tag_data, $source) {
if (strpos($source, '{' . $tag . '}')) {
$source = str_replace('{' . $tag . '}', $tag_data, $source);
}
return $source;
}
protected function _command($string, $start, $end) {
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
public function debugStart ($what) {
if ($what == 'full') {
$this->_fullTime = microtime(true);
}
else {
$this->parseTime = microtime(true);
}
return $this;
}
public function debugEnd ($what) {
if ($what == 'full') {
echo '<p><strong>Doba potřebná ke kompletnímu zobrazení stránky: ' . number_format(microtime(false) - $this->fullTime, 5) . ' s</strong></p>';
}
else {
echo '<p><strong>Doba potřebná k vykreslení parserem: ' . number_format(microtime(true) - $this->parseTime, 5) . ' s</strong></p>';
}
return $this;
}
}