Skip to content

Commit

Permalink
🐛 Fixing a bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xxb committed Aug 9, 2019
1 parent 3939647 commit a78f980
Show file tree
Hide file tree
Showing 57 changed files with 1,510 additions and 145 deletions.
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion thinkphp/library/think/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class App extends Container
{
const VERSION = '5.1.37 LTS';
const VERSION = '5.1.38 LTS';

/**
* 当前模块路径
Expand Down
4 changes: 2 additions & 2 deletions thinkphp/library/think/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function beforeAction($method, $options = [])
*/
protected function fetch($template = '', $vars = [], $config = [])
{
return $this->view->fetch($template, $vars, $config);
return Response::create($template, 'view')->assign($vars)->config($config);
}

/**
Expand All @@ -171,7 +171,7 @@ protected function fetch($template = '', $vars = [], $config = [])
*/
protected function display($content = '', $vars = [], $config = [])
{
return $this->view->display($content, $vars, $config);
return Response::create($content, 'view')->assign($vars)->config($config)->isContent(true);
}

/**
Expand Down
20 changes: 19 additions & 1 deletion thinkphp/library/think/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ public function pathinfo()
// 判断URL里面是否有兼容模式参数
$pathinfo = $_GET[$this->config['var_pathinfo']];
unset($_GET[$this->config['var_pathinfo']]);
unset($this->get[$this->config['var_pathinfo']]);
} elseif ($this->isCli()) {
// CLI模式下 index.php module/controller/action/params/...
$pathinfo = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : '';
Expand All @@ -702,6 +703,10 @@ public function pathinfo()
}
}

if (!empty($pathinfo)) {
unset($this->get[$pathinfo], $this->request[$pathinfo]);
}

$this->pathinfo = empty($pathinfo) || '/' == $pathinfo ? '' : ltrim($pathinfo, '/');
}

Expand Down Expand Up @@ -1039,7 +1044,7 @@ public function put($name = '', $default = null, $filter = '')

protected function getInputData($content)
{
if (false !== strpos($this->contentType(), 'application/json') || 0 === strpos($content, '{"')) {
if ($this->isJson()) {
return (array) json_decode($content, true);
} elseif (strpos($content, '=')) {
parse_str($content, $data);
Expand Down Expand Up @@ -1631,6 +1636,19 @@ public function isSsl()
return false;
}

/**
* 当前是否JSON请求
* @access public
* @return bool
*/
public function isJson()
{
$contentType = $this->contentType();
$acceptType = $this->type();

return false !== strpos($contentType, 'json') || false !== strpos($acceptType, 'json');
}

/**
* 当前是否Ajax请求
* @access public
Expand Down
4 changes: 3 additions & 1 deletion thinkphp/library/think/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ public function build($url = '', $vars = '', $suffix = true, $domain = false)
// 匹配路由命名标识
$url = $match[0];

$domain = $match[1];
if ($domain) {
$domain = $match[1];
}

if (!is_null($match[2])) {
$suffix = $match[2];
Expand Down
18 changes: 12 additions & 6 deletions thinkphp/library/think/db/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ protected function parseWhereItem(Query $query, $field, $val, $rule = '', $binds
$jsonType = $query->getJsonFieldType($field);
$bindType = $this->connection->getFieldBindType($jsonType);
} else {
$bindType = isset($binds[$field]) ? $binds[$field] : PDO::PARAM_STR;
$bindType = isset($binds[$field]) && 'LIKE' != $exp ? $binds[$field] : PDO::PARAM_STR;
}

if (is_scalar($value) && !in_array($exp, ['EXP', 'NOT NULL', 'NULL', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN']) && strpos($exp, 'TIME') === false) {
Expand Down Expand Up @@ -450,7 +450,7 @@ protected function parseLike(Query $query, $key, $exp, $value, $field, $bindType
// 模糊匹配
if (is_array($value)) {
foreach ($value as $item) {
$name = $query->bind($item, $bindType);
$name = $query->bind($item, PDO::PARAM_STR);
$array[] = $key . ' ' . $exp . ' :' . $name;
}

Expand Down Expand Up @@ -604,6 +604,10 @@ protected function parseCompare(Query $query, $key, $exp, $value, $field, $bindT
$value = $this->parseClosure($query, $value);
}

if ('=' == $exp && is_null($value)) {
return $key . ' IS NULL';
}

return $key . ' ' . $exp . ' ' . $value;
}

Expand Down Expand Up @@ -651,17 +655,19 @@ protected function parseIn(Query $query, $key, $exp, $value, $field, $bindType)
$value = $value->getValue();
} else {
$value = array_unique(is_array($value) ? $value : explode(',', $value));

$array = [];

foreach ($value as $k => $v) {
$name = $query->bind($v, $bindType);
$array[] = ':' . $name;
}

$zone = implode(',', $array);

$value = empty($zone) ? "''" : $zone;
if (count($array) == 1) {
return $key . ('IN' == $exp ? ' = ' : ' <> ') . $array[0];
} else {
$zone = implode(',', $array);
$value = empty($zone) ? "''" : $zone;
}
}

return $key . ' ' . $exp . ' (' . $value . ')';
Expand Down
6 changes: 2 additions & 4 deletions thinkphp/library/think/db/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -1467,9 +1467,7 @@ public function getRealSql($sql, array $bind = [])
$value = is_array($val) ? $val[0] : $val;
$type = is_array($val) ? $val[1] : PDO::PARAM_STR;

if (self::PARAM_FLOAT == $type) {
$value = (float) $value;
} elseif (PDO::PARAM_STR == $type) {
if ((self::PARAM_FLOAT == $type || PDO::PARAM_STR == $type) && is_string($value)) {
$value = '\'' . addslashes($value) . '\'';
} elseif (PDO::PARAM_INT == $type && '' === $value) {
$value = 0;
Expand Down Expand Up @@ -1503,7 +1501,7 @@ protected function bindValue(array $bind = [])
if (PDO::PARAM_INT == $val[1] && '' === $val[0]) {
$val[0] = 0;
} elseif (self::PARAM_FLOAT == $val[1]) {
$val[0] = (float) $val[0];
$val[0] = is_string($val[0]) ? (float) $val[0] : $val[0];
$val[1] = PDO::PARAM_STR;
}

Expand Down
16 changes: 8 additions & 8 deletions thinkphp/library/think/db/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ class Query
* @var array
*/
protected $timeRule = [
'today' => ['today', 'tomorrow'],
'yesterday' => ['yesterday', 'today'],
'week' => ['this week 00:00:00', 'next week 00:00:00'],
'last week' => ['last week 00:00:00', 'this week 00:00:00'],
'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00'],
'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00'],
'year' => ['this year 1/1', 'next year 1/1'],
'last year' => ['last year 1/1', 'this year 1/1'],
'today' => ['today', 'tomorrow -1second'],
'yesterday' => ['yesterday', 'today -1second'],
'week' => ['this week 00:00:00', 'next week 00:00:00 -1second'],
'last week' => ['last week 00:00:00', 'this week 00:00:00 -1second'],
'month' => ['first Day of this month 00:00:00', 'first Day of next month 00:00:00 -1second'],
'last month' => ['first Day of last month 00:00:00', 'first Day of this month 00:00:00 -1second'],
'year' => ['this year 1/1', 'next year 1/1 -1second'],
'last year' => ['last year 1/1', 'this year 1/1 -1second'],
];

/**
Expand Down
2 changes: 1 addition & 1 deletion thinkphp/library/think/db/builder/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function parseKey(Query $query, $key, $strict = false)
// JSON字段支持
list($field, $name) = explode('->', $key, 2);

return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$.' . str_replace('->', '.', $name) . '\')';
return 'json_extract(' . $this->parseKey($query, $field, true) . ', \'$' . (strpos($name, '[') === 0 ? '' : '.') . str_replace('->', '.', $name) . '\')';
} elseif (strpos($key, '.') && !preg_match('/[,\'\"\(\)`\s]/', $key)) {
list($table, $key) = explode('.', $key, 2);

Expand Down
22 changes: 21 additions & 1 deletion thinkphp/library/think/db/connector/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,27 @@ public function getTables($dbName = '')
*/
protected function getExplain($sql)
{
$pdo = $this->linkID->query("EXPLAIN " . $sql);
$pdo = $this->linkID->prepare("EXPLAIN " . $this->queryStr);

foreach ($this->bind as $key => $val) {
// 占位符
$param = is_int($key) ? $key + 1 : ':' . $key;

if (is_array($val)) {
if (PDO::PARAM_INT == $val[1] && '' === $val[0]) {
$val[0] = 0;
} elseif (self::PARAM_FLOAT == $val[1]) {
$val[0] = is_string($val[0]) ? (float) $val[0] : $val[0];
$val[1] = PDO::PARAM_STR;
}

$result = $pdo->bindValue($param, $val[0], $val[1]);
} else {
$result = $pdo->bindValue($param, $val);
}
}

$pdo->execute();
$result = $pdo->fetch(PDO::FETCH_ASSOC);
$result = array_change_key_case($result);

Expand Down
2 changes: 1 addition & 1 deletion thinkphp/library/think/exception/ValidateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ValidateException extends \RuntimeException
public function __construct($error, $code = 0)
{
$this->error = $error;
$this->message = is_array($error) ? implode("\n\r", $error) : $error;
$this->message = is_array($error) ? implode(PHP_EOL, $error) : $error;
$this->code = $code;
}

Expand Down
14 changes: 7 additions & 7 deletions thinkphp/library/think/log/driver/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected function write($message, $destination, $apart = false, $append = false
$info['timestamp'] = date($this->config['time_format']);

foreach ($message as $type => $msg) {
$msg = is_array($msg) ? implode("\r\n", $msg) : $msg;
$msg = is_array($msg) ? implode(PHP_EOL, $msg) : $msg;
if (PHP_SAPI == 'cli') {
$info['msg'] = $msg;
$info['type'] = $type;
Expand Down Expand Up @@ -212,14 +212,14 @@ protected function checkLogSize($destination)
protected function parseCliLog($info)
{
if ($this->config['json']) {
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
$message = json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
} else {
$now = $info['timestamp'];
unset($info['timestamp']);

$message = implode("\r\n", $info);
$message = implode(PHP_EOL, $info);

$message = "[{$now}]" . $message . "\r\n";
$message = "[{$now}]" . $message . PHP_EOL;
}

return $message;
Expand All @@ -242,13 +242,13 @@ protected function parseLog($info)

if ($this->config['json']) {
$info = $requestInfo + $info;
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\r\n";
return json_encode($info, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}

array_unshift($info, "---------------------------------------------------------------\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
array_unshift($info, "---------------------------------------------------------------" . PHP_EOL . "\r\n[{$info['timestamp']}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}");
unset($info['timestamp']);

return implode("\r\n", $info) . "\r\n";
return implode(PHP_EOL, $info) . PHP_EOL;
}

protected function getDebugLog(&$info, $append, $apart)
Expand Down
12 changes: 8 additions & 4 deletions thinkphp/library/think/model/concern/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,21 +189,25 @@ public function toArray()

if (!$relation) {
$relation = $this->getAttr($key);
$relation->visible($name);
if ($relation) {
$relation->visible($name);
}
}

$item[$key] = $relation->append($name)->toArray();
$item[$key] = $relation ? $relation->append($name)->toArray() : [];
} elseif (strpos($name, '.')) {
list($key, $attr) = explode('.', $name);
// 追加关联对象属性
$relation = $this->getRelation($key);

if (!$relation) {
$relation = $this->getAttr($key);
$relation->visible([$attr]);
if ($relation) {
$relation->visible([$attr]);
}
}

$item[$key] = $relation->append([$attr])->toArray();
$item[$key] = $relation ? $relation->append([$attr])->toArray() : [];
} else {
$item[$name] = $this->getAttr($name, $item);
}
Expand Down
12 changes: 10 additions & 2 deletions thinkphp/library/think/model/relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,17 @@ public function has($operator = '>=', $count = 1, $id = '*', $joinType = 'INNER'
$relation = basename(str_replace('\\', '/', $this->model));
$localKey = $this->localKey;
$foreignKey = $this->foreignKey;
$softDelete = $this->query->getOptions('soft_delete');

return $this->parent->db()
->alias($model)
->whereExists(function ($query) use ($table, $model, $relation, $localKey, $foreignKey) {
$query->table([$table => $relation])
->field($relation . '.' . $localKey)
->whereExp($model . '.' . $foreignKey, '=' . $relation . '.' . $localKey);
->whereExp($model . '.' . $foreignKey, '=' . $relation . '.' . $localKey)
->when($softDelete, function ($query) use ($softDelete, $relation) {
$query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null);
});
});
}

Expand All @@ -167,12 +171,16 @@ public function hasWhere($where = [], $fields = null)
$this->getQueryWhere($where, $relation);
}

$fields = $this->getRelationQueryFields($fields, $model);
$fields = $this->getRelationQueryFields($fields, $model);
$softDelete = $this->query->getOptions('soft_delete');

return $this->parent->db()
->alias($model)
->field($fields)
->join([$table => $relation], $model . '.' . $this->foreignKey . '=' . $relation . '.' . $this->localKey, $this->joinType)
->when($softDelete, function ($query) use ($softDelete, $relation) {
$query->where($relation . strstr($softDelete[0], '.'), '=' == $softDelete[1][0] ? $softDelete[1][1] : null);
})
->where($where);
}

Expand Down
Loading

0 comments on commit a78f980

Please sign in to comment.