Skip to content

Commit

Permalink
Merge pull request #11 from webman-tech/thinkpdo-distributed
Browse files Browse the repository at this point in the history
Thinkpdo 分布式配置支持
  • Loading branch information
krissss authored Mar 19, 2024
2 parents f448183 + 9ff4be0 commit 032171f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
24 changes: 24 additions & 0 deletions src/DataCollector/PDO/PDOCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace WebmanTech\Debugbar\DataCollector\PDO;

use DebugBar\DataCollector\PDO\TraceablePDO as DebugbarTraceablePDO;
use DebugBar\DataCollector\TimeDataCollector;

class PDOCollector extends \DebugBar\DataCollector\PDO\PDOCollector
{
/**
* @inheritDoc
*/
protected function collectPDO(DebugbarTraceablePDO $pdo, TimeDataCollector $timeCollector = null, $connectionName = null)
{
$data = parent::collectPDO($pdo, $timeCollector, $connectionName);

if ($pdo instanceof TraceablePDO) {
// 清除历史记录,否则在 webman 常驻内存的情况下会有历史累计
$pdo->cleanExecutedStatements();
}

return $data;
}
}
15 changes: 15 additions & 0 deletions src/DataCollector/PDO/TraceablePDO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace WebmanTech\Debugbar\DataCollector\PDO;

class TraceablePDO extends \DebugBar\DataCollector\PDO\TraceablePDO
{
/**
* 清空历史
* @return void
*/
public function cleanExecutedStatements()
{
$this->executedStatements = [];
}
}
42 changes: 36 additions & 6 deletions src/DataCollector/ThinkPdoCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace WebmanTech\Debugbar\DataCollector;

use DebugBar\DataCollector\PDO\PDOCollector;
use DebugBar\DataCollector\PDO\TraceablePDO;
use DebugBar\DataCollector\TimeDataCollector;
use think\db\PDOConnection;
use think\facade\Db;
use WebmanTech\Debugbar\DataCollector\PDO\PDOCollector;
use WebmanTech\Debugbar\DataCollector\PDO\TraceablePDO;

class ThinkPdoCollector extends PDOCollector
{
Expand All @@ -21,11 +21,41 @@ public function __construct(array $thinkOrmConfig = [], TimeDataCollector $timeC
$connectionNames[] = $config['default'] ?? '';
$connectionNames = array_unique($connectionNames);

foreach ($connectionNames as $name) {
$connection = Db::connect($name);
foreach ($connectionNames as $connectionName) {
$connection = Db::connect($connectionName);
if ($connection instanceof PDOConnection) {
$pdo = $connection->connect();
$this->addConnection(new TraceablePDO($pdo), $name);
$dbConfig = $config['connections'][$connectionName] ?? [];
if (!empty($dbConfig['deploy'])) {
/**
* 分布式支持
* @link https://doc.thinkphp.cn/v8_0/distributed.html
* @see PDOConnection::multiConnect()
*/
$multiConfig = [];
foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
$dbConfig[$name] = $dbConfig[$name] ?? '';
$multiConfig[$name] = is_string($dbConfig[$name]) ? explode(',', $dbConfig[$name]) : $dbConfig[$name];
}
$multiDBConfig = [];
foreach ($multiConfig['hostname'] as $index => $hostname) {
$tempConfig = [];
foreach (['username', 'password', 'hostname', 'hostport', 'database', 'dsn', 'charset'] as $name) {
$tempConfig[$name] = $multiConfig[$name][$index] ?? $multiConfig[$name][0];
}
$multiDBConfig[] = [
'hostname' => $hostname,
'config' => $tempConfig,
];
}
foreach ($multiDBConfig as $index => $item) {
$pdo = $connection->connect($item['config'], $index);
$this->addConnection(new TraceablePDO($pdo), $connectionName . ':' . $item['hostname']);
}
} else {
// 单机
$pdo = $connection->connect();
$this->addConnection(new TraceablePDO($pdo), $connectionName);
}
}
}

Expand Down

0 comments on commit 032171f

Please sign in to comment.