-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathView.php
95 lines (63 loc) · 2.44 KB
/
View.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
<?php
class Wave_View {
private $twig;
private static $_filters = array();
private static $instance = null;
private function __construct(){
$loader = new Twig_Loader_Filesystem(Wave_Config::get('wave')->path->views);
$conf = array('cache' => Wave_Config::get('wave')->view->cache);
if(Wave_Core::$_MODE == Wave_Core::MODE_DEVELOPMENT){
$conf['auto_reload'] = true;
$conf['debug'] = true;
}
$this->twig = new Wave_View_TwigEnvironment($loader, $conf);
$this->twig->addExtension(new Wave_View_TwigExtension());
foreach(self::$_filters as $name => $action)
$this->twig->addFilter($name, $action);
$this->twig->registerUndefinedFilterCallback(function ($name) {
if (function_exists($name)) {
return new Twig_Filter_Function($name);
}
return false;
});
$this->twig->addFilter('last', new Twig_Filter_Function('Wave_Utils::array_peek'));
$this->twig->addFilter('short', new Twig_Filter_Function('Wave_Utils::shorten'));
// global variables
$this->twig->addGlobal('_assets', Wave_Config::get('deploy')->assets);
//$this->twig->addGlobal('_protocol', Wave_Router::$protocol);
$this->twig->addGlobal('_host', Wave_Config::get('deploy')->baseurl);
$this->twig->addGlobal('_mode', Wave_Core::$_MODE);
}
public static function getInstance(){
if(self::$instance === null)
self::$instance = new self();
return self::$instance;
}
public function render($template, $data = array()){
// locate the template file
$template .= Wave_Config::get('wave')->view->extension;
$loaded_template = $this->twig->loadTemplate($template);
return $loaded_template->render($data);
}
public static function registerFilter($filter, $action){
self::$_filters[$filter] = $action;
}
public static function generate(){
// delete caches
$dir_iterator = new RecursiveDirectoryIterator(Wave_Config::get('wave')->view->cache);
$iterator = new RecursiveIteratorIterator($dir_iterator);
foreach($iterator as $cache_file){
@unlink ($cache_file);
}
$self = self::getInstance();
$dir_iterator = new RecursiveDirectoryIterator(Wave_Config::get('wave')->path->views);
$iterator = new RecursiveIteratorIterator($dir_iterator);
$l = strlen(Wave_Config::get('wave')->path->views);
foreach($iterator as $template){
$i = pathinfo($template);
if($i['extension'] != 'phtml') continue;
$self->twig->loadTemplate(substr($template, $l));
}
}
}
?>