From 22587e40bd7ba2f2d3cd64345f17142e2d441625 Mon Sep 17 00:00:00 2001 From: augushong Date: Thu, 2 Jun 2022 14:34:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AF=BC=E5=87=BA=E9=9D=A2?= =?UTF-8?q?=E6=9D=BF=EF=BC=9B=E5=88=A0=E9=99=A4=E9=83=A8=E5=88=86=E5=9E=83?= =?UTF-8?q?=E5=9C=BE=E6=96=87=E4=BB=B6=EF=BC=9B=E5=BC=95=E5=85=A5=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=97=A5=E5=BF=97=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .htaccess | 1 - extend/.gitignore | 1 - extend/think/log/driver/DebugMysql.php | 125 +++++++++++++++++++++++++ view/admin/post/output.html | 3 +- 4 files changed, 126 insertions(+), 4 deletions(-) delete mode 100644 .htaccess create mode 100644 extend/think/log/driver/DebugMysql.php diff --git a/.htaccess b/.htaccess deleted file mode 100644 index 0519ecb..0000000 --- a/.htaccess +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/extend/.gitignore b/extend/.gitignore index c96a04f..b722e9e 100644 --- a/extend/.gitignore +++ b/extend/.gitignore @@ -1,2 +1 @@ -* !.gitignore \ No newline at end of file diff --git a/extend/think/log/driver/DebugMysql.php b/extend/think/log/driver/DebugMysql.php new file mode 100644 index 0000000..f94d1af --- /dev/null +++ b/extend/think/log/driver/DebugMysql.php @@ -0,0 +1,125 @@ +config = array_merge($this->config, $config); + } + + $dsn = $this->parseDsn($config); + + $pdo = $this->createPdo($dsn, $config['username'], $config['password'], $config['params']); + + $this->pdo = $pdo; + + $this->tableName = $config['prefix'] . 'debug_log'; + } + + public function save(array $log): bool + { + + + $app_name = app('http')->getName() ?: ''; + + $controller_name = request()->controller(); + $action_name = request()->action(); + + if (App::runningInConsole()) { + $app_name = 'cli'; + } + + $create_time = time(); + + + $create_time_title = date('Y-m-d H:i:s', $create_time); + + $log_key = uniqid(); + + foreach ($log as $log_level => $log_list) { + foreach ($log_list as $key => $log_item) { + + if (!is_string($log_item)) { + $log_item = json_encode($log_item, JSON_UNESCAPED_UNICODE); + } + + $log_data = [ + 'level' => $log_level, + 'content' => $log_item, + 'create_time' => $create_time, + 'create_time_title' => $create_time_title, + 'uid' => $log_key, + 'app_name' => $app_name, + 'controller_name' => $controller_name, + 'action_name' => $action_name, + ]; + + foreach ($log_data as $key => &$value) { + $value = str_replace('\'', '\\\'', $value); + } + + $data_keys = array_keys($log_data); + + $data_keys_in_sql = join(',', $data_keys); + + $data_values_in_sql = join('\',\'', $log_data); + + $sql = "INSERT INTO {$this->tableName} ($data_keys_in_sql) VALUES ('$data_values_in_sql');"; + + $this->pdo->exec($sql); + } + } + + return true; + } + + /** + * 解析pdo连接的dsn信息 + * @access protected + * @param array $config 连接信息 + * @return string + */ + protected function parseDsn(array $config): string + { + if (!empty($config['socket'])) { + $dsn = 'mysql:unix_socket=' . $config['socket']; + } elseif (!empty($config['hostport'])) { + $dsn = 'mysql:host=' . $config['hostname'] . ';port=' . $config['hostport']; + } else { + $dsn = 'mysql:host=' . $config['hostname']; + } + $dsn .= ';dbname=' . $config['database']; + + if (!empty($config['charset'])) { + $dsn .= ';charset=' . $config['charset']; + } + + return $dsn; + } + + protected function createPdo($dsn, $username, $password, $params) + { + return new PDO($dsn, $username, $password, $params); + } + + public function __destruct() + { + $this->pdo = null; + } +} diff --git a/view/admin/post/output.html b/view/admin/post/output.html index 0823338..37d340b 100644 --- a/view/admin/post/output.html +++ b/view/admin/post/output.html @@ -52,9 +52,8 @@
{$post.desc}
-
- {notempty name='$post.poster'} + {notempty name='$post->getData("poster")'}