-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.php
136 lines (107 loc) · 3.74 KB
/
Parser.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
<?php
namespace Spike;
use \Spike\Lexema as Lexema;
require_once "Lexema.php";
require_once "Lexema/Tag.php";
require_once "Lexema/Text.php";
require_once "Lexema/Tag/Variable.php";
require_once "Lexema/Tag/Callback.php";
require_once "Lexema/Tag/Condition.php";
require_once "Lexema/Tag/Loop.php";
require_once "Lexema/Tag/Assign.php";
require_once "Lexema/Params.php";
require_once "DataStack.php";
require_once "Timer.php";
class Parser {
/**
* Данные складываются в стек. Это позволяет вложенным шаблонам получать доступ к переменным более верхнего уровня.
* @var DataStack
*/
private $dataStack;
public function parse($content, $data) {
$content = $this->stripComments($content);
$this->getDataStack()->pushData($data);
$position = 0;
$stack = array();
\Spike\Timer::start("Поиск лексем");
while(!$this->isEof($content, $position)) {
$lexema = $this->nextLexemma($content, $position);
if($lexema instanceof \Spike\Lexema\Tag && $lexema->isCloseTag()) {
/* ищем открывающий тег */
$innerTags = array();
while(($l = array_pop($stack)) != null) {
if($l instanceof \Spike\Lexema\Tag && $l->getName() == $lexema->getOpenTagName() && !$l->isClosed()) {
// нашли открывающий тег
$l->setTags(array_reverse($innerTags));
$l->setBody(substr($content, $l->getPosition(), $position - $l->getPosition() - strlen($lexema->getContent())));
$l->close();
array_push($stack, $l);
break;
}
$innerTags[] = $l;
}
// @TODO Проверить, найден ли открывающий тег. Если нет - бросить исключение.
} else {
array_push($stack, $lexema);
}
}
\Spike\Timer::stop();
$html = "";
$data = $this->getDataStack()->getData();
foreach ($stack as $k => $lexema) {
\Spike\Timer::start("[$k]" . ($lexema->getName() ? $lexema->getName() : 'Text') );
$html .= $lexema->parse($data);
\Spike\Timer::stop();
}
$this->getDataStack()->popData();
return $html;
}
protected function nextLexemma($content, &$position) {
$lex = null;
if(substr($content, $position, 2) == "{{") {
// Начало лексемы
$pos = strpos($content, "}}", $position);
$length = $pos - $position + 2;
$text = substr($content, $position, $length);
$lex = new Lexema\Tag(substr($content, $position, $length));
$position = $pos + 2;
$lex->setPosition($position);
} else {
// Какой-то произвольный HTML код. Просто запоминаем его как текст.
$pos = strpos($content, "{{", $position);
if($pos !== false) {
$length = $pos - $position;
$text = substr($content, $position, $length);
} else {
$text = substr($content, $position);
// смещаем позицию в конец шаблона. Разбор закончен.
$pos = strlen($content);
}
$lex = new Lexema\Text($text);
$position = $pos;
}
return $lex;
}
protected function isEof($content, $position) {
return $position == strlen($content);
}
public function setCallback($callback) {
Lexema::$callback = $callback;
}
public function getCallback() {
return Lexema::$callback;
}
/**
* @return DataStack
*/
public function getDataStack() {
if(empty($this->dataStack)) {
$this->dataStack = new DataStack();
}
return $this->dataStack;
}
protected function stripComments($content) {
return preg_replace('/{{\*.*?\*}}/ms', "", $content);
}
}
?>