-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathDatabaseReverseCommand.php
More file actions
144 lines (122 loc) · 5.95 KB
/
DatabaseReverseCommand.php
File metadata and controls
144 lines (122 loc) · 5.95 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
<?php
/**
* MIT License. This file is part of the Propel package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Propel\Generator\Command;
use Propel\Generator\Manager\ReverseManager;
use Propel\Generator\Schema\Dumper\XmlDumper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @author William Durand <william.durand1@gmail.com>
*/
class DatabaseReverseCommand extends AbstractCommand
{
public const DEFAULT_OUTPUT_DIRECTORY = 'generated-reversed-database';
public const DEFAULT_DATABASE_NAME = 'default';
public const DEFAULT_SCHEMA_NAME = 'schema';
/**
* @inheritDoc
*/
protected function configure()
{
parent::configure();
$this
->addOption('output-dir', null, InputOption::VALUE_REQUIRED, 'The output directory', self::DEFAULT_OUTPUT_DIRECTORY)
->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'The database name used in the created schema.xml. If not defined we use `connection`.')
->addOption('schema-name', null, InputOption::VALUE_REQUIRED, 'The schema name to generate', self::DEFAULT_SCHEMA_NAME)
->addOption('namespace', null, InputOption::VALUE_OPTIONAL, 'The PHP namespace to use for generated models')
->addArgument(
'connection',
InputArgument::OPTIONAL,
'Connection name or dsn to use. Example: \'mysql:host=127.0.0.1;dbname=test;user=root;password=foobar\' (don\'t forget the quote for dsn)',
NULL
)
->setName('database:reverse')
->setAliases(['reverse'])
->setDescription('Reverse-engineer a XML schema file based on given database. Uses given `connection` as name, as dsn or your `reverse.connection` configuration in propel config as connection.');
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$configOptions = [];
$connection = (string) $input->getArgument('connection');
if ($connection !== NULL && false === strpos($connection, ':')) {
//treat it as connection name
$configOptions['propel']['reverse']['connection'] = $connection;
if (!$input->getOption('database-name')) {
$input->setOption('database-name', $connection);
}
} else if ($connection !== NULL) {
//probably a dsn
$configOptions += $this->connectionToProperties('reverseconnection=' . $connection, 'reverse');
$configOptions['propel']['reverse']['parserClass'] = sprintf(
'\\Propel\\Generator\\Reverse\\%sSchemaParser',
ucfirst($configOptions['propel']['database']['connections']['reverseconnection']['adapter'])
);
if (!$input->getOption('database-name')) {
$input->setOption('database-name', self::DEFAULT_DATABASE_NAME);
}
}
if ($input->getOption('namespace')) {
$configOptions['propel']['reverse']['namespace'] = $input->getOption('namespace');
}
if ($input->getOption('output-dir') !== self::DEFAULT_OUTPUT_DIRECTORY) {
$configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir');
}
if ($input->getOption('schema-name') !== self::DEFAULT_SCHEMA_NAME) {
$configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name');
}
$generatorConfig = $this->getGeneratorConfig($configOptions, $input);
$outputDir = $generatorConfig->getConfigProperty('paths.schemaDir');
$shemaName = $generatorConfig->getConfigProperty('generator.schema.basename');
if (empty($outputDir) || empty($shemaName)) {
// if output dir or shema name is still empty, we have to set them
// to his default values, in order to keep the previous
// behaviour of the script
if (empty($outputDir)) {
$configOptions['propel']['paths']['schemaDir'] = $input->getOption('output-dir');
}
if (empty($shemaName)) {
$configOptions['propel']['generator']['schema']['basename'] = $input->getOption('schema-name');
}
// regenerate config with the new options
$generatorConfig = $this->getGeneratorConfig($configOptions, $input);exit(1);
}
if (!$connection) {
$connection = $generatorConfig->getConfigProperty('reverse.connection');
if ($connection && !$input->getOption('database-name')) {
$input->setOption('database-name', $connection);
}
}
$this->createDirectory(
$generatorConfig->getConfigProperty('paths.schemaDir'));
$manager = new ReverseManager(new XmlDumper());
$manager->setGeneratorConfig($generatorConfig);
$manager->setLoggerClosure(function ($message) use ($input, $output) {
if ($input->getOption('verbose')) {
$output->writeln($message);
}
});
$manager->setWorkingDirectory(
$generatorConfig->getConfigProperty('paths.schemaDir'));
$manager->setDatabaseName($input->getOption('database-name'));
$manager->setSchemaName(
$generatorConfig->getConfigProperty('generator.schema.basename'));
$namespace = $generatorConfig->getConfigProperty('reverse.namespace');
$namespace = $input->getOption('namespace');
if ($namespace) {
$manager->setNamespace($namespace);
}
if ($manager->reverse() === true) {
$output->writeln('<info>Schema reverse engineering finished.</info>');
}
return static::CODE_SUCCESS;
}
}