1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\ThemeEngine; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Anax base class for wrapping sessions. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CThemeBasic implements IThemeEngine, \Anax\DI\IInjectionAware |
10
|
|
|
{ |
11
|
|
|
use \Anax\TConfigure, |
12
|
|
|
\Anax\DI\TInjectionAware; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Properties |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
protected $data = []; // Array with variables to provide to template files. |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Shortcut to set title. |
26
|
|
|
* |
27
|
|
|
* @param string $value of the variable. |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public function setTitle($value) |
32
|
|
|
{ |
33
|
|
|
return $this->setVariable('title', $value); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set a base title which is appended to the page title. |
40
|
|
|
* |
41
|
|
|
* @param string $value of the variable. |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function setBaseTitle($value) |
46
|
|
|
{ |
47
|
|
|
return $this->setVariable('title_append', $value); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set a variable which will be exposed to the template files during render. |
54
|
|
|
* |
55
|
|
|
* @param string $which variable to set value of. |
56
|
|
|
* @param mixed $value of the variable. |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setVariable($which, $value) |
61
|
|
|
{ |
62
|
|
|
$this->data[$which] = $value; |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get a value of a variable which will be exposed to the template files |
70
|
|
|
* during render. |
71
|
|
|
* |
72
|
|
|
* @param string $which variable to get value of. |
73
|
|
|
* |
74
|
|
|
* @return mixed as value of variable, or null if value is not set. |
75
|
|
|
*/ |
76
|
|
|
public function getVariable($which) |
77
|
|
|
{ |
78
|
|
|
if (isset($this->data[$which])) { |
79
|
|
|
return $this->data[$which]; |
80
|
|
|
} elseif (isset($this->config['data'])) { |
81
|
|
|
return $this->config['data'][$which]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Add a stylesheet. |
91
|
|
|
* |
92
|
|
|
* @param string $uri to add. |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function addStylesheet($uri) |
97
|
|
|
{ |
98
|
|
|
$this->config['data']['stylesheets'][] = $uri; |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add a javascript asset. |
106
|
|
|
* |
107
|
|
|
* @param string $uri to add. |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function addJavaScript($uri) |
112
|
|
|
{ |
113
|
|
|
$this->config['data']['javascript_include'][] = $uri; |
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Render the theme by applying the variables onto the template files. |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function render() |
125
|
|
|
{ |
126
|
|
|
// Prepare details |
127
|
|
|
$path = $this->config['settings']['path']; |
128
|
|
|
$name = $this->config['settings']['name'] . '/'; |
129
|
|
|
$template = 'index.tpl.php'; |
130
|
|
|
$functions = 'functions.php'; |
131
|
|
|
|
132
|
|
|
// Include theme specific functions file |
133
|
|
|
$file = $path . $name . $functions; |
134
|
|
|
if (is_readable($file)) { |
135
|
|
|
include $file; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
// Create views for regions, from config-file |
139
|
|
|
if (isset($this->config['views'])) { |
140
|
|
|
foreach ($this->config['views'] as $view) { |
141
|
|
|
$this->di->views->add($view['template'], $view['data'], $view['region'], $view['sort']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Sen response headers, if any. |
146
|
|
|
$this->di->response->sendHeaders(); |
147
|
|
|
|
148
|
|
|
// Create a view to execute the default template file |
149
|
|
|
$tpl = $path . $name . $template; |
150
|
|
|
$data = array_merge($this->config['data'], $this->data); |
151
|
|
|
$view = $this->di->get('view'); |
152
|
|
|
$view->set($tpl, $data); |
153
|
|
|
$view->setDI($this->di); |
154
|
|
|
$view->render(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|