1 | <?php |
||||
2 | |||||
3 | namespace Anax\Database; |
||||
4 | |||||
5 | /** |
||||
6 | * Database wrapper, provides a database API on top of PHP PDO for |
||||
7 | * enhancing the API and dealing with error reporting and tracking. |
||||
8 | */ |
||||
9 | class DatabaseDebug extends Database |
||||
10 | { |
||||
11 | /** |
||||
12 | * @var integer $numQueries count all queries made |
||||
13 | * @var array $queries save all queries for debugging |
||||
14 | * @var array $params Save all parameters for debugging |
||||
15 | */ |
||||
16 | private static $numQueries = 0; |
||||
17 | private static $queries = []; |
||||
18 | private static $params = []; |
||||
19 | |||||
20 | |||||
21 | |||||
22 | /** |
||||
23 | * Connect to the database. |
||||
24 | * |
||||
25 | * @return self |
||||
26 | */ |
||||
27 | public function connect() : object |
||||
28 | { |
||||
29 | if ($this->options['verbose']) { |
||||
30 | echo "<p>Connecting to dsn:<br><code>" . $this->options['dsn'] . "</code>"; |
||||
31 | } |
||||
32 | |||||
33 | $this->loadHistory(); |
||||
34 | return parent::connect(); |
||||
35 | } |
||||
36 | |||||
37 | |||||
38 | |||||
39 | /** |
||||
40 | * Load query-history from session if available. |
||||
41 | * |
||||
42 | * @return int number of database queries made. |
||||
43 | * |
||||
44 | * @SuppressWarnings(PHPMD.Superglobals) |
||||
45 | */ |
||||
46 | public function loadHistory() : int |
||||
47 | { |
||||
48 | $key = $this->options['session_key']; |
||||
49 | |||||
50 | if (isset($_SESSION['CDatabase'])) { |
||||
51 | self::$numQueries = $_SESSION[$key]['numQueries']; |
||||
52 | self::$queries = $_SESSION[$key]['queries']; |
||||
53 | self::$params = $_SESSION[$key]['params']; |
||||
54 | unset($_SESSION[$key]); |
||||
55 | } |
||||
56 | |||||
57 | return self::$numQueries; |
||||
58 | } |
||||
59 | |||||
60 | |||||
61 | |||||
62 | /** |
||||
63 | * Save query-history in session, useful as a flashmemory when redirecting |
||||
64 | * to another page. |
||||
65 | * |
||||
66 | * @param string $extra enables to save some extra debug information. |
||||
67 | * |
||||
68 | * @return void |
||||
69 | * |
||||
70 | * @SuppressWarnings(PHPMD.Superglobals) |
||||
71 | */ |
||||
72 | public function saveHistory(string $extra = null) : void |
||||
73 | { |
||||
74 | if (!is_null($extra)) { |
||||
75 | self::$queries[] = $extra; |
||||
76 | self::$params[] = null; |
||||
77 | } |
||||
78 | |||||
79 | self::$queries[] = 'Saved query-history to session.'; |
||||
80 | self::$params[] = null; |
||||
81 | |||||
82 | $key = $this->options['session_key']; |
||||
83 | $_SESSION[$key]['numQueries'] = self::$numQueries; |
||||
84 | $_SESSION[$key]['queries'] = self::$queries; |
||||
85 | $_SESSION[$key]['params'] = self::$params; |
||||
86 | } |
||||
87 | |||||
88 | |||||
89 | |||||
90 | /** |
||||
91 | * Get how many queries have been processed. |
||||
92 | * |
||||
93 | * @return int number of database queries made. |
||||
94 | */ |
||||
95 | public function getNumQueries() : int |
||||
96 | { |
||||
97 | return self::$numQueries; |
||||
98 | } |
||||
99 | |||||
100 | |||||
101 | |||||
102 | /** |
||||
103 | * Get all the queries that have been processed. |
||||
104 | * |
||||
105 | * @return array with queries. |
||||
106 | */ |
||||
107 | public function getQueries() : array |
||||
108 | { |
||||
109 | return [self::$queries, self::$params]; |
||||
110 | } |
||||
111 | |||||
112 | |||||
113 | |||||
114 | /** |
||||
115 | * Get a HTML representation of all queries made, for debugging |
||||
116 | * and analysing purpose. |
||||
117 | * |
||||
118 | * @return string with HTML. |
||||
119 | */ |
||||
120 | public function dump() : string |
||||
121 | { |
||||
122 | $html = '<p><i>You have made ' . self::$numQueries . ' database queries.</i></p><pre>'; |
||||
123 | |||||
124 | foreach (self::$queries as $key => $val) { |
||||
125 | $params = empty(self::$params[$key]) |
||||
126 | ? null |
||||
127 | : htmlentities(print_r(self::$params[$key], 1), null, 'UTF-8') . '<br/><br/>'; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() It seems like
print_r(self::params[$key], 1) can also be of type true ; however, parameter $string of htmlentities() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
128 | $html .= htmlentities($val, null, 'UTF-8') . '<br/><br/>' . $params; |
||||
129 | } |
||||
130 | |||||
131 | return $html . '</pre>'; |
||||
132 | } |
||||
133 | |||||
134 | |||||
135 | |||||
136 | /** |
||||
137 | * Execute a SQL-query and ignore the resultset. |
||||
138 | * |
||||
139 | * @param string $query the SQL statement |
||||
140 | * @param array $params the params array |
||||
141 | * |
||||
142 | * @throws Anax\Database\Exception when failing to prepare question. |
||||
143 | * |
||||
144 | * @return self |
||||
145 | */ |
||||
146 | public function execute($query, $params = []) : object |
||||
147 | { |
||||
148 | self::$queries[] = $query; |
||||
149 | self::$params[] = $params; |
||||
150 | self::$numQueries++; |
||||
151 | |||||
152 | if ($this->options['verbose']) { |
||||
153 | echo "<p>Num query = " |
||||
154 | . self::$numQueries |
||||
155 | . "</p><p>Query = </p><pre>" |
||||
156 | . htmlentities($query) |
||||
157 | . "</pre>" |
||||
158 | . (empty($params) |
||||
159 | ? null |
||||
160 | : "<p>Params:</p><pre>" . htmlentities(print_r($params, 1)) . "</pre>" |
||||
0 ignored issues
–
show
It seems like
print_r($params, 1) can also be of type true ; however, parameter $string of htmlentities() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
161 | ); |
||||
162 | } |
||||
163 | |||||
164 | return parent::execute($query, $params); |
||||
165 | } |
||||
166 | } |
||||
167 |