-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.php
151 lines (130 loc) · 4.07 KB
/
Template.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
<?php
namespace Jacere\Skhema;
use Jacere\Bramble\Core\Serialization\IPhpSerializable;
use Jacere\Bramble\Core\Serialization\PhpSerializationMap;
class Template implements IPhpSerializable {
private $m_name;
private $m_root;
private $m_parent;
private $m_includes;
private $m_filters;
/**
* @param Node $node
* @param string $name
* @param string $parent
* @param string[] $includes
* @param string[] $filters
*/
function __construct(Node $node, $name = NULL, $parent = NULL, array $includes = NULL, array $filters = NULL) {
$this->m_name = ($name != NULL ? $name : $node->name());
$this->m_root = $node;
if ($includes) {
$this->m_parent = $parent;
$this->m_includes = $includes;
$this->m_filters = $filters;
}
else {
// check first child for inheritance
if (($firstChild = $node->firstToken()) && ($firstChild->type() === TokenType::T_INHERIT)) {
$this->m_parent = $firstChild->name();
}
// include dependency names
$this->m_includes = array_map('strval', iterator_to_array($node->GetChildrenByType(TokenType::T_INCLUDE, true)));
/** @var EvalNameToken $token */
$this->m_filters = [];
foreach ($node->GetChildrenByClass(EvalNameToken::class, true) as $token) {
foreach ($token->filters() as $filter) {
$this->m_filters[$filter] = $filter;
}
}
}
}
public function finalize($templates) {
$root = $this->m_root;
// derived templates can only include definitions (at the top level)
// definitions can only be included in derived templates
if ($this->m_parent != NULL) {
$definitions = iterator_to_array($root->GetChildrenByType(TokenType::T_DEFINE));
$definitions = array_combine(array_map('strval', $definitions), $definitions);
// replace the contents of the root with the parent contents
// go through children and fill in definitions
$this->m_root->replaceContents($templates[$this->m_parent]->m_root, $definitions);
}
}
public function evaluate(TemplateManager $manager, array $context, array $current = NULL) {
// check filters
foreach ($this->m_filters as $filter) {
if (!$manager->filter($filter)) {
throw new \Exception("Undefined filter `$filter` in template `{$this->m_name}`");
}
}
// search the inheritance hierarchy
$t = $this;
while ($t->m_parent) {
$t = $t->m_parent;
$t = $manager->template($t);
if (isset($context[$t->m_name])) {
// copy definitions to corresponding child
if (!isset($context[$this->m_name])) {
$context[$this->m_name] = [];
}
foreach ($context[$t->m_name] as $parentKey => $parentVal) {
if (!isset($context[$this->m_name][$parentKey])) {
$context[$this->m_name][$parentKey] = $parentVal;
}
}
}
}
if ($this->anonymous()) {
$rootName = $this->m_root->name();
if ($current != NULL && isset($current[$rootName])) {
$current = $current[$rootName];
// check for int key to determine if this is a list
// (this is a hack)
reset($current);
if (count($current) && !is_int(key($current))) {
$current = [$current];
}
foreach ($current as $key => $row) {
$row['__iteration'] = $key;
$this->m_root->evaluate($manager, $context, $row);
}
}
else {
// how to define optional sources?
//die(sprintf('No mapping for source "%s".', $this->m_name));
//echo $this->m_name."<br>\n";
}
}
else {
if ($context != NULL && isset($context[$this->m_name])) {
$current = $context[$this->m_name];
}
else {
//$current = NULL;
}
$this->m_root->evaluate($manager, $context, $current);
}
}
public function name() {
return $this->m_name;
}
public function anonymous() {
return ($this->m_root->type() === TokenType::T_SOURCE);
}
public function dependencies() {
return array_filter(array_merge((array)$this->m_parent, $this->m_includes));
}
public function __toString() {
return $this->m_name;
}
public function phpSerializable(PhpSerializationMap $map) {
return $map->newObject($this, [
$this->m_root,
$this->m_name,
$this->m_parent,
$this->m_includes,
$this->m_filters,
]);
}
}