/
sgView.class.php
103 lines (88 loc) · 2.55 KB
/
sgView.class.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
<?php
/**
* View - provides a wrapper for Twig
*/
class sgView
{
private static $instance;
protected
$twig,
$loader,
$view,
$templatePaths;
private function __construct()
{
$this->setPaths();
$this->loader = new Twig_Loader_Filesystem($this->templatePaths);
$this->twig = new Twig_Environment($this->loader, array(
'debug' => sgConfiguration::get('settings.debug'),
'cache' => sgConfiguration::get('settings.cache_dir') . '/templates',
'auto_reload' => !sgConfiguration::get('settings.cache_templates'), // TODO maybe this should instead dictate the cache setting
));
foreach (sgConfiguration::get('settings.enabled_twig_extensions') as $extension)
{
$this->twig->addExtension(new $extension());
}
// foreach (sgAutoloader::getPaths() as $class => $path)
// {
// if (strpos($class, 'Twig_Extension') === 0)
// {
// $this->twig->addExtension(new $class());
// }
// }
}
public static function getInstance()
{
if (!isset(self::$instance)) {
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
public function __clone()
{
trigger_error('Only one instance of a singleton is allowed.', E_USER_ERROR);
}
public function setPaths(array $templatePaths = array(), $overridePaths = FALSE)
{
if (!$overridePaths)
{
$this->templatePaths[] = sgConfiguration::getRootDir() . '/views';
$enabledPlugins = sgConfiguration::get('settings.enabled_plugins', array());
foreach ($enabledPlugins as $plugin)
{
if (is_dir(sgConfiguration::getRootDir() . "/plugins/$plugin/views"))
{
$this->templatePaths[] = sgConfiguration::getRootDir() . "/plugins/$plugin/views";
}
}
$this->templatePaths[] = dirname(__FILE__) . '/views';
}
if (isset($this->twig))
{
$this->twig->getLoader()->setPaths($this->templatePaths);
}
}
public function getTwig()
{
return $this->twig;
}
//wonder if removing some of the dependency injection from twig is really a good idea
public function loadTemplate($template)
{
$this->view = $this->twig->loadTemplate($template);
return $this->view;
}
public function getView()
{
return $this->view;
}
public function render($variables = array())
{
if (!$this->view)
{
throw new Exception('No view is defined. You must call loadTemplate() before you can render a view.');
}
return $this->view->render($variables);
}
}