1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\SqlDumper\Dumpers; |
4
|
|
|
|
5
|
|
|
use SqlFormatter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The HTML dumper. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class HtmlDumper extends FileDumper |
12
|
|
|
{ |
13
|
|
|
use ConfigAware; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The file extension. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $extension = 'html'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add the given query to dump |
24
|
|
|
* |
25
|
|
|
* @param string $query |
26
|
|
|
* @return DumperInterface |
27
|
|
|
*/ |
28
|
15 |
|
public function addQuery(string $query): DumperInterface |
29
|
|
|
{ |
30
|
15 |
|
$this->content .= '<h3>Query</h3>'; |
31
|
15 |
|
$this->content .= $this->formatSqlBlock($query); |
32
|
|
|
|
33
|
15 |
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Format an SQL code block for the given query |
38
|
|
|
* |
39
|
|
|
* @param string $query |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
15 |
|
protected function formatSqlBlock(string $query): string |
43
|
|
|
{ |
44
|
15 |
|
SqlFormatter::$cli = false; |
45
|
|
|
|
46
|
15 |
|
$formattedQuery = SqlFormatter::format($query, true); |
47
|
|
|
|
48
|
15 |
|
return "<code class='sql'>{$formattedQuery}</code>"; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add the query execution time to dump |
53
|
|
|
* |
54
|
|
|
* @param float $milliseconds |
55
|
|
|
* @return DumperInterface |
56
|
|
|
*/ |
57
|
15 |
|
public function addTime(float $milliseconds): DumperInterface |
58
|
|
|
{ |
59
|
15 |
|
$seconds = $milliseconds / 1000; |
60
|
|
|
|
61
|
15 |
|
$this->content .= "<p>Execution time in seconds: <strong>{$seconds}</strong></p>"; |
62
|
|
|
|
63
|
15 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Add the query caller to dump |
68
|
|
|
* |
69
|
|
|
* @param string $file |
70
|
|
|
* @param int $line |
71
|
|
|
* @return DumperInterface |
72
|
|
|
*/ |
73
|
15 |
|
public function addCaller(string $file, int $line): DumperInterface |
74
|
|
|
{ |
75
|
15 |
|
$this->content .= "<p>Executed in file <code>{$file}</code> on line <strong>{$line}</strong></p>"; |
76
|
|
|
|
77
|
15 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Add the given explanation rows to dump |
82
|
|
|
* |
83
|
|
|
* @param array $explanationRows |
84
|
|
|
* @return DumperInterface |
85
|
|
|
*/ |
86
|
15 |
|
public function addExplanations(array $explanationRows): DumperInterface |
87
|
|
|
{ |
88
|
15 |
|
$this->content .= '<h3>Explanation</h3>'; |
89
|
|
|
|
90
|
15 |
|
$headers = array_keys((array) $explanationRows[0]); |
91
|
|
|
|
92
|
15 |
|
$this->content .= '<div style="overflow:auto">' . $this->formatTable($headers, $explanationRows) . '</div>'; |
93
|
|
|
|
94
|
15 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Format a markdown table with the given headers and rows |
99
|
|
|
* |
100
|
|
|
* @param array $headers |
101
|
|
|
* @param array $rows |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
15 |
|
protected function formatTable(array $headers, array $rows): string |
105
|
|
|
{ |
106
|
15 |
|
$table = '<table class="table table-bordered table-striped"><thead><tr>'; |
107
|
|
|
|
108
|
15 |
|
foreach ($headers as $header) { |
109
|
15 |
|
$table .= "<th scope='col'>{$header}</th>"; |
110
|
|
|
} |
111
|
|
|
|
112
|
15 |
|
$table .= '</tr></thead><tbody>'; |
113
|
|
|
|
114
|
15 |
|
foreach ($rows as $row) { |
115
|
15 |
|
$table .= '<tr>'; |
116
|
|
|
|
117
|
15 |
|
foreach ((array) $row as $value) { |
118
|
15 |
|
$table .= "<td>{$value}</td>"; |
119
|
|
|
} |
120
|
|
|
|
121
|
15 |
|
$table .= '</tr>'; |
122
|
|
|
} |
123
|
|
|
|
124
|
15 |
|
return $table . '</tbody></table>'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Add a separator |
129
|
|
|
* |
130
|
|
|
* @return DumperInterface |
131
|
|
|
*/ |
132
|
15 |
|
public function addSeparator(): DumperInterface |
133
|
|
|
{ |
134
|
15 |
|
$this->content .= '<hr>'; |
135
|
|
|
|
136
|
15 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Add the execution time of all queries to dump |
141
|
|
|
* |
142
|
|
|
* @param float $milliseconds |
143
|
|
|
* @return DumperInterface |
144
|
|
|
*/ |
145
|
15 |
|
public function addTotalTime(float $milliseconds): DumperInterface |
146
|
|
|
{ |
147
|
15 |
|
$seconds = $milliseconds / 1000; |
148
|
|
|
|
149
|
15 |
|
$this->content .= "<p>Total queries execution time in seconds: <strong>{$seconds}</strong></p>"; |
150
|
|
|
|
151
|
15 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Dump queries information |
156
|
|
|
* |
157
|
|
|
* @return mixed |
158
|
|
|
*/ |
159
|
9 |
|
public function dump() |
160
|
|
|
{ |
161
|
9 |
|
$template = $this->config('template', __DIR__ . '/../../stubs/template.html.stub'); |
162
|
9 |
|
$this->content = str_replace('{{content}}', $this->content, file_get_contents($template)); |
163
|
|
|
|
164
|
9 |
|
parent::dump(); |
165
|
9 |
|
} |
166
|
|
|
} |
167
|
|
|
|