mirror of
https://github.com/dg/dibi.git
synced 2025-08-05 05:37:39 +02:00
updated for new Nette Debug Bar
This commit is contained in:
44
dibi/Nette/IDebugPanel.php
Normal file
44
dibi/Nette/IDebugPanel.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
*
|
||||
* @copyright Copyright (c) 2004, 2010 David Grudl
|
||||
* @license http://nettephp.com/license Nette license
|
||||
* @link http://nettephp.com
|
||||
* @category Nette
|
||||
* @package Nette
|
||||
*/
|
||||
|
||||
/*namespace Nette;*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Custom output for Nette\Debug.
|
||||
*
|
||||
* @copyright Copyright (c) 2004, 2010 David Grudl
|
||||
* @package Nette
|
||||
*/
|
||||
interface IDebugPanel
|
||||
{
|
||||
|
||||
/**
|
||||
* Renders HTML code for custom tab.
|
||||
* @return void
|
||||
*/
|
||||
function getTab();
|
||||
|
||||
/**
|
||||
* Renders HTML code for custom panel.
|
||||
* @return void
|
||||
*/
|
||||
function getPanel();
|
||||
|
||||
/**
|
||||
* Returns panel ID.
|
||||
* @return string
|
||||
*/
|
||||
function getId();
|
||||
|
||||
}
|
@@ -62,8 +62,8 @@ if (!class_exists('FileNotFoundException', FALSE)) {
|
||||
class FileNotFoundException extends IOException {}
|
||||
}
|
||||
|
||||
if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
|
||||
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
|
||||
if (!interface_exists(/*Nette\*/'IDebugPanel', FALSE)) {
|
||||
require_once dirname(__FILE__) . '/Nette/IDebugPanel.php';
|
||||
}
|
||||
|
||||
if (!class_exists('DateTime53', FALSE)) {
|
||||
@@ -737,23 +737,4 @@ class dibi
|
||||
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns brief descriptions.
|
||||
* @return string
|
||||
* @return array
|
||||
*/
|
||||
public static function getColophon($sender = NULL)
|
||||
{
|
||||
$arr = array(
|
||||
'Number of SQL queries: ' . dibi::$numOfQueries
|
||||
. (dibi::$totalTime === NULL ? '' : ', elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms'),
|
||||
);
|
||||
if ($sender === 'bluescreen') {
|
||||
$arr[] = 'dibi ' . dibi::VERSION . ' (revision ' . dibi::REVISION . ')';
|
||||
}
|
||||
return $arr;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -42,10 +42,6 @@ class DibiConnection extends DibiObject
|
||||
*/
|
||||
public function __construct($config, $name = NULL)
|
||||
{
|
||||
if (class_exists(/*Nette\*/'Debug', FALSE)) {
|
||||
/*Nette\*/Debug::addColophon(array('dibi', 'getColophon'));
|
||||
}
|
||||
|
||||
// DSN string
|
||||
if (is_string($config)) {
|
||||
parse_str($config, $config);
|
||||
|
@@ -18,7 +18,7 @@
|
||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class DibiException extends Exception implements /*Nette\*/IDebuggable
|
||||
class DibiException extends Exception implements /*Nette\*/IDebugPanel
|
||||
{
|
||||
/** @var string */
|
||||
private $sql;
|
||||
@@ -59,23 +59,39 @@ class DibiException extends Exception implements /*Nette\*/IDebuggable
|
||||
|
||||
|
||||
|
||||
/********************* interface Nette\IDebuggable ****************d*g**/
|
||||
/********************* interface Nette\IDebugPanel ****************d*g**/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns custom panels.
|
||||
* @return array
|
||||
* Returns HTML code for custom tab.
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPanels()
|
||||
public function getTab()
|
||||
{
|
||||
return 'SQL';
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom panel.
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPanel()
|
||||
{
|
||||
$panels = array();
|
||||
if ($this->sql !== NULL) {
|
||||
$panels['SQL'] = array(
|
||||
'expanded' => TRUE,
|
||||
'content' => dibi::dump($this->sql, TRUE),
|
||||
);
|
||||
}
|
||||
return $panels;
|
||||
return dibi::dump($this->sql, TRUE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns panel ID.
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return __CLASS__;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -18,13 +18,13 @@
|
||||
* @copyright Copyright (c) 2005, 2010 David Grudl
|
||||
* @package dibi
|
||||
*/
|
||||
class DibiProfiler extends DibiObject implements IDibiProfiler
|
||||
class DibiProfiler extends DibiObject implements IDibiProfiler, /*Nette\*/IDebugPanel
|
||||
{
|
||||
/** maximum number of rows */
|
||||
const FIREBUG_MAX_ROWS = 30;
|
||||
const MAX_ROWS = 30;
|
||||
|
||||
/** maximum SQL length */
|
||||
const FIREBUG_MAX_LENGTH = 500;
|
||||
const MAX_LENGTH = 500;
|
||||
|
||||
/** @var string Name of the file where SQL errors should be logged */
|
||||
private $file;
|
||||
@@ -45,6 +45,10 @@ class DibiProfiler extends DibiObject implements IDibiProfiler
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if (class_exists(/*Nette\*/'Debug', FALSE) && is_callable('Debug::addPanel')) {
|
||||
/*Nette\*/Debug::addPanel($this);
|
||||
}
|
||||
|
||||
$this->useFirebug = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP/');
|
||||
}
|
||||
|
||||
@@ -114,28 +118,27 @@ class DibiProfiler extends DibiObject implements IDibiProfiler
|
||||
$count = '?';
|
||||
}
|
||||
|
||||
if ($this->useFirebug && !headers_sent()) {
|
||||
if (count(self::$table) < self::FIREBUG_MAX_ROWS) {
|
||||
self::$table[] = array(
|
||||
sprintf('%0.3f', dibi::$elapsedTime * 1000),
|
||||
strlen($sql) > self::FIREBUG_MAX_LENGTH ? substr($sql, 0, self::FIREBUG_MAX_LENGTH) . '...' : $sql,
|
||||
$count,
|
||||
$connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
||||
);
|
||||
}
|
||||
if (count(self::$table) < self::MAX_ROWS) {
|
||||
self::$table[] = array(
|
||||
sprintf('%0.3f', dibi::$elapsedTime * 1000),
|
||||
strlen($sql) > self::MAX_LENGTH ? substr($sql, 0, self::MAX_LENGTH) . '...' : $sql,
|
||||
$count,
|
||||
$connection->getConfig('driver') . '/' . $connection->getConfig('name')
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->useFirebug && !headers_sent()) {
|
||||
header('X-Wf-Protocol-dibi: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
|
||||
header('X-Wf-dibi-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0');
|
||||
header('X-Wf-dibi-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
|
||||
|
||||
$payload = array(
|
||||
$payload = json_encode(array(
|
||||
array(
|
||||
'Type' => 'TABLE',
|
||||
'Label' => 'dibi profiler (' . dibi::$numOfQueries . ' SQL queries took ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)',
|
||||
),
|
||||
self::$table,
|
||||
);
|
||||
$payload = json_encode($payload);
|
||||
));
|
||||
foreach (str_split($payload, 4990) as $num => $s) {
|
||||
$num++;
|
||||
header("X-Wf-dibi-1-1-d$num: |$s|\\"); // protocol-, structure-, plugin-, message-index
|
||||
@@ -198,4 +201,58 @@ class DibiProfiler extends DibiObject implements IDibiProfiler
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/********************* interface Nette\IDebugPanel ****************d*g**/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom tab.
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTab()
|
||||
{
|
||||
return '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAEYSURBVBgZBcHPio5hGAfg6/2+R980k6wmJgsJ5U/ZOAqbSc2GnXOwUg7BESgLUeIQ1GSjLFnMwsKGGg1qxJRmPM97/1zXFAAAAEADdlfZzr26miup2svnelq7d2aYgt3rebl585wN6+K3I1/9fJe7O/uIePP2SypJkiRJ0vMhr55FLCA3zgIAOK9uQ4MS361ZOSX+OrTvkgINSjS/HIvhjxNNFGgQsbSmabohKDNoUGLohsls6BaiQIMSs2FYmnXdUsygQYmumy3Nhi6igwalDEOJEjPKP7CA2aFNK8Bkyy3fdNCg7r9/fW3jgpVJbDmy5+PB2IYp4MXFelQ7izPrhkPHB+P5/PjhD5gCgCenx+VR/dODEwD+A3T7nqbxwf1HAAAAAElFTkSuQmCC">'
|
||||
. dibi::$numOfQueries . ' queries';
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns HTML code for custom panel.
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPanel()
|
||||
{
|
||||
if (!dibi::$numOfQueries) return;
|
||||
|
||||
$content = '<h1>SQL queries: ' . dibi::$numOfQueries . (dibi::$totalTime === NULL ? '' : ', elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms') . '</h1>';
|
||||
if (self::$table) {
|
||||
$content .= '<table>';
|
||||
foreach (self::$table as $i => $row) {
|
||||
if ($i === 0) {
|
||||
$content .= "<tr><th>$row[0]</th><th>$row[1]</th><th>$row[2]</th><th>$row[3]</th></tr>";
|
||||
} else {
|
||||
$content .= "<tr ".($i%2 ? 'class="nette-alt"':'')."><td>$row[0]</td> <td>$row[1]</td><td>$row[2]</td><td>$row[3]</td></tr>";
|
||||
}
|
||||
}
|
||||
$content .= '</table>';
|
||||
} else {
|
||||
$content .= '<p>no query</p>';
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns panel ID.
|
||||
* @return string
|
||||
*/
|
||||
public function getId()
|
||||
{
|
||||
return get_class($this);
|
||||
}
|
||||
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,3 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<h1>Nette\Debug & dibi example</h1>
|
||||
|
||||
|
||||
|
@@ -1,3 +1,5 @@
|
||||
<!DOCTYPE html>
|
||||
|
||||
<h1>Nette\Debug & dibi example 2</h1>
|
||||
|
||||
|
||||
@@ -25,4 +27,4 @@ dibi::connect(array(
|
||||
|
||||
|
||||
// throws error
|
||||
Debug::consoleDump( dibi::fetchAll('SELECT * FROM [customers] WHERE [customer_id] < %i', 38), '[customers]' );
|
||||
Debug::barDump( dibi::fetchAll('SELECT * FROM [customers] WHERE [customer_id] < %i', 38), '[customers]' );
|
||||
|
Reference in New Issue
Block a user