This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
AdaptiveView.php
76 lines (62 loc) · 2.2 KB
/
AdaptiveView.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
<?php
namespace Yaf\Extras;
# TODO
# enable autorender
# - auto replace extension in autorender, yaf will input `template.phtml`
# - default extension setting, like use `html.twig`
# * returned data from controller will not pass to render(), yaf sucks
// Register custom renderer according to file's extension name
// use default Yaf Simple View as fallback if no renderer matched
class AdaptiveView implements \Yaf\View_Interface {
private $path;
private $renderers = array();
private $data = array();
public function __construct($path = null) {
if ( isset($path) ) {
$this->path = $path;
} else {
$config = \Yaf\Application::app()->getConfig();
$this->path = $config['application.directory'] .'/views/';
}
}
public function render($file, $data = null) {
if ( is_array($data) ) {
$this->data = array_merge($this->data, $data);
}
// render according to extname
$extname = strtolower( pathinfo($file, PATHINFO_EXTENSION) );
if ( isset($this->renderers[$extname]) ) {
return call_user_func($this->renderers[$extname], $file, $this->data);
} else {
return $this->renderDefault($file, $this->data);
}
}
public function display($file, $data = null) {
echo $this->render($file, $data);
}
public function assign($name, $value = null) {
$this->data[$name] = $value;
}
public function getScriptPath() {
return $this->path;
}
public function setScriptPath($path) {
$this->path = $path;
}
// renderer registration
// $renderder is a callable object
// so it can be a function or a array(object, methodName)
// see http://www.php.net/manual/en/function.is-callable.php
public function on($extname, $renderer) {
$extname = strtolower($extname);
if ( is_callable($renderer) ) {
$this->renderers[$extname] = $renderer;
} else {
throw new Exception('renderer is not callable');
}
}
private function renderDefault($file, $data) {
$view = new \Yaf\View\Simple($this->path);
return $view->render($file, $data);
}
}