-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathRenderer.php
368 lines (325 loc) · 8.32 KB
/
Renderer.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
// vim: foldmethod=marker
/**
* Renderer.php
*
* @author Kazuhiro Hosoi <[email protected]>
* @license http://www.opensource.org/licenses/bsd-license.php The BSD License
* @package Ethna
* @version $Id$
*/
// {{{ Ethna_Renderer
/**
* Template Renderer
*
* @author Kazuhiro Hosoi <[email protected]>
* @access public
* @package Ethna
*/
class Ethna_Renderer
{
/**#@+
* @access private
*/
/** @protected object Ethna_Controller controllerオブジェクト */
protected $controller;
/** @protected object Ethna_Controller controllerオブジェクト($controllerの省略形) */
protected $ctl;
/** @protected array config.phpのレンダラ設定 */
protected $config;
/** @protected array default configuration for the renderer */
protected $config_default = array();
/** @protected string template directory */
protected $template_dir;
/** @protected string template engine */
protected $engine;
/** @protected string template file */
protected $template;
/** @protected string テンプレート変数 */
protected $prop;
/** @protected string レンダラプラグイン(Ethna_Pluginとは関係なし) */
protected $plugin_registry;
/** @protected object Ethna_Logger ログオブジェクト */
protected $logger;
/**
* Ethna_Rendererクラスのコンストラクタ
*
* @access public
*/
public function __construct($controller)
{
$this->controller = $controller;
$this->ctl = $this->controller;
$this->engine = null;
$this->template = null;
$this->prop = array();
$this->plugin_registry = array();
$template_dir = $controller->getTemplatedir();
$this->template_dir = $template_dir;
// load configuration
$config = $this->ctl->getConfig();
$renderer_config = $config->get('renderer');
$this->config = $this->mergeConfig(
$this->config_default,
(isset($renderer_config[$this->getName()]) ? $renderer_config[$this->getName()] : array())
);
$this->logger = $this->controller->getBackend()->getLogger();
}
/**
* getName
*
* @return string renreder name
*/
public function getName()
{
return 'ethna';
}
/**
* getConfig
*
* Get renderer configuration
*
* @return array renderer configuration
*/
public function getConfig()
{
return $this->config;
}
/**
* ビューを出力する
*
* @param string $template テンプレート
* @param bool $capture true ならば出力を表示せずに返す
*
* @access public
*/
public function perform($template = null, $capture = false)
{
if ($template == null && $this->template == null) {
return Ethna::raiseWarning('template is not defined');
}
if ($template != null) {
$this->template = $template;
}
// テンプレートの有無のチェック
if (is_readable($this->template_dir . $this->template) === false) {
return Ethna::raiseWarning("template is not found: " . $this->template);
}
extract($this->prop);
if ($capture === true) {
ob_start();
include $this->template_dir . $this->template;
$captured = ob_get_contents();
ob_end_clean();
return $captured;
} else {
include $this->template_dir . $this->template;
return true;
}
}
/**
* テンプレートエンジンを取得する
*
* @return object Template Engine.
*
* @access public
*/
public function getEngine()
{
return $this->engine;
}
/**
* テンプレートディレクトリを取得する
*
* @return string Template Directory
*
* @access public
*/
public function getTemplateDir()
{
return $this->template_dir;
}
/**
* テンプレート変数を取得する
*
* @param string $name 変数名
*
* @return mixed 変数
*
* @access public
*/
public function getProp($name)
{
if (isset($this->prop[$name])) {
return $this->prop[$name];
}
return null;
}
/**
* テンプレート変数を削除する
*
* @param name 変数名
*
* @access public
*/
public function removeProp($name)
{
if (isset($this->prop[$name])) {
unset($this->prop[$name]);
}
}
/**
* テンプレート変数に配列を割り当てる
*
* @param array $array
*
* @access public
*/
public function setPropArray($array)
{
$this->prop = array_merge($this->prop, $array);
}
/**
* テンプレート変数に配列を参照として割り当てる
*
* @param array $array
*
* @access public
*/
public function setPropArrayByRef(&$array)
{
$keys = array_keys($array);
$count = sizeof($keys);
for ($i = 0; $i < $count; $i++) {
$this->prop[$keys[$i]] = $array[$keys[$i]];
}
}
/**
* テンプレート変数を割り当てる
*
* @param string $name 変数名
* @param mixed $value 値
*
* @access public
*/
public function setProp($name, $value)
{
$this->prop[$name] = $value;
}
/**
* テンプレート変数に参照を割り当てる
*
* @param string $name 変数名
* @param mixed $value 値
*
* @access public
*/
public function setPropByRef($name, &$value)
{
$this->prop[$name] = $value;
}
/**
* テンプレートを割り当てる
*
* @param string $template テンプレート名
*
* @access public
*/
public function setTemplate($template)
{
$this->template = $template;
}
/**
* Get template name
*
* @return string template name
*/
public function getTemplate()
{
return $this->template;
}
/**
* テンプレートディレクトリを割り当てる
*
* @param string $dir ディレクトリ名
*
* @access public
*/
public function setTemplateDir($dir)
{
$this->template_dir = $dir;
if (substr($this->template_dir, -1) != '/') {
$this->template_dir .= '/';
}
}
/**
* テンプレートの有無をチェックする
*
* @param string $template テンプレート名
*
* @access public
*/
public function templateExists($template)
{
if (substr($this->template_dir, -1) != '/') {
$this->template_dir .= '/';
}
return (is_readable($this->template_dir . $template));
}
/**
* プラグインをセットする
*
* @param string $name プラグイン名
* @param string $type プラグインタイプ
* @param string $plugin プラグイン本体
*
* @access public
*/
public function setPlugin($name, $type, $plugin)
{
$this->plugin_registry[$type][$name] = $plugin;
}
// {{{ proxy methods (for B.C.)
/**
* テンプレート変数を割り当てる(後方互換)
*
* @access public
*/
public function assign($name, $value)
{
$this->setProp($name, $value);
}
// }}}
/**
* テンプレート変数に参照を割り当てる(後方互換)
*
* @access public
*/
public function assign_by_ref($name, &$value)
{
$this->setPropByRef($name, $value);
}
/**
* ビューを出力する
*
* @access public
*/
public function display($template = null)
{
return $this->perform($template);
}
// }}}
/**
* mergeConfig
*
* Merge renderer configuration default and user config.
*/
public function mergeConfig(array $config_default, array $user_config)
{
return array_merge(
$config_default,
$user_config
);
}
}
// }}}