ColoredSqlOutput   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 162
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A formatSqls() 0 21 1
A write() 0 4 1
A writeln() 0 4 1
A setVerbosity() 0 4 1
A getVerbosity() 0 4 1
A setDecorated() 0 4 1
A isDecorated() 0 4 1
A isQuiet() 0 4 1
A isVerbose() 0 4 1
A isVeryVerbose() 0 4 1
A isDebug() 0 4 1
A setFormatter() 0 4 1
A getFormatter() 0 4 1
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 implements OutputInterface
24
{
25
26
	use \Kdyby\StrictObjects\Scream;
27
28
	/**
29
	 * @var \Symfony\Component\Console\Output\OutputInterface
30
	 */
31
	private $output;
32
33
34
35
	public function __construct(OutputInterface $output)
36
	{
37
		$this->output = $output;
38
	}
39
40
41
42
	protected function formatSqls($message)
43
	{
44
		$message = Nette\Utils\Strings::replace($message, "~((?:CREATE|ALTER|DROP)\\s+TABLE|(?:DROP|CREATE)\\s+INDEX)[^;]+;~i", function (array $match) {
45
			$output = Nette\Utils\Strings::replace($match[0], '~(?<=\b)([^\s]*[a-z]+[^\s]*)(?=\b)~', function ($id) {
46
				return '<info>' . $id[0] . '</info>';
47
			});
48
			$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) {
49
				return '<fg=cyan>' . $id[0] . '</fg=cyan>';
50
			});
51
52
			return $output;
53
		});
54
		$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) {
55
			return '<comment>' . $id[0] . '</comment>';
56
		});
57
		$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) {
58
			return '<fg=magenta>' . $id[0] . '</fg=magenta>';
59
		});
60
61
		return $message;
62
	}
63
64
65
66
	/**
67
	 * {@inheritdoc}
68
	 */
69
	public function write($messages, $newline = false, $type = self::OUTPUT_NORMAL)
70
	{
71
		return $this->output->write($this->formatSqls($messages), $newline, $type);
72
	}
73
74
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function writeln($messages, $type = self::OUTPUT_NORMAL)
80
	{
81
		return $this->output->writeln($this->formatSqls($messages), $type);
82
	}
83
84
85
86
	/**
87
	 * {@inheritdoc}
88
	 */
89
	public function setVerbosity($level)
90
	{
91
		return $this->output->setVerbosity($level);
92
	}
93
94
95
96
	/**
97
	 * {@inheritdoc}
98
	 */
99
	public function getVerbosity()
100
	{
101
		return $this->output->getVerbosity();
102
	}
103
104
105
106
	/**
107
	 * {@inheritdoc}
108
	 */
109
	public function setDecorated($decorated)
110
	{
111
		return $this->output->setDecorated($decorated);
112
	}
113
114
115
116
	/**
117
	 * {@inheritdoc}
118
	 */
119
	public function isDecorated()
120
	{
121
		return $this->output->isDecorated();
122
	}
123
124
125
126
	/**
127
	 * {@inheritdoc}
128
	 */
129
	public function isQuiet()
130
	{
131
		return $this->output->isQuiet();
132
	}
133
134
135
136
	/**
137
	 * {@inheritdoc}
138
	 */
139
	public function isVerbose()
140
	{
141
		return $this->output->isVerbose();
142
	}
143
144
145
146
	/**
147
	 * {@inheritdoc}
148
	 */
149
	public function isVeryVerbose()
150
	{
151
		return $this->output->isVeryVerbose();
152
	}
153
154
155
156
	/**
157
	 * {@inheritdoc}
158
	 */
159
	public function isDebug()
160
	{
161
		return $this->output->isDebug();
162
	}
163
164
165
166
	/**
167
	 * {@inheritdoc}
168
	 */
169
	public function setFormatter(OutputFormatterInterface $formatter)
170
	{
171
		return $this->output->setFormatter($formatter);
172
	}
173
174
175
176
	/**
177
	 * {@inheritdoc}
178
	 */
179
	public function getFormatter()
180
	{
181
		return $this->output->getFormatter();
182
	}
183
184
}
185