1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 13:47:33 +02:00

DibiProfiler is configurable via DibiConnection $config

This commit is contained in:
David Grudl
2010-05-19 20:32:36 +02:00
parent 88b1a45e42
commit 651c0f8c4a
4 changed files with 38 additions and 12 deletions

View File

@@ -89,15 +89,20 @@ class DibiConnection extends DibiObject
$this->config = $config; $this->config = $config;
$this->driver = new $class; $this->driver = new $class;
if (!empty($config['profiler'])) { // profiler
$class = $config['profiler']; $profilerCfg = & $config['profiler'];
if (is_numeric($class) || is_bool($class)) { if (is_numeric($profilerCfg) || is_bool($profilerCfg)) { // back compatibility
$class = 'DibiProfiler'; $profilerCfg = array('run' => (bool) $profilerCfg);
} } elseif (is_string($profilerCfg)) {
$profilerCfg = array('run' => TRUE, 'class' => $profilerCfg);
}
if (!empty($profilerCfg['run'])) {
$class = isset($profilerCfg['class']) ? $profilerCfg['class'] : 'DibiProfiler';
if (!class_exists($class)) { if (!class_exists($class)) {
throw new DibiException("Unable to create instance of dibi profiler '$class'."); throw new DibiException("Unable to create instance of dibi profiler '$class'.");
} }
$this->setProfiler(new $class); $this->setProfiler(new $class($profilerCfg));
} }
if (!empty($config['substitutes'])) { if (!empty($config['substitutes'])) {

View File

@@ -15,6 +15,10 @@
/** /**
* dibi basic logger & profiler (experimental). * dibi basic logger & profiler (experimental).
* *
* Profiler options:
* - 'explain' - explain SELECT queries?
* - 'filter' - which queries to log?
*
* @copyright Copyright (c) 2005, 2010 David Grudl * @copyright Copyright (c) 2005, 2010 David Grudl
* @package dibi * @package dibi
*/ */
@@ -32,6 +36,9 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
/** @var bool log to firebug? */ /** @var bool log to firebug? */
public $useFirebug; public $useFirebug;
/** @var bool explain queries? */
public $explainQuery = TRUE;
/** @var int */ /** @var int */
private $filter = self::ALL; private $filter = self::ALL;
@@ -43,13 +50,21 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
public function __construct() public function __construct(array $config)
{ {
if (class_exists(/*Nette\*/'Debug', FALSE) && is_callable(/*Nette\*/'Debug::addPanel')) { if (class_exists(/*Nette\*/'Debug', FALSE) && is_callable(/*Nette\*/'Debug::addPanel')) {
/*Nette\*/Debug::addPanel($this); /*Nette\*/Debug::addPanel($this);
} }
$this->useFirebug = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/'); $this->useFirebug = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/');
if (isset($config['filter'])) {
$this->setFilter($config['filter']);
}
if (isset($config['explain'])) {
$this->explainQuery = (bool) $config['explain'];
}
} }
@@ -132,7 +147,7 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
$connection->getConfig('driver') . '/' . $connection->getConfig('name') $connection->getConfig('driver') . '/' . $connection->getConfig('name')
); );
if ($event === self::SELECT) { if ($this->explainQuery && $event === self::SELECT) {
try { try {
$ticket[5] = dibi::dump($connection->setProfiler(NULL)->nativeQuery('EXPLAIN ' . $sql), TRUE); $ticket[5] = dibi::dump($connection->setProfiler(NULL)->nativeQuery('EXPLAIN ' . $sql), TRUE);
} catch (DibiException $e) {} } catch (DibiException $e) {}
@@ -261,10 +276,10 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug
if (!($event & self::QUERY)) continue; if (!($event & self::QUERY)) continue;
$content .= " $content .= "
<tr {$classes[++$i%2]}> <tr {$classes[++$i%2]}>
<td>" . sprintf('%0.3f', $time * 1000) . " <td>" . sprintf('%0.3f', $time * 1000) . ($explain ? "
<br><a href='#' class='nette-toggler' rel='#nette-debug-DibiProfiler-row-$i'>explain&nbsp;&#x25ba;</a></td> <br><a href='#' class='nette-toggler' rel='#nette-debug-DibiProfiler-row-$i'>explain&nbsp;&#x25ba;</a>" : '') . "</td>
<td class='dibi-sql'>" . dibi::dump(strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql, TRUE) . " <td class='dibi-sql'>" . dibi::dump(strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql, TRUE) . ($explain ? "
<div id='nette-debug-DibiProfiler-row-$i'>{$explain}</div></td> <div id='nette-debug-DibiProfiler-row-$i'>{$explain}</div>" : '') . "</td>
<td>{$count}</td> <td>{$count}</td>
<td>" . htmlSpecialChars($connection->getConfig('driver') . '/' . $connection->getConfig('name')) . "</td> <td>" . htmlSpecialChars($connection->getConfig('driver') . '/' . $connection->getConfig('name')) . "</td>
</tr> </tr>

View File

@@ -22,6 +22,9 @@ Debug::enable();
dibi::connect(array( dibi::connect(array(
'driver' => 'sqlite', 'driver' => 'sqlite',
'database' => 'sample.sdb', 'database' => 'sample.sdb',
'profiler' => array(
'run' => TRUE,
)
)); ));

View File

@@ -22,6 +22,9 @@ Debug::enable();
dibi::connect(array( dibi::connect(array(
'driver' => 'sqlite', 'driver' => 'sqlite',
'database' => 'sample.sdb', 'database' => 'sample.sdb',
'profiler' => array(
'run' => TRUE,
)
)); ));