1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Kdyby (http://www.kdyby.org) |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2008 Filip Procházka ([email protected]) |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the file license.txt that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Kdyby\Doctrine\Console; |
12
|
|
|
|
13
|
|
|
use Kdyby; |
14
|
|
|
use Nette; |
15
|
|
|
use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Filip Procházka <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ColoredSqlOutput extends Nette\Object implements OutputInterface |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Symfony\Component\Console\Output\OutputInterface |
28
|
|
|
*/ |
29
|
|
|
private $output; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
public function __construct(OutputInterface $output) |
34
|
|
|
{ |
35
|
|
|
$this->output = $output; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
protected function formatSqls($message) |
41
|
|
|
{ |
42
|
|
|
$message = Nette\Utils\Strings::replace($message, "~((?:CREATE|ALTER|DROP)\\s+TABLE|(?:DROP|CREATE)\\s+INDEX)[^;]+;~i", function (array $match) { |
43
|
|
|
$output = Nette\Utils\Strings::replace($match[0], '~(?<=\b)([^\s]*[a-z]+[^\s]*)(?=\b)~', function ($id) { |
44
|
|
|
return '<info>' . $id[0] . '</info>'; |
45
|
|
|
}); |
46
|
|
|
$output = Nette\Utils\Strings::replace($output, '~(?<=\b)(CREATE|ALTER|DROP|TABLE|INDEX|ADD|CHANGE|PRIMARY\s+KEY|UNIQUE|CONSTRAINT|FOREIGN\s+KEY|REFERENCES|COMMENT|ENGINE)(?=\b)~', function ($id) { |
47
|
|
|
return '<fg=cyan>' . $id[0] . '</fg=cyan>'; |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
return $output; |
51
|
|
|
}); |
52
|
|
|
$message = Nette\Utils\Strings::replace($message, '~(?<=\b)(INSERT|UPDATE|SELECT|DELETE|INTO|VALUES|ON\s+DUPLICATE\s+KEY\s+UPDATE|SET|FROM|JOIN|LEFT|RIGHT|INNER|OUTER|NATURAL|CROSS|FULL|ON|WHERE|GROUP\s+BY|HAVING|ORDER\s+BY|LIMIT|OFFSET|UNION)(?=\b)~', function ($id) { |
53
|
|
|
return '<comment>' . $id[0] . '</comment>'; |
54
|
|
|
}); |
55
|
|
|
$message = Nette\Utils\Strings::replace($message, '~(?<=\b)(AS|ASC|DESC|USING|DEFAULT|DISTINCT|AND|OR|IN|BETWEEN|IS|LIKE|NOT|NULL|ALL|ANY|SOME|EXISTS|SET\s+NULL|AUTO_INCREMENT|CASCADE|RESTRICT|INT|SMALLINT|TINYINT|NUMERIC|VARCHAR|DATETIME|TIMESTAMP|TEXT)(?=\b)~', function ($id) { |
56
|
|
|
return '<fg=magenta>' . $id[0] . '</fg=magenta>'; |
57
|
|
|
}); |
58
|
|
|
|
59
|
|
|
return $message; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL) |
68
|
|
|
{ |
69
|
|
|
return $this->output->write($this->formatSqls($messages), $newline, $type); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function writeln($messages, $type = self::OUTPUT_NORMAL) |
78
|
|
|
{ |
79
|
|
|
return $this->output->writeln($this->formatSqls($messages), $type); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function setVerbosity($level) |
88
|
|
|
{ |
89
|
|
|
return $this->output->setVerbosity($level); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function getVerbosity() |
98
|
|
|
{ |
99
|
|
|
return $this->output->getVerbosity(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function setDecorated($decorated) |
108
|
|
|
{ |
109
|
|
|
return $this->output->setDecorated($decorated); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function isDecorated() |
118
|
|
|
{ |
119
|
|
|
return $this->output->isDecorated(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function isQuiet() |
128
|
|
|
{ |
129
|
|
|
return $this->output->isQuiet(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
|
|
public function isVerbose() |
138
|
|
|
{ |
139
|
|
|
return $this->output->isVerbose(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function isVeryVerbose() |
148
|
|
|
{ |
149
|
|
|
return $this->output->isVeryVerbose(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
|
|
public function isDebug() |
158
|
|
|
{ |
159
|
|
|
return $this->output->isDebug(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
public function setFormatter(OutputFormatterInterface $formatter) |
168
|
|
|
{ |
169
|
|
|
return $this->output->setFormatter($formatter); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
|
|
public function getFormatter() |
178
|
|
|
{ |
179
|
|
|
return $this->output->getFormatter(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|