规范调整

This commit is contained in:
thinkphp
2015-10-03 21:43:48 +08:00
parent 7c2bcbcff2
commit 1cfb3704c6
31 changed files with 393 additions and 285 deletions

View File

@@ -30,7 +30,7 @@ class Imagick{
*/
public function __construct($imgname = null) {
if ( !extension_loaded('Imagick') ) {
E(L('_NOT_SUPPERT_').':Imagick');
throw new \Exception(Lang::get('_NOT_SUPPERT_').':Imagick');
}
$imgname && $this->open($imgname);
}

View File

@@ -243,7 +243,7 @@ class App {
$_SERVER['PATH_INFO'] = __INFO__;
if(__INFO__ && !defined('BIND_MODULE')){
if($config['url_deny_suffix'] && preg_match('/\.('.$config['url_deny_suffix'].')$/i', __INFO__)){
exit;
throw new Exception('URL_SUFFIX_DENY');
}
$paths = explode($config['pathinfo_depr'], __INFO__,2);
// 获取URL中的模块名

View File

@@ -33,6 +33,6 @@ class Cache {
}
static public function __callStatic($method, $params){
return call_user_func_array(array(self::$handler, $method), $params);
return call_user_func_array([self::$handler, $method], $params);
}
}

View File

@@ -11,7 +11,6 @@
namespace think;
use think\View;
use org\Transform;
class Controller {
// 视图类实例
@@ -182,8 +181,8 @@ class Controller {
$data['jumpUrl'] = $jumpUrl;
// 提示标题
$data['msgTitle'] = $status ? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_');
$data['status'] = $status; // 状态
$data['msgTitle'] = Lang::get( $status ? '_OPERATION_SUCCESS_' : '_OPERATION_FAIL_');
$data['status'] = $status; // 状态
//保证输出不受静态缓存影响
Config::set('html_cache_on',false);

View File

@@ -10,6 +10,7 @@
// +----------------------------------------------------------------------
namespace think\controller;
use think\Response;
abstract class rest {
@@ -68,7 +69,7 @@ abstract class rest {
$this->$fun();
}else{
// 抛出异常
throw new \think\Exception(L('_ERROR_ACTION_:').ACTION_NAME);
throw new \think\Exception(\think\Lang::get('_ERROR_ACTION_:').ACTION_NAME);
}
}

View File

@@ -14,13 +14,12 @@ namespace think;
class Create {
static public function build($build) {
// 锁定
$lockfile = APP_PATH.'create.lock';
$lockfile = APP_PATH.'create.lock';
if(is_writable($lockfile)) {
return ;
} else {
if(!touch($lockfile)){
header('Content-Type:text/html; charset=utf-8');
exit('目录 [ '.APP_PATH.' ] 不可写!');
throw new Exception('目录 [ '.APP_PATH.' ] 不可写!');
}
}
foreach ($build as $module=>$list){
@@ -45,7 +44,7 @@ class Create {
mkdir(APP_PATH.$module.'/'.$path);
}
foreach($file as $val){
$filename = APP_PATH.$module.'/'.$path.'/'.strtolower($val).EXT;
$filename = APP_PATH.$module.'/'.$path.'/'.strtolower($val).EXT;
switch($path) {
case 'controller':// 控制器
if(!is_file($filename)) {
@@ -75,7 +74,7 @@ class Create {
// 创建欢迎页面
static public function buildHelloController($module) {
$filename = APP_PATH.$module.'/controller/index'.EXT;
$filename = APP_PATH.$module.'/controller/index'.EXT;
if(!is_file($filename)) {
$content = file_get_contents(THINK_PATH.'tpl/default_index.tpl');
$content = str_replace('{$module}',$module,$content);

View File

@@ -92,6 +92,6 @@ class Db {
// 调用驱动类的方法
static public function __callStatic($method, $params){
return call_user_func_array(array(self::$_instance, $method), $params);
return call_user_func_array([self::$_instance, $method], $params);
}
}

View File

@@ -491,7 +491,7 @@ abstract class Driver {
$whereStr = $where;
}else{ // 使用数组表达式
$operate = isset($where['_logic'])?strtoupper($where['_logic']):'';
if(in_array($operate,array('AND','OR','XOR'))){
if(in_array($operate,['AND','OR','XOR'])){
// 定义逻辑运算规则 例如 OR XOR AND NOT
$operate = ' '.$operate.' ';
unset($where['_logic']);
@@ -512,7 +512,7 @@ abstract class Driver {
$key = trim($key);
if(strpos($key,'|')) { // 支持 name|title|nickname 方式定义查询字段
$array = explode('|',$key);
$str = array();
$str = [];
foreach ($array as $m=>$k){
$v = $multi?$val[$m]:$val;
$str[] = $this->parseWhereItem($this->parseKey($k),$v);
@@ -520,7 +520,7 @@ abstract class Driver {
$whereStr .= '( '.implode(' OR ',$str).' )';
}elseif(strpos($key,'&')){
$array = explode('&',$key);
$str = array();
$str = [];
foreach ($array as $m=>$k){
$v = $multi?$val[$m]:$val;
$str[] = '('.$this->parseWhereItem($this->parseKey($k),$v).')';
@@ -548,8 +548,8 @@ abstract class Driver {
}elseif(preg_match('/^(notlike|like)$/',$exp)){// 模糊查找
if(is_array($val[1])) {
$likeLogic = isset($val[2])?strtoupper($val[2]):'OR';
if(in_array($likeLogic,array('AND','OR','XOR'))){
$like = array();
if(in_array($likeLogic,['AND','OR','XOR'])){
$like = [];
foreach ($val[1] as $item){
$like[] = $key.' '.$this->exp[$exp].' '.$this->parseValue($item);
}
@@ -576,12 +576,12 @@ abstract class Driver {
$data = is_string($val[1])? explode(',',$val[1]):$val[1];
$whereStr .= $key.' '.$this->exp[$exp].' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]);
}else{
throw new Exception(L('_EXPRESS_ERROR_').':'.$val[0]);
throw new Exception(Lang::get('_EXPRESS_ERROR_').':'.$val[0]);
}
}else {
$count = count($val);
$rule = isset($val[$count-1]) ? (is_array($val[$count-1]) ? strtoupper($val[$count-1][0]) : strtoupper($val[$count-1]) ) : '' ;
if(in_array($rule,array('AND','OR','XOR'))) {
if(in_array($rule,['AND','OR','XOR'])) {
$count = $count -1;
}else{
$rule = 'AND';
@@ -792,7 +792,7 @@ abstract class Driver {
public function insert($data,$options=[],$replace=false) {
$values = $fields = [];
$this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
foreach ($data as $key=>$val){
if(is_array($val) && 'exp' == $val[0]){
$fields[] = $this->parseKey($key);
@@ -826,14 +826,14 @@ abstract class Driver {
* @param boolean $replace 是否replace
* @return false | integer
*/
public function insertAll($dataSet,$options=array(),$replace=false) {
$values = array();
public function insertAll($dataSet,$options=[],$replace=false) {
$values = [];
$this->model = $options['model'];
if(!is_array($dataSet[0])) return false;
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$fields = array_map(array($this,'parseKey'),array_keys($dataSet[0]));
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
$fields = array_map([$this,'parseKey'],array_keys($dataSet[0]));
foreach ($dataSet as $data){
$value = array();
$value = [];
foreach ($data as $key=>$val){
if(is_array($val) && 'exp' == $val[0]){
$value[] = $val[1];
@@ -866,9 +866,9 @@ abstract class Driver {
*/
public function selectInsert($fields,$table,$options=[]) {
$this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
if(is_string($fields)) $fields = explode(',',$fields);
array_walk($fields, array($this, 'parseKey'));
array_walk($fields, [$this, 'parseKey']);
$sql = 'INSERT INTO '.$this->parseTable($table).' ('.implode(',', $fields).') ';
$sql .= $this->buildSelectSql($options);
return $this->execute($sql,!empty($options['fetch_sql']) ? true : false);
@@ -883,7 +883,7 @@ abstract class Driver {
*/
public function update($data,$options) {
$this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
$table = $this->parseTable($options['table']);
$sql = 'UPDATE ' . $table . $this->parseSet($data);
if(strpos($table,',')){// 多表更新支持JOIN操作
@@ -907,7 +907,7 @@ abstract class Driver {
*/
public function delete($options=[]) {
$this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
$table = $this->parseTable($options['table']);
$sql = 'DELETE FROM '.$table;
if(strpos($table,',')){// 多表删除支持USING和JOIN操作
@@ -934,7 +934,7 @@ abstract class Driver {
*/
public function select($options=[]) {
$this->model = $options['model'];
$this->parseBind(!empty($options['bind'])?$options['bind']:array());
$this->parseBind(!empty($options['bind'])?$options['bind']:[]);
$sql = $this->buildSelectSql($options);
$result = $this->query($sql,!empty($options['fetch_sql']) ? true : false,!empty($options['read_master']) ? true : false);
return $result;
@@ -1105,7 +1105,7 @@ abstract class Driver {
}
if($m != $r ){
$db_master = array(
$db_master = [
'username' => isset($_config['username'][$m])?$_config['username'][$m]:$_config['username'][0],
'password' => isset($_config['password'][$m])?$_config['password'][$m]:$_config['password'][0],
'hostname' => isset($_config['hostname'][$m])?$_config['hostname'][$m]:$_config['hostname'][0],
@@ -1113,9 +1113,9 @@ abstract class Driver {
'database' => isset($_config['database'][$m])?$_config['database'][$m]:$_config['database'][0],
'dsn' => isset($_config['dsn'][$m])?$_config['dsn'][$m]:$_config['dsn'][0],
'charset' => isset($_config['charset'][$m])?$_config['charset'][$m]:$_config['charset'][0],
);
];
}
$db_config = array(
$db_config = [
'username' => isset($_config['username'][$r])?$_config['username'][$r]:$_config['username'][0],
'password' => isset($_config['password'][$r])?$_config['password'][$r]:$_config['password'][0],
'hostname' => isset($_config['hostname'][$r])?$_config['hostname'][$r]:$_config['hostname'][0],
@@ -1123,7 +1123,7 @@ abstract class Driver {
'database' => isset($_config['database'][$r])?$_config['database'][$r]:$_config['database'][0],
'dsn' => isset($_config['dsn'][$r])?$_config['dsn'][$r]:$_config['dsn'][0],
'charset' => isset($_config['charset'][$r])?$_config['charset'][$r]:$_config['charset'][0],
);
];
return $this->connect($db_config,$r,$r == $m ? false : $db_master);
}

View File

@@ -31,7 +31,7 @@ class Mongo extends Driver {
*/
public function __construct($config=''){
if ( !class_exists('mongoClient') ) {
throw new Exception(L('_NOT_SUPPERT_').':Mongo');
throw new Exception(Lang::get('_NOT_SUPPERT_').':Mongo');
}
if(!empty($config)) {
$this->config = array_merge($this->config,$config);
@@ -631,7 +631,7 @@ class Mongo extends Driver {
}else{
// 查询字段的安全过滤
if(!preg_match('/^[A-Z_\|\&\-.a-z0-9]+$/',trim($key))){
throw new Exception(L('_ERROR_QUERY_').':'.$key);
throw new Exception(Lang::get('_ERROR_QUERY_').':'.$key);
}
$key = trim($key);
if(strpos($key,'|')) {

View File

@@ -10,9 +10,10 @@
// +----------------------------------------------------------------------
namespace think\db;
use think\config;
use think\debug;
use think\log;
use think\Config;
use think\Debug;
use think\Log;
use think\Exception;
use PDO;
class Lite {

View File

@@ -60,8 +60,8 @@ class Debug {
$a = ['B', 'KB', 'MB', 'GB', 'TB'];
$pos = 0;
while ($size >= 1024) {
$size /= 1024;
$pos++;
$size /= 1024;
$pos++;
}
return round($size,$dec)." ".$a[$pos];
}
@@ -81,8 +81,8 @@ class Debug {
$a = ['B', 'KB', 'MB', 'GB', 'TB'];
$pos = 0;
while ($size >= 1024) {
$size /= 1024;
$pos++;
$size /= 1024;
$pos++;
}
return round($size,$dec)." ".$a[$pos];
}

View File

@@ -94,7 +94,7 @@ class Error {
$data['code'] = $code;
$data['msg'] = $message;
$data['time'] = NOW_TIME;
App::returnData($data);
Response::returnData($data);
}
$e = [];
if (APP_DEBUG) {
@@ -116,10 +116,11 @@ class Error {
if (!empty($error_page)) {
header('Location: ' . $error_page);
} else {
if (Config::get('show_error_msg'))
if (Config::get('show_error_msg')){
$e['message'] = is_array($error) ? $error['message'] : $error;
else
}else{
$e['message'] = C('error_message');
}
}
}
// 包含异常页面模板

View File

@@ -150,7 +150,7 @@ class Input {
* 获取系统变量 支持过滤和默认值
* @access public
* @param string $method 输入数据类型
* @param array $args 参数 array(key,filter,default)
* @param array $args 参数 [key,filter,default]
* @return mixed
*/
static private function getData($name,$input,$filter,$default) {
@@ -183,7 +183,7 @@ class Input {
$filters = explode(',',$filters);
}
}elseif(is_int($filters)){
$filters = array($filters);
$filters = [$filters];
}
if(is_array($filters)){

View File

@@ -28,7 +28,7 @@ class Lang {
* @return mixed
*/
static public function set($name, $value=null,$range='') {
$range = $range?$range:self::$range;
$range = $range? $range : self::$range;
// 批量定义
if (is_array($name)){
return self::$lang[$range] = array_merge(self::$lang[$range], array_change_key_case($name));
@@ -46,8 +46,9 @@ class Lang {
static public function get($name=null, $range='') {
$range = $range?$range:self::$range;
// 空参数返回所有定义
if (empty($name))
if (empty($name)){
return self::$lang[$range];
}
$name = strtolower($name);
return isset(self::$lang[$range][$name]) ? self::$lang[$range][$name] : $name;
}

View File

@@ -89,8 +89,9 @@ class Loader {
$baseUrl = APP_PATH . $class_strut[0] . '/';
}
}
if (substr($baseUrl, -1) != '/')
if (substr($baseUrl, -1) != '/'){
$baseUrl .= '/';
}
// 如果类存在 则导入类库文件
$filename = $baseUrl . $class . $ext;
if(is_file($filename)) {

View File

@@ -56,7 +56,9 @@ class Log {
*/
static public function save($destination='',$level='') {
$log = self::getLog($level);
if(empty($log)) return ;
if(empty($log)) {
return ;
}
$message = '';
if($level) {
$message .= implode("\r\n",$log);

View File

@@ -13,9 +13,9 @@ namespace think;
class Model {
// 操作状态
const MODEL_INSERT = 1; // 插入模型数据
const MODEL_UPDATE = 2; // 更新模型数据
const MODEL_BOTH = 3; // 包含上面两种方式
const MODEL_INSERT = 1; // 新增
const MODEL_UPDATE = 2; // 更新
const MODEL_BOTH = 3; // 全部
// 当前数据库操作对象
protected $db = null;
// 数据库对象池
@@ -43,9 +43,9 @@ class Model {
// 查询表达式参数
protected $options = [];
// 命名范围定义
protected $scope = [];
protected $scope = [];
// 字段映射定义
protected $map = [];
protected $map = [];
/**
* 架构函数
@@ -110,7 +110,7 @@ class Model {
* @return mixed
*/
public function __get($name) {
return isset($this->data[$name])?$this->data[$name]:null;
return isset($this->data[$name])? $this->data[$name] : null;
}
/**
@@ -206,7 +206,9 @@ class Model {
if(false !== $result && is_numeric($result)) {
$pk = $this->getPk();
// 增加复合主键支持
if (is_array($pk)) return $result;
if (is_array($pk)) {
return $result;
}
$insertId = $this->getLastInsID();
if($insertId) {
// 自增主键返回插入ID
@@ -288,7 +290,7 @@ class Model {
if(isset($data[$field])) {
$where[$field] = $data[$field];
} else {
// 如果缺少复合主键数据则不执行
// 如果缺少复合主键数据则不执行
$this->error = Lang::get('_OPERATION_WRONG_');
return false;
}
@@ -351,7 +353,9 @@ class Model {
if (is_array($options) && (count($options) > 0) && is_array($pk)) {
$count = 0;
foreach (array_keys($options) as $key) {
if (is_int($key)) $count++;
if (is_int($key)) {
$count++;
}
}
if ($count == count($pk)) {
$i = 0;
@@ -376,7 +380,9 @@ class Model {
$result = $this->db->delete($options);
if(false !== $result && is_numeric($result)) {
$data = [];
if(isset($pkValue)) $data[$pk] = $pkValue;
if(isset($pkValue)) {
$data[$pk] = $pkValue;
}
$this->_after_delete($data,$options);
}
// 返回删除记录个数
@@ -406,7 +412,9 @@ class Model {
// 根据复合主键查询
$count = 0;
foreach (array_keys($options) as $key) {
if (is_int($key)) $count++;
if (is_int($key)) {
$count++;
}
}
if ($count == count($pk)) {
$i = 0;
@@ -427,7 +435,7 @@ class Model {
// 判断查询缓存
if(isset($options['cache'])){
$cache = $options['cache'];
$key = is_string($cache['key'])?$cache['key']:md5(serialize($options));
$key = is_string($cache['key'])? $cache['key'] : md5(serialize($options));
$data = Cache::get($key,'',$cache);
if(false !== $data){
return $data;
@@ -577,7 +585,9 @@ class Model {
// 根据复合主键查询
$count = 0;
foreach (array_keys($options) as $key) {
if (is_int($key)) $count++;
if (is_int($key)) {
$count++;
}
}
if ($count == count($pk)) {
$i = 0;
@@ -597,7 +607,7 @@ class Model {
// 判断查询缓存
if(isset($options['cache'])){
$cache = $options['cache'];
$key = is_string($cache['key'])?$cache['key']:md5(serialize($options));
$key = is_string($cache['key'])? $cache['key'] : md5(serialize($options));
$data = Cache::get($key,'',$cache);
if(false !== $data){
$this->data = $data;
@@ -620,7 +630,7 @@ class Model {
$this->data = $data;
if(isset($cache)){
Cache::set($key,$data,$cache);
}
}
return $this->data;
}
@@ -667,7 +677,7 @@ class Model {
}
// 状态
$type = $type?$type:(!empty($data[$this->getPk()])?self::MODEL_UPDATE:self::MODEL_INSERT);
$type = $type? $type : (!empty($data[$this->getPk()])? self::MODEL_UPDATE : self::MODEL_INSERT);
// 检测提交字段的合法性
if(isset($this->options['field'])) { // $this->field('field1,field2...')->create()
@@ -738,8 +748,9 @@ class Model {
* @return string
*/
public function getModelName() {
if(empty($this->name))
if(empty($this->name)){
$this->name = substr(get_class($this),0,-5);
}
return $this->name;
}
@@ -828,7 +839,7 @@ class Model {
// 增加复合主键支持
if (!empty($this->fields['_pk'])) {
if (is_string($this->fields['_pk'])) {
$this->pk = array($this->fields['_pk']);
$this->pk = [$this->fields['_pk']];
$this->fields['_pk'] = $this->pk;
}
$this->pk[] = $key;
@@ -896,10 +907,10 @@ class Model {
$options = $this->_parseOptions();
$sql = $this->db->parseSql($sql,$options);
}elseif(is_array($parse)){ // SQL预处理
$parse = array_map(array($this->db,'escapeString'),$parse);
$parse = array_map([$this->db,'escapeString'],$parse);
$sql = vsprintf($sql,$parse);
}else{
$sql = strtr($sql,array('__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix));
$sql = strtr($sql,['__TABLE__'=>$this->getTableName(),'__PREFIX__'=>$this->tablePrefix]);
$prefix = $this->tablePrefix;
$sql = preg_replace_callback("/__([A-Z0-9_-]+)__/sU", function($match) use($prefix){ return $prefix.strtolower($match[1]);}, $sql);
}
@@ -999,8 +1010,9 @@ class Model {
$expire = $key;
$key = true;
}
if(false !== $key)
if(false !== $key){
$this->options['cache'] = ['key'=>$key,'expire'=>$expire,'type'=>$type];
}
return $this;
}
@@ -1014,7 +1026,7 @@ class Model {
public function field($field,$except=false){
if(true === $field) {// 获取全部字段
$fields = $this->getDbFields();
$field = $fields?:'*';
$field = $fields? : '*';
}elseif($except) {// 字段排除
if(is_string($field)) {
$field = explode(',',$field);
@@ -1074,8 +1086,8 @@ class Model {
$parse = func_get_args();
array_shift($parse);
}
$parse = array_map([$this->db,'escapeString'],$parse);
$where = vsprintf($where,$parse);
$parse = array_map([$this->db,'escapeString'],$parse);
$where = vsprintf($where,$parse);
}elseif(is_object($where)){
$where = get_object_vars($where);
}
@@ -1118,7 +1130,7 @@ class Model {
if(is_null($listRows) && strpos($page,',')){
list($page,$listRows) = explode(',',$page);
}
$this->options['page'] = array(intval($page),intval($listRows));
$this->options['page'] = [intval($page),intval($listRows)];
return $this;
}
@@ -1324,7 +1336,7 @@ class Model {
* @return Model
*/
public function readMaster(){
$this->options['read_master'] = true;
return $this;
$this->options['read_master'] = true;
return $this;
}
}

View File

@@ -49,7 +49,7 @@ class MongoModel extends \Think\Model{
$where[$name] =$args[0];
return $this->where($where)->getField($args[1]);
}else{
throw new \think\Exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
throw new \think\Exception(__CLASS__.':'.$method.Lang::get('_METHOD_NOT_EXIST_'));
return;
}
}
@@ -104,7 +104,7 @@ class MongoModel extends \Think\Model{
// 查询成功后的回调方法
protected function _after_select(&$resultSet,$options) {
array_walk($resultSet,array($this,'checkMongoId'));
array_walk($resultSet,[$this,'checkMongoId']);
}
/**
@@ -134,11 +134,11 @@ class MongoModel extends \Think\Model{
* @param mixed $options 表达式参数
* @return mixed
*/
public function find($options=array()) {
public function find($options=[]) {
if( is_numeric($options) || is_string($options)) {
$id = $this->getPk();
$where[$id] = $options;
$options = array();
$options = [];
$options['where'] = $where;
}
// 分析表达式
@@ -165,7 +165,7 @@ class MongoModel extends \Think\Model{
* @return boolean
*/
public function setInc($field,$step=1) {
return $this->setField($field,array('inc',$step));
return $this->setField($field,['inc',$step]);
}
/**
@@ -176,7 +176,7 @@ class MongoModel extends \Think\Model{
* @return boolean
*/
public function setDec($field,$step=1) {
return $this->setField($field,array('inc','-'.$step));
return $this->setField($field,['inc','-'.$step]);
}
/**
@@ -196,11 +196,11 @@ class MongoModel extends \Think\Model{
}
$resultSet = $this->db->select($options);
if(!empty($resultSet)) {
$_field = explode(',', $field);
$field = array_keys($resultSet[0]);
$key = array_shift($field);
$key2 = array_shift($field);
$cols = array();
$_field = explode(',', $field);
$field = array_keys($resultSet[0]);
$key = array_shift($field);
$key2 = array_shift($field);
$cols = [];
$count = count($_field);
foreach ($resultSet as $result){
$name = $result[$key];
@@ -246,7 +246,7 @@ class MongoModel extends \Think\Model{
* @param array $args 参数
* @return mixed
*/
public function mongoCode($code,$args=array()) {
public function mongoCode($code,$args=[]) {
return $this->db->execute($code,$args);
}

View File

@@ -231,8 +231,9 @@ class Route {
return true;
}elseif(isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'] )) {
return true;
}else{
return false;
}
return false;
}
// 执行正则匹配下的闭包方法 支持参数调用
@@ -354,13 +355,13 @@ class Route {
// 解析规则路由
// '路由规则'=>'[控制器/操作]?额外参数1=值1&额外参数2=值2...'
// '路由规则'=>array('[控制器/操作]','额外参数1=值1&额外参数2=值2...')
// '路由规则'=> ['[控制器/操作]','额外参数1=值1&额外参数2=值2...']
// '路由规则'=>'外部地址'
// '路由规则'=>array('外部地址','重定向代码')
// '路由规则'=>['外部地址','重定向代码']
// 路由规则中 :开头 表示动态变量
// 外部地址中可以用动态变量 采用 :1 :2 的方式
// 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
// 'new/:id'=>array('/new.php?id=:1',301), 重定向
// 'news/:month/:day/:id'=>['News/read?cate=1','status=1'],
// 'new/:id'=>['/new.php?id=:1',301], 重定向
static private function parseRule($rule, $route, $regx) {
// 获取路由地址规则
$url = is_array($route) ? $route[0] : $route;
@@ -418,12 +419,12 @@ class Route {
// 解析正则路由
// '路由正则'=>'[控制器/操作]?参数1=值1&参数2=值2...'
// '路由正则'=>array('[控制器/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...')
// '路由正则'=>['[控制器/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...']
// '路由正则'=>'外部地址'
// '路由正则'=>array('外部地址','重定向代码')
// '路由正则'=>['外部地址','重定向代码']
// 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
// '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
// '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
// '/new\/(\d+)\/(\d+)/'=>['News/read?id=:1&page=:2&cate=1','status=1'],
// '/new\/(\d+)/'=>['/new.php?id=:1&page=:2&status=1','301'], 重定向
static private function parseRegex($matches, $route, $regx) {
// 获取路由地址规则
$url = is_array($route) ? $route[0] : $route;

View File

@@ -171,8 +171,9 @@ class Template {
* @return boolen
*/
private function checkCache($template,$cacheFile) {
if (!$this->config['tpl_cache']) // 优先对配置设定检测
if (!$this->config['tpl_cache']) {// 优先对配置设定检测
return false;
}
// 检查编译存储是否有效
return $this->storage->check($template,$cacheFile,$this->config['cache_time']);
}
@@ -226,7 +227,9 @@ class Template {
*/
public function parse($content) {
// 内容为空不解析
if(empty($content)) return '';
if(empty($content)) {
return '';
}
$begin = $this->config['taglib_begin'];
$end = $this->config['taglib_end'];
// 检查include语法
@@ -279,7 +282,7 @@ class Template {
}
// PHP语法检查
if($this->config['tpl_deny_php'] && false !== strpos($content,'<?php')) {
exit('_NOT_ALLOW_PHP_');
throw new Exception('_NOT_ALLOW_PHP_');
}
return $content;
}
@@ -361,8 +364,9 @@ class Template {
private function parseXmlAttrs($attrs) {
$xml = '<tpl><tag '.$attrs.' /></tpl>';
$xml = simplexml_load_string($xml);
if(!$xml)
exit('_XML_TAG_ERROR_');
if(!$xml){
throw new Exception('_XML_TAG_ERROR_');
}
$xml = (array)($xml->tag->attributes());
$array = array_change_key_case($xml['@attributes']);
return $array;
@@ -375,7 +379,9 @@ class Template {
* @return string
*/
private function parseLiteral($content) {
if(trim($content)=='') return '';
if(trim($content)=='') {
return '';
}
$content = stripslashes($content);
$i = count($this->literal);
$parseStr = "<!--###literal{$i}###-->";
@@ -500,13 +506,14 @@ class Template {
* @return string
*/
private function parseXmlTag($tLib,$tagLib,$tag,$attr,$content) {
$attr = stripslashes($attr);
$content= stripslashes($content);
if(ini_get('magic_quotes_sybase'))
$attr = str_replace('\"','\'',$attr);
$parse = '_'.$tag;
$content = trim($content);
$tags = $tLib->parseXmlAttr($attr,$tag);
$attr = stripslashes($attr);
$content = stripslashes($content);
if(ini_get('magic_quotes_sybase')){
$attr = str_replace('\"','\'',$attr);
}
$parse = '_'.$tag;
$content = trim($content);
$tags = $tLib->parseXmlAttr($attr,$tag);
return $tLib->$parse($tags,$content);
}
@@ -554,7 +561,9 @@ class Template {
$varStr = trim($varStr);
static $_varParseList = [];
//如果已经解析过该变量字串,则直接返回变量值
if(isset($_varParseList[$varStr])) return $_varParseList[$varStr];
if(isset($_varParseList[$varStr])) {
return $_varParseList[$varStr];
}
$parseStr = '';
if(!empty($varStr)){
$varArray = explode('|',$varStr);
@@ -582,8 +591,9 @@ class Template {
$name = "$$var";
}
//对变量使用函数
if(count($varArray)>0)
if(count($varArray)>0){
$name = $this->parseVarFunction($name,$varArray);
}
$parseStr = '<?php echo ('.$name.'); ?>';
}
$_varParseList[$varStr] = $parseStr;
@@ -645,13 +655,13 @@ class Template {
switch($vars[1]){
case 'SERVER':
$parseStr = '$_SERVER[\''.strtoupper($vars[2]).'\']';
break;
break;
case 'GET':
$parseStr = '$_GET[\''.$vars[2].'\']';
break;
break;
case 'POST':
$parseStr = '$_POST[\''.$vars[2].'\']';
break;
break;
case 'COOKIE':
if(isset($vars[3])) {
$parseStr = '$_COOKIE[\''.$vars[2].'\'][\''.$vars[3].'\']';
@@ -668,24 +678,24 @@ class Template {
break;
case 'ENV':
$parseStr = '$_ENV[\''.strtoupper($vars[2]).'\']';
break;
break;
case 'REQUEST':
$parseStr = '$_REQUEST[\''.$vars[2].'\']';
break;
break;
case 'CONST':
$parseStr = strtoupper($vars[2]);
break;
break;
case 'LANG':
$parseStr = '\\think\\lang::get("'.$vars[2].'")';
break;
break;
case 'CONFIG':
if(isset($vars[3])) {
$vars[2] .= '.'.$vars[3];
}
$parseStr = '\\think\\config::get("'.$vars[2].'")';
break;
break;
default:
break;
break;
}
}else if(count($vars)==2){
switch($vars[1]){
@@ -704,7 +714,7 @@ class Template {
default:
if(defined($vars[1])){
$parseStr = $vars[1];
}
}
}
}
return $parseStr;
@@ -737,9 +747,10 @@ class Template {
* @return string
*/
private function parseTemplateName($templateName){
if(substr($templateName,0,1)=='$')
if(substr($templateName,0,1)=='$'){
//支持加载变量文件名
$templateName = $this->get(substr($templateName,1));
}
$array = explode(',',$templateName);
$parseStr = '';
foreach ($array as $templateName){

View File

@@ -10,7 +10,7 @@
// +----------------------------------------------------------------------
namespace think\template\driver;
use think\exception;
use think\Exception;
class File {
// 写入编译缓存

View File

@@ -10,6 +10,7 @@
// +----------------------------------------------------------------------
namespace think\template;
use think\Exception;
/**
* ThinkPHP标签库TagLib解析基类
@@ -83,7 +84,7 @@ class TagLib {
$xml = '<tpl><tag '.$attr.' /></tpl>';
$xml = simplexml_load_string($xml);
if(!$xml) {
exit('_XML_TAG_ERROR_ : '.$attr);
throw new Exception('_XML_TAG_ERROR_ : '.$attr);
}
$xml = (array)($xml->tag->attributes());
$array = array_change_key_case($xml['@attributes']);
@@ -100,7 +101,7 @@ class TagLib {
if( isset($array[$name])) {
$array[$name] = str_replace('___','&',$array[$name]);
}elseif(false !== array_search($name,$must)){
exit('_PARAM_ERROR_:'.$name);
throw new Exception('_PARAM_ERROR_:'.$name);
}
}
}

View File

@@ -25,7 +25,7 @@ class Url {
$config = Config::get();
// 解析URL
$info = parse_url($url);
$url = !empty($info['path'])?$info['path']:ACTION_NAME;
$url = !empty($info['path'])? $info['path'] : ACTION_NAME;
if(isset($info['fragment'])) { // 解析锚点
$anchor = $info['fragment'];
if(false !== strpos($anchor,'?')) { // 解析参数
@@ -39,7 +39,7 @@ class Url {
}
// 解析子域名
if(isset($host)) {
$domain = $host.(strpos($host,'.')?'':strstr($_SERVER['HTTP_HOST'],'.'));
$domain = $host.(strpos($host,'.')? '' : strstr($_SERVER['HTTP_HOST'],'.'));
}elseif($domain===true){
$domain = $_SERVER['HTTP_HOST'];
if($config['app_sub_domain_deplay'] ) { // 开启子域名部署
@@ -107,7 +107,9 @@ class Url {
}
if(!empty($vars)) { // 添加参数
foreach ($vars as $var => $val){
if('' !== trim($val)) $url .= $depr . $var . $depr . urlencode($val);
if('' !== trim($val)) {
$url .= $depr . $var . $depr . urlencode($val);
}
}
}
if($suffix) {