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 PommProject\Foundation\Where; |
14
|
|
|
use Symfony\Component\Console\Helper\Table; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use PommProject\Cli\Exception\CliException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* InspectSchema |
21
|
|
|
* |
22
|
|
|
* Inspector from the command line. |
23
|
|
|
* |
24
|
|
|
* @package Cli |
25
|
|
|
* @copyright 2014 - 2015 Grégoire HUBERT |
26
|
|
|
* @author Grégoire HUBERT |
27
|
|
|
* @license X11 {@link http://opensource.org/licenses/mit-license.php} |
28
|
|
|
* @see SchemaAwareCommand |
29
|
|
|
*/ |
30
|
|
|
class InspectSchema extends SchemaAwareCommand |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* configure |
34
|
|
|
* |
35
|
|
|
* @see Command |
36
|
|
|
*/ |
37
|
|
|
public function configure() |
38
|
|
|
{ |
39
|
|
|
$this |
40
|
|
|
->setName('pomm:inspect:schema') |
41
|
|
|
->setDescription('Show relations in a given schema.') |
42
|
|
|
; |
43
|
|
|
|
44
|
|
|
parent::configure(); |
45
|
|
|
} |
46
|
|
|
/** |
47
|
|
|
* execute |
48
|
|
|
* |
49
|
|
|
* @see Command |
50
|
|
|
*/ |
51
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
|
|
parent::execute($input, $output); |
54
|
|
|
$inspector = $this->getSession()->getInspector('schema'); |
55
|
|
|
$schema_info = $inspector |
56
|
|
|
->getUserSchemas(new Where("n.nspname = $*", [$this->schema])) |
57
|
|
|
->current(); |
58
|
|
|
|
59
|
|
|
if ($schema_info === null) { |
60
|
|
|
throw new CliException( |
61
|
|
|
sprintf( |
62
|
|
|
"Could not find schema '%s'.\nAvailable schemas are {%s}", |
63
|
|
|
$this->schema, |
64
|
|
|
join(', ', $inspector->getUserSchemas()->slice('name')) |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
$info = $this |
69
|
|
|
->getSession() |
70
|
|
|
->getInspector('relation') |
71
|
|
|
->getRelationsInSchema($this->schema); |
72
|
|
|
$this->formatOutput($output, $info); |
73
|
|
|
|
74
|
|
|
return 0; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* formatOutput |
79
|
|
|
* |
80
|
|
|
* Format result |
81
|
|
|
* |
82
|
|
|
* @access protected |
83
|
|
|
* @param OutputInterface $output |
84
|
|
|
* @param ResultIterator $info |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
protected function formatOutput(OutputInterface $output, ResultIterator $info) |
88
|
|
|
{ |
89
|
|
|
$output->writeln( |
90
|
|
|
sprintf( |
91
|
|
|
"Found <info>%d</info> relations in schema <info>'%s'</info>.", |
92
|
|
|
$info->count(), |
93
|
|
|
$this->schema |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
$table = (new Table($output)) |
97
|
|
|
->setHeaders(['name', 'type', 'oid ', 'owner', 'size', 'comment']) |
98
|
|
|
; |
99
|
|
|
|
100
|
|
View Code Duplication |
foreach ($info as $table_info) { |
|
|
|
|
101
|
|
|
$table->addRow([ |
102
|
|
|
sprintf("<fg=yellow>%s</fg=yellow>", $table_info['name']), |
103
|
|
|
$table_info['type'], |
104
|
|
|
$table_info['oid'], |
105
|
|
|
$table_info['owner'], |
106
|
|
|
$table_info['size'], |
107
|
|
|
wordwrap($table_info['comment']) |
108
|
|
|
]); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$table->render(); |
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.