diff --git a/src/DataCollector/PDO/PDOCollector.php b/src/DataCollector/PDO/PDOCollector.php new file mode 100644 index 0000000..b9767a5 --- /dev/null +++ b/src/DataCollector/PDO/PDOCollector.php @@ -0,0 +1,24 @@ +cleanExecutedStatements(); + } + + return $data; + } +} \ No newline at end of file diff --git a/src/DataCollector/PDO/TraceablePDO.php b/src/DataCollector/PDO/TraceablePDO.php new file mode 100644 index 0000000..623e08e --- /dev/null +++ b/src/DataCollector/PDO/TraceablePDO.php @@ -0,0 +1,15 @@ +executedStatements = []; + } +} \ No newline at end of file diff --git a/src/DataCollector/ThinkPdoCollector.php b/src/DataCollector/ThinkPdoCollector.php index 7f3b758..c3038f9 100644 --- a/src/DataCollector/ThinkPdoCollector.php +++ b/src/DataCollector/ThinkPdoCollector.php @@ -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 { @@ -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); + } } }