Skip to content

Commit

Permalink
feature: #9 support ThinkOrm(just pdo based)
Browse files Browse the repository at this point in the history
  • Loading branch information
krissss committed Nov 9, 2023
1 parent dc16c1b commit 7172990
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"illuminate/events": "^8.83",
"illuminate/redis": "^8.83",
"symfony/finder": "^5.4",
"workerman/webman-framework": "^1.4"
"workerman/webman-framework": "^1.4",
"topthink/think-orm": "^2.0"
}
}
62 changes: 62 additions & 0 deletions src/DataCollector/ThinkPdoCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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;

class ThinkPdoCollector extends PDOCollector
{
public function __construct(array $thinkOrmConfig = [], TimeDataCollector $timeCollector = null)
{
$config = array_merge([
'default' => '',
'connections' => [],
], $thinkOrmConfig);

$connectionNames = array_keys($config['connections']);
$connectionNames[] = $config['default'] ?? '';
$connectionNames = array_unique($connectionNames);

foreach ($connectionNames as $name) {
$connection = Db::connect($name);
if ($connection instanceof PDOConnection) {
$pdo = $connection->connect();
$this->addConnection(new TraceablePDO($pdo), $name);
}
}

parent::__construct(null, $timeCollector);
}

/**
* @inheritDoc
*/
public function getName()
{
return 'ThinkDB';
}

/**
* @inheritDoc
*/
public function getWidgets()
{
$name = $this->getName();
return array(
$name => array(
"icon" => "database",
"widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
"map" => $name,
"default" => "[]"
),
"{$name}:badge" => array(
"map" => "{$name}.nb_statements",
"default" => 0
)
);
}
}
13 changes: 13 additions & 0 deletions src/WebmanDebugBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use WebmanTech\Debugbar\DataCollector\RequestDataCollector;
use WebmanTech\Debugbar\DataCollector\RouteCollector;
use WebmanTech\Debugbar\DataCollector\SessionCollector;
use WebmanTech\Debugbar\DataCollector\ThinkPdoCollector;
use WebmanTech\Debugbar\DataCollector\TimeDataCollector;
use WebmanTech\Debugbar\DataCollector\WebmanCollector;
use WebmanTech\Debugbar\Ext\HttpExt;
Expand Down Expand Up @@ -70,6 +71,7 @@ class WebmanDebugBar extends DebugBar
'memory' => true,
'route' => true,
'laravelDB' => true,
'thinkDb' => true,
'laravelRedis' => true,
],
'collectors_response' => [
Expand Down Expand Up @@ -200,6 +202,17 @@ protected function collectorMaps(): array
}
return null;
},
'thinkDb' => function () {
if (class_exists('think\facade\Db')) {
$timeDataCollector = null;
if ($this->hasCollector('time')) {
/** @var \DebugBar\DataCollector\TimeDataCollector $timeDataCollector */
$timeDataCollector = $this->getCollector('time');
}
return new ThinkPdoCollector(config('thinkorm', []), $timeDataCollector);
}
return null;
}
];
}

Expand Down

0 comments on commit 7172990

Please sign in to comment.