内核更新

This commit is contained in:
2016-08-06 15:06:11 +08:00
parent 9f5c4070cc
commit fed3a1d215
11 changed files with 229 additions and 60 deletions

View File

@@ -41,7 +41,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
{ {
// 数据库对象池 // 数据库对象池
private static $links = []; protected static $links = [];
// 数据库配置 // 数据库配置
protected $connection = []; protected $connection = [];
// 当前模型名称 // 当前模型名称
@@ -85,8 +85,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
protected $createTime = 'create_time'; protected $createTime = 'create_time';
// 更新时间字段 // 更新时间字段
protected $updateTime = 'update_time'; protected $updateTime = 'update_time';
// 删除时间字段
protected $deleteTime = 'delete_time';
// 时间字段取出后的默认时间格式 // 时间字段取出后的默认时间格式
protected $dateFormat = 'Y-m-d H:i:s'; protected $dateFormat = 'Y-m-d H:i:s';
// 字段类型或者格式转换 // 字段类型或者格式转换
@@ -168,10 +166,6 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
$query->pk($this->pk); $query->pk($this->pk);
} }
// 全局作用域
if (method_exists($this, 'base')) {
call_user_func_array([$this, 'base'], [ & $query]);
}
self::$links[$model] = $query; self::$links[$model] = $query;
} }
// 返回当前模型的数据库查询对象 // 返回当前模型的数据库查询对象
@@ -275,8 +269,37 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public function setAttr($name, $value, $data = []) public function setAttr($name, $value, $data = [])
{ {
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime, $this->deleteTime])) { if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
// 自动写入的时间戳字段 // 自动写入的时间戳字段
$value = $this->autoWriteTimestamp($name);
} else {
// 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
$value = $this->$method($value, array_merge($data, $this->data));
} elseif (isset($this->type[$name])) {
// 类型转换
$value = $this->writeTransform($value, $this->type[$name]);
}
}
// 标记字段更改
if (!isset($this->data[$name]) || ($this->data[$name] != $value && !in_array($name, $this->change))) {
$this->change[] = $name;
}
// 设置数据对象属性
$this->data[$name] = $value;
return $this;
}
/**
* 自动写入时间戳
* @access public
* @param string $name 时间戳字段
* @return mixed
*/
protected function autoWriteTimestamp($name)
{
if (isset($this->type[$name])) { if (isset($this->type[$name])) {
$type = $this->type[$name]; $type = $this->type[$name];
if (strpos($type, ':')) { if (strpos($type, ':')) {
@@ -296,24 +319,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
} else { } else {
$value = $_SERVER['REQUEST_TIME']; $value = $_SERVER['REQUEST_TIME'];
} }
} else { return $value;
// 检测修改器
$method = 'set' . Loader::parseName($name, 1) . 'Attr';
if (method_exists($this, $method)) {
$value = $this->$method($value, array_merge($data, $this->data));
} elseif (isset($this->type[$name])) {
// 类型转换
$value = $this->writeTransform($value, $this->type[$name]);
}
}
// 标记字段更改
if (!isset($this->data[$name]) || ($this->data[$name] != $value && !in_array($name, $this->change))) {
$this->change[] = $name;
}
// 设置数据对象属性
$this->data[$name] = $value;
return $this;
} }
/** /**
@@ -777,9 +783,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
/** /**
* 删除当前的记录 * 删除当前的记录
* @access public * @access public
* @param bool $force 是否强制删除
* @return integer * @return integer
*/ */
public function delete() public function delete($force = false)
{ {
if (false === $this->trigger('before_delete', $this)) { if (false === $this->trigger('before_delete', $this)) {
return false; return false;
@@ -960,7 +967,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public static function get($data = null, $with = [], $cache = false) public static function get($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = static::parseQuery($data, $with, $cache);
return $query->find($data); return $query->find($data);
} }
@@ -975,7 +982,7 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
*/ */
public static function all($data = null, $with = [], $cache = false) public static function all($data = null, $with = [], $cache = false)
{ {
$query = self::parseQuery($data, $with, $cache); $query = static::parseQuery($data, $with, $cache);
return $query->select($data); return $query->select($data);
} }
@@ -1279,14 +1286,19 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
public function __call($method, $args) public function __call($method, $args)
{ {
$query = $this->db();
// 全局作用域
if (method_exists($this, 'base')) {
call_user_func_array('static::base', [ & $query]);
}
if (method_exists($this, 'scope' . $method)) { if (method_exists($this, 'scope' . $method)) {
// 动态调用命名范围 // 动态调用命名范围
$method = 'scope' . $method; $method = 'scope' . $method;
array_unshift($args, $this->db()); array_unshift($args, $query);
call_user_func_array([$this, $method], $args); call_user_func_array([$this, $method], $args);
return $this; return $this;
} else { } else {
return call_user_func_array([$this->db(), $method], $args); return call_user_func_array([$query, $method], $args);
} }
} }
@@ -1297,6 +1309,10 @@ abstract class Model implements \JsonSerializable, \ArrayAccess
self::$links[$model] = (new static())->db(); self::$links[$model] = (new static())->db();
} }
$query = self::$links[$model]; $query = self::$links[$model];
// 全局作用域
if (method_exists($model, 'base')) {
call_user_func_array('static::base', [ & $query]);
}
return call_user_func_array([$query, $method], $params); return call_user_func_array([$query, $method], $params);
} }

View File

@@ -1204,9 +1204,9 @@ class Route
foreach ($matches[1] as $name) { foreach ($matches[1] as $name) {
if (strpos($name, '?')) { if (strpos($name, '?')) {
$name = substr($name, 0, -1); $name = substr($name, 0, -1);
$replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '') . '?)'; $replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '\w+') . '?)';
} else { } else {
$replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '') . ')'; $replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '\w+') . ')';
} }
$value[] = $name; $value[] = $name;
} }

View File

@@ -19,6 +19,9 @@ use think\Route;
class Url class Url
{ {
// 生成URL地址的root
protected static $root;
/** /**
* URL生成 支持路由反射 * URL生成 支持路由反射
* @param string $url URL表达式 * @param string $url URL表达式
@@ -129,7 +132,7 @@ class Url
// 检测域名 // 检测域名
$domain = self::parseDomain($url, $domain); $domain = self::parseDomain($url, $domain);
// URL组装 // URL组装
$url = $domain . Request::instance()->root() . '/' . ltrim($url, '/'); $url = $domain . (self::$root ?: Request::instance()->root()) . '/' . ltrim($url, '/');
return $url; return $url;
} }
@@ -354,4 +357,11 @@ class Url
{ {
Cache::rm('think_route_map'); Cache::rm('think_route_map');
} }
// 指定当前生成URL地址的root
public static function root($root)
{
self::$root = $root;
Request::instance()->root($root);
}
} }

View File

@@ -109,6 +109,7 @@ class Memcache
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return $this->handler->increment($name, $step); return $this->handler->increment($name, $step);
} }
@@ -121,7 +122,14 @@ class Memcache
*/ */
public function dec($name, $step = 1) public function dec($name, $step = 1)
{ {
return $this->handler->decrement($name, $step); $name = $this->options['prefix'] . $name;
$value = $this->handler->get($name) - $step;
$res = $this->handler->set($name, $value);
if (!$res) {
return false;
} else {
return $value;
}
} }
/** /**

View File

@@ -115,6 +115,7 @@ class Memcached
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return $this->handler->increment($name, $step); return $this->handler->increment($name, $step);
} }
@@ -127,7 +128,14 @@ class Memcached
*/ */
public function dec($name, $step = 1) public function dec($name, $step = 1)
{ {
return $this->handler->decrement($name, $step); $name = $this->options['prefix'] . $name;
$value = $this->handler->get($name) - $step;
$res = $this->handler->set($name, $value);
if (!$res) {
return false;
} else {
return $value;
}
} }
/** /**

View File

@@ -115,6 +115,7 @@ class Redis
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return $this->handler->incrby($name, $step); return $this->handler->incrby($name, $step);
} }
@@ -127,6 +128,7 @@ class Redis
*/ */
public function dec($name, $step = 1) public function dec($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return $this->handler->decrby($name, $step); return $this->handler->decrby($name, $step);
} }

View File

@@ -94,6 +94,7 @@ class Wincache
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return wincache_ucache_inc($name, $step); return wincache_ucache_inc($name, $step);
} }
@@ -106,6 +107,7 @@ class Wincache
*/ */
public function dec($name, $step = 1) public function dec($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return wincache_ucache_dec($name, $step); return wincache_ucache_dec($name, $step);
} }

View File

@@ -94,6 +94,7 @@ class Xcache
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return xcache_inc($name, $step); return xcache_inc($name, $step);
} }
@@ -106,6 +107,7 @@ class Xcache
*/ */
public function dec($name, $step = 1) public function dec($name, $step = 1)
{ {
$name = $this->options['prefix'] . $name;
return xcache_dec($name, $step); return xcache_dec($name, $step);
} }

View File

@@ -107,6 +107,8 @@ abstract class Connection
'sql_explain' => false, 'sql_explain' => false,
// Builder类 // Builder类
'builder' => '', 'builder' => '',
// 软删除字段
'soft_delete_field' => '',
]; ];
// PDO连接参数 // PDO连接参数

View File

@@ -566,7 +566,11 @@ class Query
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition)); $guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite('inc', $guid, $step, $lazyTime); $step = $this->lazyWrite('inc', $guid, $step, $lazyTime);
if (false === $step) { if (false === $step) {
return true; // 等待下次写入 // 清空查询条件
$this->options = [];
return true;
} else {
return $this->setField($field, $step);
} }
} }
return $this->setField($field, ['exp', $field . '+' . $step]); return $this->setField($field, ['exp', $field . '+' . $step]);
@@ -593,7 +597,11 @@ class Query
$guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition)); $guid = md5($this->getTable() . '_' . $field . '_' . serialize($condition));
$step = $this->lazyWrite('dec', $guid, $step, $lazyTime); $step = $this->lazyWrite('dec', $guid, $step, $lazyTime);
if (false === $step) { if (false === $step) {
return true; // 等待下次写入 // 清空查询条件
$this->options = [];
return true;
} else {
return $this->setField($field, $step);
} }
} }
return $this->setField($field, ['exp', $field . '-' . $step]); return $this->setField($field, ['exp', $field . '-' . $step]);

View File

@@ -0,0 +1,111 @@
<?php
namespace traits\model;
trait SoftDelete
{
/**
* 查询软删除数据
* @access public
* @return \think\db\Query
*/
public static function withTrashed()
{
$model = new static();
return $model->db();
}
/**
* 只查询软删除数据
* @access public
* @return \think\db\Query
*/
public static function onlyTrashed()
{
$model = new static();
return $model->db()->where(static::$deleteTime, '>', 0);
}
/**
* 删除当前的记录
* @access public
* @param bool $force 是否强制删除
* @return integer
*/
public function delete($force = false)
{
if (false === $this->trigger('before_delete', $this)) {
return false;
}
if (static::$deleteTime && !$force) {
// 软删除
$name = static::$deleteTime;
$this->change[] = $name;
$this->data[$name] = $this->autoWriteTimestamp($name);
$result = $this->isUpdate()->save();
} else {
$result = $this->db()->delete($this->data);
}
$this->trigger('after_delete', $this);
return $result;
}
/**
* 删除记录
* @access public
* @param mixed $data 主键列表 支持闭包查询条件
* @param bool $force 是否强制删除
* @return integer 成功删除的记录数
*/
public static function destroy($data, $force = false)
{
$model = new static();
$query = $model->db();
if (is_array($data) && key($data) !== 0) {
$query->where($data);
$data = null;
} elseif ($data instanceof \Closure) {
call_user_func_array($data, [ & $query]);
$data = null;
}
$resultSet = $query->select($data);
$count = 0;
if ($resultSet) {
foreach ($resultSet as $data) {
$result = $data->delete($force);
$count += $result;
}
}
return $count;
}
/**
* 恢复被软删除的记录
* @access public
* @return integer
*/
public function restore()
{
if (static::$deleteTime) {
// 恢复删除
$this->setAttr(static::$deleteTime, 0);
return $this->isUpdate()->save();
}
return false;
}
/**
* 查询默认不包含软删除数据
* @access protected
* @return void
*/
protected static function base($query)
{
if (static::$deleteTime) {
$query->where(static::$deleteTime, 0);
}
}
}