1 <?php
2 3 4 5 6 7
8
9 namespace Cross\Core;
10
11 use Cross\Exception\CoreException;
12 use Cross\Http\Response;
13 use Cross\Http\Request;
14 use Cross\MVC\View;
15
16 17 18 19 20 21 22 23 24
25 class FrameBase
26 {
27 28 29 30 31
32 protected $action;
33
34 35 36 37 38
39 protected $params;
40
41 42 43 44 45
46 protected $controller;
47
48 49 50
51 protected $delegate;
52
53 54 55 56 57
58 protected $view_controller;
59
60 61 62 63 64
65 protected $action_annotate;
66
67 68 69
70 public static $app_delegate;
71
72 public function __construct()
73 {
74 $this->delegate = self::$app_delegate;
75 $runtime_config = $this->delegate->getClosureContainer()->run('~controller~runtime~');
76
77 $this->view_controller = &$runtime_config['view_controller_namespace'];
78 $this->action_annotate = &$runtime_config['action_annotate'];
79 $this->controller = &$runtime_config['controller'];
80 $this->action = &$runtime_config['action'];
81 $this->params = &$runtime_config['params'];
82 }
83
84 85 86
87 function getConfig()
88 {
89 return $this->delegate->getConfig();
90 }
91
92 93 94
95 function getDelegate()
96 {
97 return $this->delegate;
98 }
99
100 101 102 103 104 105 106 107 108
109 function result($status = 1, $message = '', $json_encode = false)
110 {
111 $result = array('status' => $status, 'message' => $message);
112 if ($json_encode) {
113 if (($result = json_encode($result)) === false) {
114 throw new CoreException('json encode fail');
115 }
116 }
117
118 return $result;
119 }
120
121 122 123 124 125 126 127
128 function loadConfig($config_file)
129 {
130 return Config::load($this->config->get('path', 'config') . $config_file);
131 }
132
133 134 135 136 137 138 139 140
141 function parseGetFile($name, $get_file_content = false)
142 {
143 return Loader::read($this->getFilePath($name), $get_file_content);
144 }
145
146 147 148 149 150 151 152 153 154 155 156 157 158 159
160 function getFilePath($name)
161 {
162 $prefix_name = 'project';
163 if (false !== strpos($name, '::')) {
164 list($prefix_name, $file_name) = explode('::', $name);
165 if (!empty($prefix_name)) {
166 $prefix_name = strtolower(trim($prefix_name));
167 }
168 } else {
169 $file_name = $name;
170 }
171
172 static $cache = null;
173 if (!isset($cache[$prefix_name])) {
174 switch ($prefix_name) {
175 case 'app':
176 $prefix_path = $this->config->get('app', 'path');
177 break;
178
179 case 'cache':
180 case 'config':
181 $prefix_path = $this->config->get('path', $prefix_name);
182 break;
183
184 case 'static':
185 $prefix_path = $this->config->get('static', 'path');
186 break;
187
188 default:
189 $prefix_path = PROJECT_REAL_PATH;
190 }
191 $cache[$prefix_name] = rtrim($prefix_path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
192 }
193
194 return $cache[$prefix_name] . str_replace('/', DIRECTORY_SEPARATOR, $file_name);
195 }
196
197 198 199 200 201 202 203 204 205
206 protected function setAuth($key, $value, $expire = 86400)
207 {
208 return HttpAuth::factory($this->getConfig()->get('sys', 'auth'), $this->getUrlEncryptKey('auth'))->set($key, $value, $expire);
209 }
210
211 212 213 214 215 216 217 218
219 protected function getAuth($key, $deCode = false)
220 {
221 return HttpAuth::factory($this->getConfig()->get('sys', 'auth'), $this->getUrlEncryptKey('auth'))->get($key, $deCode);
222 }
223
224 225 226 227 228 229 230
231 protected function urlEncrypt($params, $type = 'encode')
232 {
233 return Helper::encodeParams($params, $this->getUrlEncryptKey('uri'), $type);
234 }
235
236 237 238 239 240 241
242 protected function getUrlEncryptKey($type = 'auth')
243 {
244 $encrypt_key = $this->getConfig()->get('encrypt', $type);
245 if (empty($encrypt_key)) {
246 $encrypt_key = 'cross';
247 }
248
249 return $encrypt_key;
250 }
251
252 253 254 255 256 257 258
259 protected function sParams($use_annotate = true, $params = null)
260 {
261 $config = $this->getConfig();
262 $addition_params = $config->get('ori_router', 'addition_params');
263 if (empty($addition_params)) {
264 $addition_params = array();
265 }
266
267 $url_config = $config->get('url');
268 if (null === $params) {
269 $ori_params = $config->get('ori_router', 'params');
270 switch ($url_config['type']) {
271 case 2:
272 $params = current(array_keys($ori_params));
273 array_shift($addition_params);
274 break;
275
276 default:
277 if (is_array($ori_params)) {
278 $params = array_shift($ori_params);
279 } else {
280 $params = $ori_params;
281 }
282 }
283 }
284
285 $decode_params_str = false;
286 if (is_string($params)) {
287 $decode_params_str = $this->urlEncrypt($params, 'decode');
288 }
289
290 if (false == $decode_params_str) {
291 if ($params !== null) return $params;
292 return $this->params;
293 }
294
295 $op_type = 2;
296 $ori_result = array();
297 if (!empty($url_config['params_dot'])) {
298 $url_dot = &$url_config['params_dot'];
299 } else {
300 $url_dot = &$url_config['dot'];
301 }
302
303 switch ($url_config['type']) {
304 case 1:
305 case 5:
306 $op_type = 1;
307 $ori_result = explode($url_dot, $decode_params_str);
308 break;
309 case 2:
310 parse_str($decode_params_str, $ori_result);
311 break;
312 case 3:
313 case 4:
314 $ori_result = Application::stringParamsToAssociativeArray($decode_params_str, $url_dot);
315 break;
316 }
317
318 if (!empty($this->action_annotate['params']) && $use_annotate) {
319 $result = Application::combineParamsAnnotateConfig($ori_result, $this->action_annotate['params'], $op_type);
320 } else {
321 $result = $ori_result;
322 }
323
324 if (!empty($addition_params) && is_array($addition_params)) {
325 $result += $addition_params;
326 }
327
328 return $result;
329 }
330
331 332 333 334 335
336 protected function initView()
337 {
338 $view = new $this->view_controller();
339 $view->config = $this->getConfig();
340 $view->params = $this->params;
341 return $view;
342 }
343
344 345 346 347 348 349
350 function __get($property)
351 {
352 switch ($property) {
353 case 'config':
354 return $this->config = $this->delegate->getConfig();
355
356 case 'request' :
357 return $this->request = $this->delegate->getRequest();
358
359 case 'response' :
360 return $this->response = $this->delegate->getResponse();
361
362 case 'view' :
363 return $this->view = $this->initView();
364 }
365
366 return null;
367 }
368 }
369