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 .= "