1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Migrations\Tools\Console\Command; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
6
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader; |
7
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader; |
8
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader; |
9
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationChainLoader; |
10
|
|
|
use Doctrine\DBAL\Migrations\OutputWriter; |
11
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* CLI Command for adding and deleting migration versions from the version table. |
20
|
|
|
* |
21
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
22
|
|
|
* @link www.doctrine-project.org |
23
|
|
|
* @since 2.0 |
24
|
|
|
* @author Jonathan Wage <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
abstract class AbstractCommand extends Command |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* The configuration property only contains the configuration injected by the setter. |
30
|
|
|
* |
31
|
|
|
* @var Configuration |
32
|
|
|
*/ |
33
|
|
|
private $configuration; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The migrationConfiguration property contains the configuration |
37
|
|
|
* created taking into account the command line options. |
38
|
|
|
* |
39
|
|
|
* @var Configuration |
40
|
|
|
*/ |
41
|
|
|
private $migrationConfiguration; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var OutputWriter |
45
|
|
|
*/ |
46
|
|
|
private $outputWriter; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var \Doctrine\DBAL\Connection |
50
|
|
|
*/ |
51
|
|
|
private $connection; |
52
|
|
|
|
53
|
52 |
|
protected function configure() |
54
|
|
|
{ |
55
|
52 |
|
$this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'); |
56
|
52 |
|
$this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'); |
57
|
52 |
|
} |
58
|
|
|
|
59
|
11 |
|
protected function outputHeader(Configuration $configuration, OutputInterface $output) |
60
|
|
|
{ |
61
|
11 |
|
$name = $configuration->getName(); |
62
|
11 |
|
$name = $name ? $name : 'Doctrine Database Migrations'; |
63
|
11 |
|
$name = str_repeat(' ', 20) . $name . str_repeat(' ', 20); |
64
|
11 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
65
|
11 |
|
$output->writeln('<question>' . $name . '</question>'); |
66
|
11 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
67
|
11 |
|
$output->writeln(''); |
68
|
11 |
|
} |
69
|
|
|
|
70
|
23 |
|
public function setMigrationConfiguration(Configuration $config) |
71
|
|
|
{ |
72
|
23 |
|
$this->configuration = $config; |
73
|
23 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* When any (config) command line option is passed to the migration the migrationConfiguration |
77
|
|
|
* property is set with the new generated configuration. |
78
|
|
|
* If no (config) option is passed the migrationConfiguration property is set to the value |
79
|
|
|
* of the configuration one (if any). |
80
|
|
|
* Else a new configuration is created and assigned to the migrationConfiguration property. |
81
|
|
|
* |
82
|
|
|
* @param InputInterface $input |
83
|
|
|
* @param OutputInterface $output |
84
|
|
|
* |
85
|
|
|
* @return Configuration |
86
|
|
|
*/ |
87
|
36 |
|
protected function getMigrationConfiguration(InputInterface $input, OutputInterface $output) |
88
|
|
|
{ |
89
|
36 |
|
if ( ! $this->migrationConfiguration) { |
90
|
36 |
|
if ($this->getHelperSet()->has('configuration') |
91
|
36 |
|
&& $this->getHelperSet()->get('configuration') instanceof ConfigurationHelper) { |
92
|
1 |
|
$configHelper = $this->getHelperSet()->get('configuration'); |
93
|
|
|
} else { |
94
|
35 |
|
$configHelper = new ConfigurationHelper($this->getConnection($input), $this->configuration); |
95
|
|
|
} |
96
|
35 |
|
$this->migrationConfiguration = $configHelper->getMigrationConfig($input, $this->getOutputWriter($output)); |
97
|
|
|
} |
98
|
|
|
|
99
|
35 |
|
return $this->migrationConfiguration; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $question |
104
|
|
|
* @param InputInterface $input |
105
|
|
|
* @param OutputInterface $output |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
6 |
|
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) |
109
|
|
|
{ |
110
|
6 |
|
return $this->getHelper('question')->ask($input, $output, new ConfirmationQuestion($question)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
115
|
|
|
* |
116
|
|
|
* @return \Doctrine\DBAL\Migrations\OutputWriter |
117
|
|
|
*/ |
118
|
35 |
|
private function getOutputWriter(OutputInterface $output) |
119
|
|
|
{ |
120
|
35 |
|
if ( ! $this->outputWriter) { |
121
|
35 |
|
$this->outputWriter = new OutputWriter(function ($message) use ($output) { |
122
|
33 |
|
return $output->writeln($message); |
123
|
35 |
|
}); |
124
|
|
|
} |
125
|
|
|
|
126
|
35 |
|
return $this->outputWriter; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
131
|
|
|
* |
132
|
|
|
* @return \Doctrine\DBAL\Connection |
133
|
|
|
* @throws \Doctrine\DBAL\DBALException |
134
|
|
|
*/ |
135
|
35 |
|
private function getConnection(InputInterface $input) |
136
|
|
|
{ |
137
|
35 |
|
if ($this->connection) { |
138
|
|
|
return $this->connection; |
139
|
|
|
} |
140
|
|
|
|
141
|
35 |
|
$chainLoader = new ConnectionConfigurationChainLoader( |
142
|
|
|
[ |
143
|
35 |
|
new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')), |
144
|
35 |
|
new ArrayConnectionConfigurationLoader('migrations-db.php'), |
145
|
35 |
|
new ConnectionHelperLoader($this->getHelperSet(), 'connection'), |
146
|
35 |
|
new ConnectionConfigurationLoader($this->configuration), |
147
|
|
|
] |
148
|
|
|
); |
149
|
35 |
|
$connection = $chainLoader->chosen(); |
150
|
|
|
|
151
|
35 |
|
if ($connection) { |
152
|
34 |
|
return $this->connection = $connection; |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|