forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTemplate.php
203 lines (186 loc) · 5.37 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* hold PMA\libraries\Template class
*
* @package PMA\libraries
*/
namespace PMA\libraries;
use Twig_Environment;
use Twig_Loader_Filesystem;
use PMA\libraries\twig\I18nExtension;
use PMA\libraries\twig\UrlExtension;
use PMA\libraries\twig\UtilExtension;
/**
* Class Template
*
* Handle front end templating
*
* @package PMA\libraries
*/
class Template
{
/**
* Name of the template
*/
protected $name = null;
/**
* Data associated with the template
*/
protected $data;
/**
* Helper functions for the template
*/
protected $helperFunctions;
/**
* Twig environment
*/
protected $twig;
const BASE_PATH = 'templates/';
/**
* Template constructor
*
* @param string $name Template name
* @param array $data Variables to be provided to the template
* @param array $helperFunctions Helper functions to be used by template
*/
protected function __construct($name, $data = array(), $helperFunctions = array())
{
$this->name = $name;
$this->data = $data;
$this->helperFunctions = $helperFunctions;
$loader = new Twig_Loader_Filesystem(static::BASE_PATH);
$this->twig = new Twig_Environment($loader, array(
'auto_reload' => true,
'cache' => CACHE_DIR . 'twig',
'debug' => false,
));
$this->twig->addExtension(new I18nExtension());
$this->twig->addExtension(new UrlExtension());
$this->twig->addExtension(new UtilExtension());
}
/**
* Template getter
*
* @param string $name Template name
* @param array $data Variables to be provided to the template
* @param array $helperFunctions Helper functions to be used by template
*
* @return Template
*/
public static function get($name, $data = array(), $helperFunctions = array())
{
return new Template($name, $data, $helperFunctions);
}
/**
* Adds more entries to the data for this template
*
* @param array|string $data containing data array or data key
* @param string $value containing data value
*
* @return void
*/
public function set($data, $value = null)
{
if(is_array($data) && ! $value) {
$this->data = array_merge(
$this->data,
$data
);
} else if (is_string($data)) {
$this->data[$data] = $value;
}
}
/**
* Adds a function for use by the template
*
* @param string $funcName function name
* @param callable $funcDef function definition
*
* @return void
*/
public function setHelper($funcName, $funcDef)
{
if (! isset($this->helperFunctions[$funcName])) {
$this->helperFunctions[$funcName] = $funcDef;
} else {
throw new \LogicException(
'The function "' . $funcName . '" is already associated with the template.'
);
}
}
/**
* Removes a function
*
* @param string $funcName function name
*
* @return void
*/
public function removeHelper($funcName)
{
if (isset($this->helperFunctions[$funcName])) {
unset($this->helperFunctions[$funcName]);
} else {
throw new \LogicException(
'The function "' . $funcName . '" is not associated with the template.'
);
}
}
/**
* Magic call to locally inaccessible but associated helper functions
*
* @param string $funcName function name
* @param array $arguments function arguments
*
* @return mixed
*/
public function __call($funcName, $arguments)
{
if (isset($this->helperFunctions[$funcName])) {
return call_user_func_array($this->helperFunctions[$funcName], $arguments);
} else {
throw new \LogicException(
'The function "' . $funcName . '" is not associated with the template.'
);
}
}
/**
* Render template
*
* @param array $data Variables to be provided to the template
* @param array $helperFunctions Helper functions to be used by template
*
* @return string
*/
public function render($data = array(), $helperFunctions = array())
{
$template = static::BASE_PATH . $this->name;
if (file_exists($template . '.twig')) {
$this->set($data);
return $this->twig->load($this->name . '.twig')
->render($this->data);
}
$template = $template . '.phtml';
try {
$this->set($data);
$this->helperFunctions = array_merge(
$this->helperFunctions,
$helperFunctions
);
extract($this->data);
ob_start();
if (file_exists($template)) {
include $template;
} else {
throw new \LogicException(
'The template "' . $template . '" not found.'
);
}
$content = ob_get_clean();
return $content;
} catch (\LogicException $e) {
ob_end_clean();
throw new \LogicException($e->getMessage());
}
}
}