-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVolpi.php
98 lines (84 loc) · 2.56 KB
/
Volpi.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
<?php
define('TPL_ROOT', ROOT.'/templates');
class Volpi implements ArrayAccess
{
private $template_vars = array();
private $content;
public function offsetSet($offset, $value)
{
if (is_null($offset)) {
$this->template_vars[] = $value;
} else {
$this->template_vars[$offset] = $value;
}
}
public function offsetExists($offset)
{
return isset($this->template_vars[$offset]);
}
public function offsetUnset($offset)
{
unset($this->template_vars[$offset]);
}
public function offsetGet($offset)
{
return isset($this->template_vars[$offset]) ? $this->template_vars[$offset] : null;
}
public static function getPath($path)
{
$tpl_path = str_replace('.', DS, $path);
$extention = '.tpl';
return TPL_ROOT.DS.$tpl_path.$extention;
}
private function insertSections()
{
$pattern = '~{{\s*include\(\'(.+?)\'\)\s*}}~';
function getSection($matches)
{
$path = array_pop($matches);
$file_path = Volpi::getPath($path);
return file_get_contents($file_path);
}
$this->content = preg_replace_callback($pattern, "getSection", $this->content);
}
private function insertVars()
{
foreach ($this->template_vars as $key => $variable)
{
if(!is_array($variable))
{
$pattern = '/{{\s*'.$key.'\s*}}/';
$replacement = "<?php echo \"{$variable}\"; ?>";
$this->content = preg_replace($pattern, $replacement, $this->content);
}
}
}
private function insertCycles()
{
$patterns = array(
'/{{\s*foreach\s*(.+?)\s*as\s*(.+?)\s*}}/' =>
'<?php foreach( $this->template_vars[\'$1\'] as $$2): ?>',
'/{{\s*endforeach\s*}}/' =>
'<?php endforeach; ?>',
'/{{\s*(.+?)\s*}}/' =>
'<?php echo $$1; ?>',
);
foreach($patterns as $pattern => $replacement)
{
$this->content = preg_replace($pattern, $replacement, $this->content);
}
}
private function compile($template)
{
$this->content = file_get_contents($template);
$this->insertSections();
$this->insertVars();
$this->insertCycles();
}
public function show($template)
{
$tpl_path = self::getPath($template);
$this->compile($tpl_path);
eval('?>'.$this->content);
}
}