diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index e8954bc4..85e64be4 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -89,15 +89,20 @@ class DibiConnection extends DibiObject $this->config = $config; $this->driver = new $class; - if (!empty($config['profiler'])) { - $class = $config['profiler']; - if (is_numeric($class) || is_bool($class)) { - $class = 'DibiProfiler'; - } + // profiler + $profilerCfg = & $config['profiler']; + if (is_numeric($profilerCfg) || is_bool($profilerCfg)) { // back compatibility + $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)) { throw new DibiException("Unable to create instance of dibi profiler '$class'."); } - $this->setProfiler(new $class); + $this->setProfiler(new $class($profilerCfg)); } if (!empty($config['substitutes'])) { diff --git a/dibi/libs/DibiProfiler.php b/dibi/libs/DibiProfiler.php index 86a41c94..845ab615 100644 --- a/dibi/libs/DibiProfiler.php +++ b/dibi/libs/DibiProfiler.php @@ -15,6 +15,10 @@ /** * dibi basic logger & profiler (experimental). * + * Profiler options: + * - 'explain' - explain SELECT queries? + * - 'filter' - which queries to log? + * * @copyright Copyright (c) 2005, 2010 David Grudl * @package dibi */ @@ -32,6 +36,9 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug /** @var bool log to firebug? */ public $useFirebug; + /** @var bool explain queries? */ + public $explainQuery = TRUE; + /** @var int */ 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')) { /*Nette\*/Debug::addPanel($this); } $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') ); - if ($event === self::SELECT) { + if ($this->explainQuery && $event === self::SELECT) { try { $ticket[5] = dibi::dump($connection->setProfiler(NULL)->nativeQuery('EXPLAIN ' . $sql), TRUE); } catch (DibiException $e) {} @@ -261,10 +276,10 @@ class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebug if (!($event & self::QUERY)) continue; $content .= " - " . sprintf('%0.3f', $time * 1000) . " -
explain ► - " . dibi::dump(strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql, TRUE) . " -
{$explain}
+ " . sprintf('%0.3f', $time * 1000) . ($explain ? " +
explain ►" : '') . " + " . dibi::dump(strlen($sql) > self::$maxLength ? substr($sql, 0, self::$maxLength) . '...' : $sql, TRUE) . ($explain ? " +
{$explain}
" : '') . " {$count} " . htmlSpecialChars($connection->getConfig('driver') . '/' . $connection->getConfig('name')) . " diff --git a/examples/nette-debug.php b/examples/nette-debug.php index 5c58761a..92e0818b 100644 --- a/examples/nette-debug.php +++ b/examples/nette-debug.php @@ -22,6 +22,9 @@ Debug::enable(); dibi::connect(array( 'driver' => 'sqlite', 'database' => 'sample.sdb', + 'profiler' => array( + 'run' => TRUE, + ) )); diff --git a/examples/nette-debug2.php b/examples/nette-debug2.php index ebe6556e..e05778c0 100644 --- a/examples/nette-debug2.php +++ b/examples/nette-debug2.php @@ -22,6 +22,9 @@ Debug::enable(); dibi::connect(array( 'driver' => 'sqlite', 'database' => 'sample.sdb', + 'profiler' => array( + 'run' => TRUE, + ) ));