CrossPHP
  • Namespace
  • Class
  • Download

Namespaces

  • Cross
    • Auth
    • Cache
      • Driver
      • Request
    • Core
    • DB
      • Connecter
      • Drivers
      • SQLAssembler
    • Exception
    • Http
    • I
    • Lib
      • Document
    • Module
    • MVC
    • Runtime
  • None

Classes

  • Cross\Auth\CookieAuth
  • Cross\Auth\SessionAuth
  • Cross\Cache\Driver\FileCacheDriver
  • Cross\Cache\Driver\MemcacheDriver
  • Cross\Cache\Driver\RedisDriver
  • Cross\Cache\Request\FileCache
  • Cross\Cache\Request\Memcache
  • Cross\Cache\Request\RedisCache
  • Cross\Cache\RequestCache
  • Cross\Core\Annotate
  • Cross\Core\Application
  • Cross\Core\ArrayMap
  • Cross\Core\Config
  • Cross\Core\CrossArray
  • Cross\Core\Delegate
  • Cross\Core\FrameBase
  • Cross\Core\Helper
  • Cross\Core\HttpAuth
  • Cross\Core\Loader
  • Cross\Core\Rest
  • Cross\Core\Router
  • Cross\DB\Connecter\BaseConnecter
  • Cross\DB\Connecter\MySQLConnecter
  • Cross\DB\Connecter\PgSQLConnecter
  • Cross\DB\Connecter\SQLiteConnecter
  • Cross\DB\DBFactory
  • Cross\DB\Drivers\CouchDriver
  • Cross\DB\Drivers\MongoDriver
  • Cross\DB\Drivers\PDOSqlDriver
  • Cross\DB\SQLAssembler\MySQLAssembler
  • Cross\DB\SQLAssembler\PgSQLAssembler
  • Cross\DB\SQLAssembler\SQLAssembler
  • Cross\DB\SQLAssembler\SQLiteAssembler
  • Cross\Http\Request
  • Cross\Http\Response
  • Cross\Lib\Array2XML
  • Cross\Lib\Document\CallTree
  • Cross\Lib\Document\CallTreeToHTML
  • Cross\Lib\Document\HTML
  • Cross\Lib\StringToPHPStream
  • Cross\Module\SQLModule
  • Cross\MVC\Controller
  • Cross\MVC\Module
  • Cross\MVC\View
  • Cross\Runtime\ClosureContainer

Interfaces

  • Cross\I\CacheInterface
  • Cross\I\HttpAuthInterface
  • Cross\I\PDOConnecter
  • Cross\I\RequestCacheInterface
  • Cross\I\RouterInterface
  • Cross\I\SqlInterface

Exceptions

  • Cross\Exception\CacheException
  • Cross\Exception\CoreException
  • Cross\Exception\CrossException
  • Cross\Exception\FrontException

Functions

  • ascLogo
  • line
  • tBody
  • th
  • tHead
  1 <?php
  2 /**
  3  * Cross - a micro PHP 5 framework
  4  *
  5  * @link        http://www.crossphp.com
  6  * @license     MIT License
  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  * @author wonli <wonli@live.com>
 18  * Class FrameBase
 19  * @package Cross\Core
 20  * @property Config $config
 21  * @property Request $request
 22  * @property Response $response
 23  * @property View $view
 24  */
 25 class FrameBase
 26 {
 27     /**
 28      * action名称
 29      *
 30      * @var string
 31      */
 32     protected $action;
 33 
 34     /**
 35      * 参数列表
 36      *
 37      * @var array
 38      */
 39     protected $params;
 40 
 41     /**
 42      * 控制器名称
 43      *
 44      * @var string
 45      */
 46     protected $controller;
 47 
 48     /**
 49      * @var Delegate
 50      */
 51     protected $delegate;
 52 
 53     /**
 54      * 视图控制器命名空间
 55      *
 56      * @var string
 57      */
 58     protected $view_controller;
 59 
 60     /**
 61      * 当前方法的注释配置
 62      *
 63      * @var array
 64      */
 65     protected $action_annotate;
 66 
 67     /**
 68      * @var Delegate
 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      * @return Config
 86      */
 87     function getConfig()
 88     {
 89         return $this->delegate->getConfig();
 90     }
 91 
 92     /**
 93      * @return Delegate
 94      */
 95     function getDelegate()
 96     {
 97         return $this->delegate;
 98     }
 99 
100     /**
101      * 返回一个数组或JSON字符串
102      *
103      * @param int $status
104      * @param string|array $message
105      * @param bool $json_encode
106      * @return array|string
107      * @throws CoreException
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      * @param string $config_file
125      * @return Config
126      * @throws CoreException
127      */
128     function loadConfig($config_file)
129     {
130         return Config::load($this->config->get('path', 'config') . $config_file);
131     }
132 
133     /**
134      * @see Loader::read()
135      *
136      * @param string $name
137      * @param bool $get_file_content
138      * @return mixed
139      * @throws CoreException
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      * <pre>
149      *  格式如下:
150      *  1 ::[path/file_name] 从当前项目根目录查找
151      *  2 app::[path/file_name] 当前app路径
152      *  3 static::[path/file_name] 静态资源目录
153      *  4 cache::[path/file_name] 缓存路径
154      *  5 config::[path/file_name] 配置路径
155      * </pre>
156      *
157      * @param string $name
158      * @return string
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      * 加密会话 sys=>auth中指定是cookie/session
199      *
200      * @param string $key key
201      * @param string $value 值
202      * @param int $expire 过期时间(默认一天过期)
203      * @return bool
204      * @throws CoreException
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      * @param string $key
215      * @param bool $deCode
216      * @return bool|mixed|string
217      * @throws CoreException
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      * uri参数加密
226      *
227      * @param string $params
228      * @param string $type
229      * @return bool|string
230      */
231     protected function urlEncrypt($params, $type = 'encode')
232     {
233         return Helper::encodeParams($params, $this->getUrlEncryptKey('uri'), $type);
234     }
235 
236     /**
237      * 获取uri加密/解密时用到的key
238      *
239      * @param string $type
240      * @return string
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      * @param bool $use_annotate
256      * @param string $params
257      * @return bool|string
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      * @return mixed
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      * request response view
346      *
347      * @param string $property
348      * @return Response|Request|View|Config|null
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 
CrossPHP API documentation generated by ApiGen