-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
executable file
·205 lines (180 loc) · 4.64 KB
/
Application.php
File metadata and controls
executable file
·205 lines (180 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
namespace Nitso\SqlConsole;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Nitso\SqlConsole\Command;
use Nitso\SqlConsole\Command\Auxiliary as AuxCommand;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class Application extends ConsoleApplication
{
/**
* @var Connection
*/
private $connection;
/**
* @var Configuration
*/
private $configuration;
/**
* @var string
*/
private $prompt;
/**
* @var string[]
*/
private $dsnParams;
/**
* @var string
*/
private $dsn;
/**
* @inheritdoc
*/
public function getDefaultCommands()
{
return array(
new ListCommand(),
new HelpCommand(),
new Command\Connect(),
new Command\Disconnect(),
new Command\Status(),
new Command\E(),
new Command\Show(),
new Command\UseDB(),
new AuxCommand\ExitCommand(),
new AuxCommand\EmptyCommand(),
);
}
/**
* @throws \Doctrine\DBAL\DBALException
*/
public function connect()
{
$this->configuration = new Configuration();
$this->connection = DriverManager::getConnection(array('url' => $this->dsn), $this->configuration);
$this->connection->connect();
$this->setConnectionPrompt();
}
/**
* @return void
*/
public function setConnectionPrompt()
{
if (!$this->connection || !$this->connection->isConnected()) {
$this->setPrompt($this->getName());
}
$this->setPrompt(sprintf(
'Connected (%s/%s)',
$this->connection->getHost(),
$this->connection->getDatabase()
));
}
/**
* @return Connection
*/
public function getConnection()
{
return $this->connection;
}
/**
* @return string
*/
public function getPrompt()
{
return $this->prompt ?: $this->getName();
}
/**
* @param string $prompt
* @return $this
*/
public function setPrompt($prompt)
{
$this->prompt = $prompt;
return $this;
}
/**
* Stupid hack just to reuse protected IO configuration code. Sorry for that.
*
* @see Shell::run
* @inheritdoc
*/
public function preConfigureIO(InputInterface $input, OutputInterface $output)
{
parent::configureIO($input, $output);
}
/**
* @return string
*/
public function getDatabase()
{
return empty($this->dsnParams['path']) ? '' : ltrim('/', $this->dsnParams['path']);
}
/**
* @param string $database
* @return $this
*/
public function setDatabase($database)
{
$params = $this->getDsnParams();
$params['path'] = '/' . $database;
$this->setDsnParams($params);
return $this;
}
/**
* @return string[]
*/
protected function getDsnParams()
{
return $this->dsnParams;
}
/**
* @param array $dsnParams
* @return $this
*/
public function setDsnParams($dsnParams)
{
$this->dsnParams = $dsnParams;
$this->dsn = $this->buildUrl($this->dsnParams);
return $this;
}
/**
* @return string
*/
protected function getDsn()
{
return $this->dsn;
}
/**
* @param string $dsn
* @return $this
*/
public function setDsn($dsn)
{
$this->dsn = $dsn;
$this->dsnParams = parse_url($dsn);
return $this;
}
/**
* SO-driven development https://stackoverflow.com/a/35207936/824926
* @param array $parts
* @return string
*/
private function buildUrl(array $parts) {
return
(isset($parts['scheme']) ? "{$parts['scheme']}:" : '') .
((isset($parts['user']) || isset($parts['host'])) ? '//' : '') .
(isset($parts['user']) ? "{$parts['user']}" : '') .
(isset($parts['pass']) ? ":{$parts['pass']}" : '') .
(isset($parts['user']) ? '@' : '') .
(isset($parts['host']) ? "{$parts['host']}" : '') .
(isset($parts['port']) ? ":{$parts['port']}" : '') .
(isset($parts['path']) ? "{$parts['path']}" : '') .
(isset($parts['query']) ? "?{$parts['query']}" : '') .
(isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
}
}