[TOC]
框架核心代码修改,是系统逼不得已为之,能不改动源码就解决的问题,肯定不会改的。以后是你每次`composer update`升级扩展的时候,如果有更新对应的扩展,请麻烦手动改下源码。
## think-orm : vendor\topthink\think-orm\src\model\relation\OneToOne.php
文件路径:`vendor\topthink\think-orm\src\model\relation\OneToOne.php`
方法名:match
```
~~~
foreach ($result->getData() as $key => $val) {
if (str_contains($key, '__')) {
[$name, $attr] = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
}
}
}
~~~
```
修改为:多加了一个elseif
~~~
foreach ($result->getData() as $key => $val) {
if (str_contains($key, '__')) {
[$name, $attr] = explode('__', $key, 2);
if ($name == $relation) {
$list[$name][$attr] = $val;
unset($result->$key);
} elseif (Str::studly($name) == $relation) {
$list[Str::studly($name)][$attr] = $val;
unset($result->$key);
}
}
}
~~~
以上问题,当模型一对一关联且使用Join查询的时候,会存在一定问题。
## think-orm : vendor\topthink\think-orm\src\model\relation\HasOneThrougn.php
文件路径:`vendor\topthink\think-orm\src\model\relation\HasOneThrougn.php`
`eagerlyWhere`方法,大概163行开始:
~~~
// 组装模型数据
$data = [];
/*
// TP 这里是原本的代码
// array_flip会造成 针对 "belongsToThough" 一些关联丢失
$keys = array_flip($keys);
foreach ($list as $set) {
$data[$keys[$set->{$this->throughKey}]] = $set;
}
*/
// 调整的代码
foreach ($list as $set) {
foreach ($keys as $key => $value) {
if ($value == $set->{$this->throughKey}) {
unset($keys[$key]);
$data[$key] = $set;
}
}
}
return $data;
~~~
不改的话会造成 `belongsToThrough`(官方只有HasOneThrougn,这个关联是我们系统利用HasOneThrougn ,单独加的相对远程一对一)部分关联丢失,你如果没有用这个关联,就不用改源码(系统默认是没有用过这个关联的)。
## nette : \vendor\nette\php-generator\src\PhpGenerator\Dumper.php
文件路径:`\vendor\nette\php-generator\src\PhpGenerator\Dumper.php` 大概127行:
~~~
return "[\n$space" . $this->indentation . implode(",\n$space" . $this->indentation, $pairs) . ",\n$space]";
~~~
`dumpArray`返回值做了修改,让生成的数组符合手写代码
## think-trace: \vendor\topthink\think-trace\src\Html.php
文件路径:`\vendor\topthink\think-trace\src\Html.php`文件修改:
~~~
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
namespace think\trace;
use think\App;
use think\Response;
/**
* 页面Trace调试
*/
class Html
{
protected $config = [
'file' => '',
'tabs' => ['base' => '基本', 'file' => '文件', 'info' => '流程', 'notice|error' => '错误', 'sql' => 'SQL', 'debug|log' => '调试'],
];
// 实例化并传入参数
public function __construct(array $config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 调试输出接口
* @access public
* @param App $app 应用实例
* @param Response $response Response对象
* @param array $log 日志信息
* @return bool|string
*/
public function output(App $app, Response $response, array $log = [])
{
$request = $app->request;
$contentType = $response->getHeader('Content-Type');
// 获取基本信息
$runtime = number_format(microtime(true) - $app->getBeginTime(), 10, '.', '');
$reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞';
$mem = number_format((memory_get_usage() - $app->getBeginMem()) / 1024, 2);
// 页面Trace信息
if ($request->host()) {
$uri = $request->protocol() . ' ' . $request->method() . ' : ' . $request->url(true);
} else {
$uri = 'cmd:' . implode(' ', $_SERVER['argv']);
}
$base = [
'请求信息' => date('Y-m-d H:i:s', $request->time() ?: time()) . ' ' . $uri,
'运行时间' => number_format((float) $runtime, 6) . 's [ 吞吐率:' . $reqs . 'req/s ] 内存消耗:' . $mem . 'kb 文件加载:' . count(get_included_files()),
'查询信息' => $app->db->getQueryTimes() . ' queries',
'缓存信息' => $app->cache->getReadTimes() . ' reads,' . $app->cache->getWriteTimes() . ' writes',
];
if (isset($app->session)) {
$base['会话信息'] = 'SESSION_ID=' . $app->session->getId();
}
$info = $this->getFileInfo();
// 页面Trace信息
$trace = [];
$db = [];
foreach ($this->config['tabs'] as $name => $title) {
$name = strtolower($name);
switch ($name) {
case 'base': // 基本信息
$trace[$title] = $base;
$db[$name] = $base;
break;
case 'file': // 文件信息
$trace[$title] = $info;
$db[$name] = '';
break;
default: // 调试信息
if (strpos($name, '|')) {
// 多组信息
$names = explode('|', $name);
$result = [];
foreach ($names as $item) {
$result = array_merge($result, $log[$item] ?? []);
}
$trace[$title] = $result;
$db[$names[0]] = $result;
} else {
$trace[$title] = $db[$name] = $log[$name] ?? '';
}
}
}
$dbDeny = \woo\common\helper\Arr
::normalize($app->config->get('trace.db_trace_deny_app_list', []));
$controller = $request->controller();
$appName = app('http')->getName();
if (is_woo_installed() && function_exists('get_installed_addons') && get_installed_addons('trace') && $app->config->get('trace.is_db_trace', false) && false === ((array_key_exists($appName, $dbDeny) && empty($dbDeny[$appName])) || (array_key_exists($appName, $dbDeny) && in_array($controller, $dbDeny[$appName])) )) {
$db['url'] = $request->url();
$db['route'] = $request->baseUrl();
$db['create_time'] = time();
$db['update_time'] = time();
$db['run_time'] = number_format((float) $runtime, 6);
foreach ($db as &$item) {
$item = is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item;
}
try {
\think\facade\Db::name('trace_trace')->insert($db);
} catch (\Exception $e) {}
}
if (!$app->config->get('trace.is_trace', true)) {
return false;
} elseif (in_array($appName, $app->config->get('trace.trace_deny_app_list', []))) {
return false;
}
if ($request->isJson() || $request->isAjax()) {
return false;
} elseif (!empty($contentType) && strpos($contentType, 'html') === false) {
return false;
} elseif ($response->getCode() == 204) {
return false;
}
// 调用Trace页面模板
ob_start();
include $this->config['file'] ?: __DIR__ . '/tpl/page_trace.tpl';
return ob_get_clean();
}
/**
* 获取文件加载信息
* @access protected
* @return integer|array
*/
protected function getFileInfo()
{
$files = get_included_files();
$info = [];
foreach ($files as $key => $file) {
$info[] = $file . ' ( ' . (is_file($file) ? number_format(filesize($file) / 1024, 2) : 0) . ' KB )';
}
return $info;
}
}
~~~
实现可以自由关闭`trace`,默认只有卸载扩展或关闭debug模式达到关闭trace的目的。
实现后台trace调试。
## think-captcha : \vendor\topthink\think-captcha\src\Captcha.php
文件路径:`\vendor\topthink\think-captcha\src\Captcha.php`中`create`生成验证码方法:
~~~
// api调用
if ($api) {
return [
'code' => implode('', $text),
'img' => 'data:image/png;base64,' . preg_replace('/[\r\n]/', '', chunk_split(base64_encode($content)))
];
}
return response($content, 200, ['Content-Length' => strlen($content)])->contentType('image/png');
~~~
并且把方法返回类型限制删除:
~~~
public function create(string $config = null, bool $api = false)
~~~
再方法最后一行之前加入api调用返回,方便API开发中使用验证码。