-
Notifications
You must be signed in to change notification settings - Fork 10
/
Resource.class.php
225 lines (203 loc) · 6.16 KB
/
Resource.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
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
<?php
class PhizResource {
const STYLE_PLACEHOLDER = '<!--FIS_STYLE_PLACEHOLDER-->';
const SCRIPT_PLACEHOLDER = '<!--FIS_SCRIPT_PLACEHOLDER-->';
/**
*
*/
const MAP_EXT = '.json';
/**
* @var string
*/
private static $_map_dir;
/**
* @var array
*/
private static $_maps = array();
/**
* @var array
*/
private static $_collection = array(
'js' => array(),
'css' => array()
);
private static $_imported = array();
/**
*
*/
public static function reset(){
self::$_maps = array();
self::$_collection = array(
'js' => array(),
'css' => array()
);
}
public static function css(){
return self::STYLE_PLACEHOLDER;
}
public static function js(){
return self::SCRIPT_PLACEHOLDER;
}
/**
* @param string $map_dir
*/
public static function setMapDir($map_dir){
self::$_map_dir = $map_dir;
}
/**
* @return string
*/
public static function getMapDir(){
return self::$_map_dir;
}
/**
* @param string $id
* @param string $def_ns
* @return string
*/
private static function getNamespace(&$id, $def_ns = null){
$pos = strpos($id, ':');
if($pos === false){
if($def_ns === null){
$def_ns = '__global__';
} else {
$id = $def_ns . ':' . $id;
}
return $def_ns;
} else {
return substr($id, 0, $pos);
}
}
/**
* @param $id
* @param $caller_ns
* @param &$ns
* @param &$map
* @return mixed
*/
public static function getInfo(&$id, $caller_ns = null, &$ns = null, &$map = null){
$ns = self::getNamespace($id, $caller_ns);
if(isset(self::$_maps[$ns])){
$map = self::$_maps[$ns];
} else {
if(self::$_map_dir){
if($ns === '__global__'){
$map_file = self::$_map_dir . '/map' . self::MAP_EXT;
} else {
$map_file = self::$_map_dir . '/' . $ns . '-map' . self::MAP_EXT;
}
if(file_exists($map_file)){
if(self::MAP_EXT === '.php'){
$map = self::$_maps[$ns] = include $map_file;
} else {
$map = self::$_maps[$ns] = json_decode(file_get_contents($map_file), true);
}
} else {
trigger_error('unable to load reource map [' . $map_file . ']', E_USER_ERROR);
}
} else {
trigger_error('undefined resource map dir', E_USER_ERROR);
}
}
if(isset($map['res'][$id])){
return $map['res'][$id];
} else {
trigger_error('undefined resource [' . $id . ']', E_USER_ERROR);
}
return null;
}
public static function import($id, $caller_ns = null){
if(isset(self::$_imported[$id])){
return self::$_imported[$id];
} else {
$info = self::getInfo($id, $caller_ns, $ns, $map);
if($info){
$uri = $info['uri'];
$type = $info['type'];
if(isset($info['pkg'])){
$info = $map['pkg'][$info['pkg']];
$uri = $info['uri'];
foreach($info['has'] as $rId){
self::$_imported[$rId] = $uri;
}
} else {
self::$_imported[$id] = $uri;
}
if(isset($info['deps'])){
foreach($info['deps'] as $dId){
self::import($dId);
}
}
self::$_collection[$type][] = $uri;
return $uri;
} else {
return null;
}
}
}
public static function render($type, $reset = true){
$html = '';
if(!empty(self::$_collection[$type])){
$uris = self::$_collection[$type];
$lf = "\n";
if($type === 'js'){
$html = '<script type="text/javascript" src="';
$html .= implode('"></script>' . $lf . '<script type="text/javascript" src="', $uris);
$html .= '"></script>' . $lf;
} else if($type === 'css'){
$html = '<link rel="stylesheet" type="text/css" href="';
$html .= implode('"/>' . $lf . '<link rel="stylesheet" type="text/css" href="', $uris);
$html .= '"/>' . $lf;
}
if($reset){
self::$_collection[$type] = array();
}
}
return $html;
}
public static function replace($html, $reset = true){
$pos = strpos($html, self::STYLE_PLACEHOLDER);
if($pos !== false){
$html = substr_replace($html, self::render('css'), $pos, strlen(self::STYLE_PLACEHOLDER));
}
$pos = strrpos($html, self::SCRIPT_PLACEHOLDER);
if($pos !== false){
$html = substr_replace($html, self::render('js'), $pos, strlen(self::SCRIPT_PLACEHOLDER));
}
if($reset){
self::reset();
}
return $html;
}
private static $_pool = array();
private static $_pool_name;
public static function startPool($name = '__global__'){
if(!isset(self::$_pool[$name])){
self::$_pool[$name] = '';
}
self::$_pool_name = $name;
ob_start();
}
public static function endPool(){
self::$_pool[self::$_pool_name] .= ob_get_clean();
}
public static function renderPool($name = '__global__'){
if(isset(self::$_pool[$name])){
return self::$_pool[$name];
} else {
return '';
}
}
}
function import($id, $caller_ns = null){
return PhizResource::import($id, $caller_ns);
}
function render($html, $reset = true){
echo PhizResource::replace($html, $reset);
}
function css(){
echo PhizResource::css();
}
function js(){
echo PhizResource::js();
}