This repository has been archived by the owner on May 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Panel.php
159 lines (123 loc) · 3.71 KB
/
Panel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace Diggriola;
use Nette\Utils\Strings,
Nette\Diagnostics\IBarPanel;
/**
* @author Štěpán Svoboda
* @author Michael Moravec
* @author Patrik Votoček
* @author Igor Hlina ([email protected])
*/
class Panel implements IBarPanel
{
/**
* @var Diggriola\Panel singleton instance
*/
private static $_instance = null;
/**
* @var array
*/
private $queries = array();
// <editor-fold defaultstate="collapsed" desc="$platform">
/**
* @var string
*/
private $platform = '';
public function getPlatform() {
return $this->platform;
}
public function setPlatform($platform) {
$this->platform = $platform;
}
// </editor-fold>
/**
* Instantiate using {@link getInstance()}; Diggriola\Panel is a singleton object.
*
* @return void
*/
public function __construct()
{
}
/**
* Enforce singleton. Disallow cloning.
*
* @return void
*/
private function __clone()
{
}
/**
* Create singleton instance
*
* @return Diggriola\Panel
*/
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function getId()
{
return 'NotORM';
}
/**
* @return string HTML code for Debugbar
*/
public function getTab()
{
return '<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAHpJREFUOMvVU8ENgDAIBON8dgY7yU3SHTohfoQUi7FGH3pJEwI9oBwl+j1YDRGR8AIzA+hiAIxLsoOW1R3zB9Cks1VKmaQWXz3wHWEJpBbilF3wivxKB9OdiUfDnJ6Q3RNGyWp3MraytbKqjADkrIvhPYgSDG3itz/TBsqre3ItA1W8AAAAAElFTkSuQmCC">' . count($this->queries) . ' queries';
}
/**
* @return string HTML code for Debugbar detail
*/
public function getPanel()
{
if (count($this->queries) == 0) {
return NULL;
}
$i = 0;
$platform = $this->platform;
$queries = $this->queries;
ob_start();
require_once __DIR__ . '/panel.phtml';
return ob_get_clean();
}
public function logQuery($sql, array $params = NULL)
{
$this->queries[] = array('sql' => $sql, 'params' => $params);
}
public static function dump($sql)
{
$keywords1 = 'CREATE\s+TABLE|CREATE(?:\s+UNIQUE)?\s+INDEX|SELECT|UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
$keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE|INTEGER|CLOB|VARCHAR|DATETIME|TIME|DATE|INT|SMALLINT|BIGINT|BOOL|BOOLEAN|DECIMAL|FLOAT|TEXT|VARCHAR|DEFAULT|AUTOINCREMENT|PRIMARY\s+KEY';
// insert new lines
$sql = " $sql ";
$sql = Strings::replace($sql, "#(?<=[\\s,(])($keywords1)(?=[\\s,)])#", "\n\$1");
if (strpos($sql, "CREATE TABLE") !== FALSE)
$sql = Strings::replace($sql, "#,\s+#i", ", \n");
// reduce spaces
$sql = Strings::replace($sql, '#[ \t]{2,}#', " ");
$sql = wordwrap($sql, 100);
$sql = htmlSpecialChars($sql);
$sql = Strings::replace($sql, "#([ \t]*\r?\n){2,}#", "\n");
$sql = Strings::replace($sql, "#VARCHAR\\(#", "VARCHAR (");
// syntax highlight
$sql = Strings::replace($sql,
"#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#s",
function ($matches) {
if (!empty($matches[1])) // comment
return '<em style="color:gray">' . $matches[1] . '</em>';
if (!empty($matches[2])) // error
return '<strong style="color:red">' . $matches[2] . '</strong>';
if (!empty($matches[3])) // most important keywords
return '<strong style="color:blue">' . $matches[3] . '</strong>';
if (!empty($matches[4])) // other keywords
return '<strong style="color:green">' . $matches[4] . '</strong>';
}
);
$sql = trim($sql);
return '<pre class="dump">' . $sql . "</pre>\n";
}
}