|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Database; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use N98\Util\Console\Helper\DatabaseHelper; |
|
7
|
|
|
use N98\Util\Console\Helper\Table\Renderer\RendererFactory; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
class InfoCommand extends AbstractDatabaseCommand |
|
14
|
|
|
{ |
|
15
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
parent::configure(); |
|
18
|
|
|
$this |
|
19
|
|
|
->setName('db:info') |
|
20
|
|
|
->addArgument('setting', InputArgument::OPTIONAL, 'Only output value of named setting') |
|
21
|
|
|
->setDescription('Dumps database informations') |
|
22
|
|
|
->addOption( |
|
23
|
|
|
'format', |
|
24
|
|
|
null, |
|
25
|
|
|
InputOption::VALUE_OPTIONAL, |
|
26
|
|
|
'Output Format. One of [' . implode(',', RendererFactory::getFormats()) . ']' |
|
27
|
|
|
) |
|
28
|
|
|
; |
|
29
|
|
|
$this->addDeprecatedAlias('database:info', 'Please use db:info'); |
|
30
|
|
|
|
|
31
|
|
|
$help = <<<HELP |
|
32
|
|
|
This command is useful to print all informations about the current configured database in app/etc/env.php. |
|
33
|
|
|
It can print connection string for JDBC, PDO connections. |
|
34
|
|
|
HELP; |
|
35
|
|
|
$this->setHelp($help); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param InputInterface $input |
|
40
|
|
|
* @param OutputInterface $output |
|
41
|
|
|
* |
|
42
|
|
|
* @throws InvalidArgumentException |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->detectDbSettings($output); |
|
48
|
|
|
|
|
49
|
|
|
$settings = array(); |
|
50
|
|
|
foreach ($this->dbSettings as $key => $value) { |
|
51
|
|
|
$settings[$key] = (string) $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$isSocketConnect = $this->isSocketConnect; |
|
55
|
|
|
|
|
56
|
|
|
// note: there is no need to specify the default port neither for PDO, nor JDBC nor CLI. |
|
57
|
|
|
$portOrDefault = isset($this->dbSettings['port']) ? $this->dbSettings['port'] : 3306; |
|
58
|
|
|
|
|
59
|
|
|
$pdoConnectionString = ''; |
|
|
|
|
|
|
60
|
|
View Code Duplication |
if ($isSocketConnect) { |
|
|
|
|
|
|
61
|
|
|
$pdoConnectionString = sprintf( |
|
62
|
|
|
'mysql:unix_socket=%s;dbname=%s', |
|
63
|
|
|
$this->dbSettings['unix_socket'], |
|
64
|
|
|
$this->dbSettings['dbname'] |
|
65
|
|
|
); |
|
66
|
|
|
} else { |
|
67
|
|
|
$pdoConnectionString = sprintf( |
|
68
|
|
|
'mysql:host=%s;port=%s;dbname=%s', |
|
69
|
|
|
$this->dbSettings['host'], |
|
70
|
|
|
$portOrDefault, |
|
71
|
|
|
$this->dbSettings['dbname'] |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
$settings['PDO-Connection-String'] = $pdoConnectionString; |
|
75
|
|
|
|
|
76
|
|
|
$jdbcConnectionString = ''; |
|
|
|
|
|
|
77
|
|
View Code Duplication |
if ($isSocketConnect) { |
|
|
|
|
|
|
78
|
|
|
// isn't supported according to this post: http://stackoverflow.com/a/18493673/145829 |
|
79
|
|
|
$jdbcConnectionString = 'Connecting using JDBC through a unix socket isn\'t supported!'; |
|
80
|
|
|
} else { |
|
81
|
|
|
$jdbcConnectionString = sprintf( |
|
82
|
|
|
'jdbc:mysql://%s:%s/%s?username=%s&password=%s', |
|
83
|
|
|
$this->dbSettings['host'], |
|
84
|
|
|
$portOrDefault, |
|
85
|
|
|
$this->dbSettings['dbname'], |
|
86
|
|
|
$this->dbSettings['username'], |
|
87
|
|
|
$this->dbSettings['password'] |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
$settings['JDBC-Connection-String'] = $jdbcConnectionString; |
|
91
|
|
|
|
|
92
|
|
|
/* @var $database DatabaseHelper */ |
|
93
|
|
|
$database = $this->getHelper('database'); |
|
94
|
|
|
$mysqlCliString = 'mysql ' . $database->getMysqlClientToolConnectionString(); |
|
95
|
|
|
$settings['MySQL-Cli-String'] = $mysqlCliString; |
|
96
|
|
|
|
|
97
|
|
|
$rows = array(); |
|
98
|
|
|
foreach ($settings as $settingName => $settingValue) { |
|
99
|
|
|
$rows[] = array($settingName, $settingValue); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (($settingArgument = $input->getArgument('setting')) !== null) { |
|
103
|
|
|
if (!isset($settings[$settingArgument])) { |
|
104
|
|
|
throw new InvalidArgumentException('Unknown setting: ' . $settingArgument); |
|
105
|
|
|
} |
|
106
|
|
|
$output->writeln((string) $settings[$settingArgument]); |
|
107
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
108
|
|
|
$this->getHelper('table') |
|
109
|
|
|
->setHeaders(array('Name', 'Value')) |
|
110
|
|
|
->renderByFormat($output, $rows, $input->getOption('format')); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.