|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\SqlDumper\Dumpers; |
|
4
|
|
|
|
|
5
|
|
|
use SqlFormatter; |
|
6
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
7
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
8
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The console dumper. |
|
12
|
|
|
* |
|
13
|
|
|
*/ |
|
14
|
|
|
class ConsoleDumper implements DumperInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The console output. |
|
18
|
|
|
* |
|
19
|
|
|
* @var SymfonyStyle |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $output; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Instantiate the class. |
|
25
|
|
|
* |
|
26
|
|
|
* @param SymfonyStyle $output |
|
27
|
|
|
*/ |
|
28
|
3 |
|
public function __construct(SymfonyStyle $output = null) |
|
29
|
|
|
{ |
|
30
|
3 |
|
$this->output = $output ?: $this->getDefaultOutput(); |
|
31
|
3 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Retrieve the default console output |
|
35
|
|
|
* |
|
36
|
|
|
* @return SymfonyStyle |
|
37
|
|
|
*/ |
|
38
|
3 |
|
protected function getDefaultOutput(): SymfonyStyle |
|
39
|
|
|
{ |
|
40
|
3 |
|
return new SymfonyStyle(new ArgvInput([]), new ConsoleOutput()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add the given query to dump |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $query |
|
47
|
|
|
* @return DumperInterface |
|
48
|
|
|
*/ |
|
49
|
3 |
|
public function addQuery(string $query): DumperInterface |
|
50
|
|
|
{ |
|
51
|
3 |
|
SqlFormatter::$cli = true; |
|
52
|
|
|
|
|
53
|
3 |
|
$this->output->title('Query'); |
|
54
|
3 |
|
$this->output->block(SqlFormatter::format($query)); |
|
55
|
|
|
|
|
56
|
3 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Add the query execution time to dump |
|
61
|
|
|
* |
|
62
|
|
|
* @param float $milliseconds |
|
63
|
|
|
* @return DumperInterface |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function addTime(float $milliseconds): DumperInterface |
|
66
|
|
|
{ |
|
67
|
3 |
|
$seconds = $milliseconds / 1000; |
|
68
|
|
|
|
|
69
|
3 |
|
$this->output->text("Execution time in seconds: {$seconds}"); |
|
70
|
|
|
|
|
71
|
3 |
|
return $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Add the query caller to dump |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $file |
|
78
|
|
|
* @param int $line |
|
79
|
|
|
* @return DumperInterface |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function addCaller(string $file, int $line): DumperInterface |
|
82
|
|
|
{ |
|
83
|
3 |
|
$this->output->newLine(); |
|
84
|
3 |
|
$this->output->text("Executed in file <info>{$file}</info> on line <info>{$line}</info>"); |
|
85
|
|
|
|
|
86
|
3 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Add the given explanation rows to dump |
|
91
|
|
|
* |
|
92
|
|
|
* @param array $explanationRows |
|
93
|
|
|
* @return DumperInterface |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function addExplanations(array $explanationRows): DumperInterface |
|
96
|
|
|
{ |
|
97
|
3 |
|
$headers = array_keys((array) $explanationRows[0]); |
|
98
|
3 |
|
$rows = array_map('get_object_vars', $explanationRows); |
|
99
|
|
|
|
|
100
|
3 |
|
$this->output->title('Explanation'); |
|
101
|
3 |
|
$this->output->table($headers, $rows); |
|
102
|
|
|
|
|
103
|
3 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add a separator |
|
108
|
|
|
* |
|
109
|
|
|
* @return DumperInterface |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public function addSeparator(): DumperInterface |
|
112
|
|
|
{ |
|
113
|
3 |
|
$this->output->newLine(); |
|
114
|
|
|
|
|
115
|
3 |
|
return $this; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Add the execution time of all queries to dump |
|
120
|
|
|
* |
|
121
|
|
|
* @param float $milliseconds |
|
122
|
|
|
* @return DumperInterface |
|
123
|
|
|
*/ |
|
124
|
3 |
|
public function addTotalTime(float $milliseconds): DumperInterface |
|
125
|
|
|
{ |
|
126
|
3 |
|
$seconds = $milliseconds / 1000; |
|
127
|
|
|
|
|
128
|
3 |
|
$this->output->block("Total queries execution time in seconds: {$seconds}", null, 'fg=green;bg=default'); |
|
129
|
|
|
|
|
130
|
3 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Dump queries information |
|
135
|
|
|
* |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
3 |
|
public function dump() |
|
139
|
|
|
{ |
|
140
|
|
|
// |
|
141
|
3 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|