This commit is contained in:
thinkphp
2016-03-04 09:11:41 +08:00
7 changed files with 511 additions and 131 deletions

View File

@@ -19,6 +19,8 @@ class Loader
protected static $load = [];
// 命名空间
protected static $namespace = [];
// 命名空间别名
protected static $namespaceAlias = [];
// PSR-4
private static $prefixLengthsPsr4 = [];
private static $prefixDirsPsr4 = [];
@@ -28,6 +30,16 @@ class Loader
// 自动加载
public static function autoload($class)
{
// 检测命名空间别名
if (!empty(self::$namespaceAlias)) {
$namespace = dirname($class);
if (isset(self::$namespaceAlias[$namespace])) {
$original = self::$namespaceAlias[$namespace] . '\\' . basename($class);
if (class_exists($original)) {
return class_alias($original, $class, false);
}
}
}
// 检查是否定义类库映射
if (isset(self::$map[$class])) {
if (is_file(self::$map[$class])) {
@@ -47,6 +59,7 @@ class Loader
if (!strpos($class, '\\')) {
return false;
}
// 解析命名空间
list($name, $class) = explode('\\', $class, 2);
if (isset(self::$namespace[$name])) {
// 注册的命名空间
@@ -94,6 +107,16 @@ class Loader
}
}
// 注册命名空间别名
public static function addNamespaceAlias($namespace, $original = '')
{
if (is_array($namespace)) {
self::$namespaceAlias = array_merge(self::$namespace, $namespace);
} else {
self::$namespaceAlias[$namespace] = $original;
}
}
// 注册自动加载机制
public static function register($autoload = '')
{

View File

@@ -340,12 +340,11 @@ class Model
throw new Exception('no data to write');
}
// 数据处理
foreach ($dataList as $key => $data) {
foreach ($dataList as &$data) {
$data = $this->_write_data($data, 'insert');
if (false === $data) {
return false;
}
$dataList[$key] = $data;
}
// 分析表达式
$options = $this->_parseOptions($options);
@@ -489,8 +488,11 @@ class Model
// 判断查询缓存
if (isset($options['cache'])) {
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
$data = Cache::get($key);
if (!isset($cache['key']) || !is_string($cache['key'])) {
$cache['key'] = md5(serialize($options));
}
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
$data = Cache::get($cache['key']);
if (false !== $data) {
return $data;
}
@@ -523,7 +525,7 @@ class Model
}
if (isset($cache)) {
Cache::set($key, $resultSet, $cache['expire']);
Cache::set($cache['key'], $resultSet, $cache['expire']);
}
return $resultSet;
}
@@ -542,7 +544,7 @@ class Model
// 根据主键查询
if (is_array($options)) {
// 判断是否索引数组
if (key($options) === 0) {
if (0 === key($options)) {
$where[$pk] = ['in', $options];
} else {
return;
@@ -554,19 +556,10 @@ class Model
$options['where'] = $where;
} elseif (is_array($pk) && is_array($options) && !empty($options)) {
// 根据复合主键查询
$count = 0;
foreach (array_keys($options) as $key) {
if (is_int($key)) {
$count++;
}
}
if (count($pk) == $count) {
$i = 0;
foreach ($pk as $field) {
$where[$field] = $options[$i];
unset($options[$i++]);
}
$options['where'] = $where;
$array = array_intersect_key($options, $pk);
if (count($pk) == count($array)) {
$options = array_diff_key($options, $array);
$options['where'] = array_combine($pk, $array);
} else {
throw new Exception('miss complex primary data');
}
@@ -604,8 +597,8 @@ class Model
// 判断查询缓存
if (isset($options['cache'])) {
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
$data = Cache::get($key);
$cache['key'] = is_string($cache['key']) ? $cache['key'] : md5($sepa . serialize($options));
$data = Cache::get($cache['key']);
if (false !== $data) {
return $data;
}
@@ -620,22 +613,19 @@ class Model
if (is_string($resultSet)) {
return $resultSet;
}
$_field = explode(',', $field);
$field = array_keys($resultSet[0]);
$key1 = array_shift($field);
$key2 = array_shift($field);
$cols = array();
$count = count($_field);
$cols = [];
$count = count($field);
foreach ($resultSet as $result) {
$name = $result[$key1];
$name = $result[$field[0]];
if (2 == $count) {
$cols[$name] = $result[$key2];
$cols[$name] = $result[$field[1]];
} else {
$cols[$name] = is_string($sepa) ? implode($sepa, array_slice($result, 1)) : $result;
}
}
if (isset($cache)) {
Cache::set($key, $cols, $cache['expire']);
Cache::set($cache['key'], $cols, $cache['expire']);
}
return $cols;
}
@@ -654,15 +644,16 @@ class Model
if (true !== $sepa && 1 == $options['limit']) {
$data = reset($result[0]);
if (isset($cache)) {
Cache::set($key, $data, $cache['expire']);
Cache::set($cache['key'], $data, $cache['expire']);
}
return $data;
}
$array = [];
foreach ($result as $val) {
$array[] = $val[$field];
}
if (isset($cache)) {
Cache::set($key, $array, $cache['expire']);
Cache::set($cache['key'], $array, $cache['expire']);
}
return $array;
}
@@ -884,8 +875,11 @@ class Model
// 判断查询缓存
if (isset($options['cache'])) {
$cache = $options['cache'];
$key = is_string($cache['key']) ? $cache['key'] : md5(serialize($options));
$data = Cache::get($key);
if (!isset($cache['key']) || !is_string($cache['key'])) {
$cache['key'] = md5(serialize($options));
}
$cache['expire'] = isset($cache['expire']) ? $cache['expire'] : null;
$data = Cache::get($cache['key']);
if (false !== $data) {
$this->data = $data;
return $data;
@@ -905,7 +899,7 @@ class Model
// 回调
$this->_after_find($data, $options);
if (isset($cache)) {
Cache::set($key, $data, $cache['expire']);
Cache::set($cache['key'], $data, $cache['expire']);
}
// 数据对象赋值
$this->data = $data;
@@ -1018,7 +1012,8 @@ class Model
if (is_numeric($key) && is_array($rule)) {
$key = array_shift($rule);
}
if (!empty($scene) && !in_array($key, $scene)) {
if ((!empty($scene) && !in_array($key, $scene)) || isset($this->error[$key])) {
// 不在指定的场景中或者已经验证过相同的字段
continue;
}
if (!$this->fieldValidate($key, $rule, $data, $options)) {
@@ -1525,6 +1520,9 @@ class Model
if (!$tableName) {
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTableName();
}
if (is_array($tableName)) {
$tableName = key($tableName) ?: current($tableName);
}
if (strpos($tableName, ',')) {
// 多表不获取字段信息
return false;
@@ -1675,7 +1673,7 @@ class Model
}
if (empty($condition)) {
if (is_array($join) && is_array($join[0])) {
if (is_array($join) && is_array(current($join))) {
// 如果为组数则循环调用join
foreach ($join as $key => $value) {
if (is_array($value) && 2 <= count($value)) {
@@ -1744,21 +1742,22 @@ class Model
}
if (is_object($union)) {
$union = get_object_vars($union);
} elseif (is_string($union)) {
// 转换union表达式
$union = (array) $union;
}
// 转换union表达式
if (is_string($union)) {
$options = $this->parseSqlTable($union);
} elseif (is_array($union)) {
if (is_array($union)) {
if (isset($union[0])) {
$this->options['union'] = array_merge($this->options['union'], $union);
return $this;
foreach ($union as &$val) {
$val = $this->parseSqlTable($val);
}
$this->options['union'] = isset($this->options['union']) ? array_merge($this->options['union'], $union) : $union;
} else {
$options = $union;
$this->options['union'][] = $union;
}
} else {
throw new Exception('data type invalid', 10300);
}
$this->options['union'][] = $options;
return $this;
}

View File

@@ -456,13 +456,18 @@ class Template
// 分析模板文件名并读取内容
$parseStr = $this->parseTemplateName($file);
// 替换变量
$varStr = "";
foreach ($array as $k => $v) {
// 以$开头字符串转换成模板变量
if (0 === strpos($v, '$')) {
$v = $this->get(substr($v, 1));
}
// 兼容 [var_name] 静态渲染
$parseStr = str_replace('[' . $k . ']', $v, $parseStr);
// 支持 $var_name = value 动态赋值
$varStr .= "{assign name=\"{$k}\" value=\"{$v}\" /}";
}
$parseStr = $varStr . $parseStr;
$content = str_replace($match[0], $parseStr, $content);
// 再次对包含文件进行模板分析
$funReplace($parseStr);

View File

@@ -81,7 +81,7 @@ class Url
$url = str_replace('/', $depr, $url);
// URL后缀
$suffix = ('/' == $url) ? '' : self::parseSuffix($suffix);
$suffix = in_array($url, ['/', '']) ? '' : self::parseSuffix($suffix);
// 锚点
$anchor = !empty($anchor) ? '#' . $anchor : '';
// 参数组装
@@ -104,7 +104,6 @@ class Url
// 检测域名
$domain = self::parseDomain($url, $domain);
// URL组装
$url = $domain . Config::get('base_url') . '/' . ltrim($url, '/');
return $url;