1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Pomm's Cli package. |
4
|
|
|
* |
5
|
|
|
* (c) 2014 - 2015 Grégoire HUBERT <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace PommProject\Cli\Command; |
11
|
|
|
|
12
|
|
|
use PommProject\Foundation\ResultIterator; |
13
|
|
|
use Symfony\Component\Console\Helper\Table; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* InspectDatabase |
19
|
|
|
* |
20
|
|
|
* Return the list of schemas in the current database. |
21
|
|
|
* |
22
|
|
|
* @package Cli |
23
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
24
|
|
|
* @author Grégoire HUBERT |
25
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
26
|
|
|
* @see PommAwareCommand |
27
|
|
|
*/ |
28
|
|
|
class InspectDatabase extends SessionAwareCommand |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* configure |
32
|
|
|
* |
33
|
|
|
* @see Command |
34
|
|
|
*/ |
35
|
|
|
public function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setName('pomm:inspect:database') |
39
|
|
|
->setDescription('Show schemas in the current database.') |
40
|
|
|
; |
41
|
|
|
|
42
|
|
|
parent::configure(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* execute |
47
|
|
|
* |
48
|
|
|
* @see Command |
49
|
|
|
*/ |
50
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
51
|
|
|
{ |
52
|
|
|
parent::execute($input, $output); |
53
|
|
|
$info = $this->getSession()->getInspector('schema')->getUserSchemas(); |
54
|
|
|
$version = $this->getSession()->getInspector('database')->getVersion(); |
55
|
|
|
$size = $this->getSession()->getInspector('database')->getSizePretty(); |
56
|
|
|
$name = $this->getSession()->getInspector('database')->getName(); |
57
|
|
|
$this->formatOutput($output, $info, $version, $size, $name); |
58
|
|
|
|
59
|
|
|
return 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* formatOutput |
64
|
|
|
* |
65
|
|
|
* Format command output from the inspector's result. |
66
|
|
|
* |
67
|
|
|
* @access protected |
68
|
|
|
* @param OutputInterface $output |
69
|
|
|
* @param ResultIterator $iterator |
70
|
|
|
* @return null |
71
|
|
|
*/ |
72
|
|
|
protected function formatOutput(OutputInterface $output, ResultIterator $iterator, $version, $size, $name) |
73
|
|
|
{ |
74
|
|
|
$output->writeln( |
75
|
|
|
sprintf( |
76
|
|
|
"PostgreSQL v.%s", |
77
|
|
|
$version |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
$output->writeln( |
81
|
|
|
sprintf( |
82
|
|
|
"Found <info>%d</info> schemas in database \"%s\" (%s).", |
83
|
|
|
$iterator->count(), |
84
|
|
|
$name, |
85
|
|
|
$size |
86
|
|
|
) |
87
|
|
|
); |
88
|
|
|
$table = (new Table($output)) |
89
|
|
|
->setHeaders(['name', 'oid ', 'relations', 'owner', 'comment']) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
View Code Duplication |
foreach ($iterator as $schema_info) { |
|
|
|
|
93
|
|
|
$table->addRow([ |
94
|
|
|
sprintf("<fg=yellow>%s</fg=yellow>", $schema_info['name']), |
95
|
|
|
$schema_info['oid'], |
96
|
|
|
$schema_info['relations'], |
97
|
|
|
$schema_info['owner'], |
98
|
|
|
wordwrap($schema_info['comment']) |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$table->render(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.