[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開發中使用驗證碼。