|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the LGPL. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Migrations\Tools\Console\Command; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Configuration; |
|
23
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ArrayConnectionConfigurationLoader; |
|
24
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationLoader; |
|
25
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionHelperLoader; |
|
26
|
|
|
use Doctrine\DBAL\Migrations\Configuration\Connection\Loader\ConnectionConfigurationChainLoader; |
|
27
|
|
|
use Doctrine\DBAL\Migrations\OutputWriter; |
|
28
|
|
|
use Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper; |
|
29
|
|
|
use Symfony\Component\Console\Command\Command; |
|
30
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
31
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
32
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
33
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* CLI Command for adding and deleting migration versions from the version table. |
|
37
|
|
|
* |
|
38
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
39
|
|
|
* @link www.doctrine-project.org |
|
40
|
|
|
* @since 2.0 |
|
41
|
|
|
* @author Jonathan Wage <[email protected]> |
|
42
|
|
|
*/ |
|
43
|
|
|
abstract class AbstractCommand extends Command |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* The configuration property only contains the configuration injected by the setter. |
|
47
|
|
|
* |
|
48
|
|
|
* @var Configuration |
|
49
|
|
|
*/ |
|
50
|
|
|
private $configuration; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The migrationConfiguration property contains the configuration |
|
54
|
|
|
* created taking into account the command line options. |
|
55
|
|
|
* |
|
56
|
|
|
* @var Configuration |
|
57
|
|
|
*/ |
|
58
|
|
|
private $migrationConfiguration; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var OutputWriter |
|
62
|
|
|
*/ |
|
63
|
|
|
private $outputWriter; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @var \Doctrine\DBAL\Connection |
|
67
|
|
|
*/ |
|
68
|
|
|
private $connection; |
|
69
|
|
|
|
|
70
|
50 |
|
protected function configure() |
|
71
|
|
|
{ |
|
72
|
50 |
|
$this->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.'); |
|
73
|
50 |
|
$this->addOption('db-configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a database connection configuration file.'); |
|
74
|
50 |
|
} |
|
75
|
|
|
|
|
76
|
11 |
|
protected function outputHeader(Configuration $configuration, OutputInterface $output) |
|
77
|
|
|
{ |
|
78
|
11 |
|
$name = $configuration->getName(); |
|
79
|
11 |
|
$name = $name ? $name : 'Doctrine Database Migrations'; |
|
80
|
11 |
|
$name = str_repeat(' ', 20) . $name . str_repeat(' ', 20); |
|
81
|
11 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
|
82
|
11 |
|
$output->writeln('<question>' . $name . '</question>'); |
|
83
|
11 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
|
84
|
11 |
|
$output->writeln(''); |
|
85
|
11 |
|
} |
|
86
|
|
|
|
|
87
|
21 |
|
public function setMigrationConfiguration(Configuration $config) |
|
88
|
|
|
{ |
|
89
|
21 |
|
$this->configuration = $config; |
|
90
|
21 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* When any (config) command line option is passed to the migration the migrationConfiguration |
|
94
|
|
|
* property is set with the new generated configuration. |
|
95
|
|
|
* If no (config) option is passed the migrationConfiguration property is set to the value |
|
96
|
|
|
* of the configuration one (if any). |
|
97
|
|
|
* Else a new configuration is created and assigned to the migrationConfiguration property. |
|
98
|
|
|
* |
|
99
|
|
|
* @param InputInterface $input |
|
100
|
|
|
* @param OutputInterface $output |
|
101
|
|
|
* |
|
102
|
|
|
* @return Configuration |
|
103
|
|
|
*/ |
|
104
|
34 |
|
protected function getMigrationConfiguration(InputInterface $input, OutputInterface $output) |
|
105
|
|
|
{ |
|
106
|
34 |
|
if (!$this->migrationConfiguration) { |
|
107
|
34 |
|
if ($this->getHelperSet()->has('configuration') |
|
108
|
34 |
|
&& $this->getHelperSet()->get('configuration') instanceof ConfigurationHelper) { |
|
109
|
1 |
|
$configHelper = $this->getHelperSet()->get('configuration'); |
|
110
|
|
|
} else { |
|
111
|
33 |
|
$configHelper = new ConfigurationHelper($this->getConnection($input), $this->configuration); |
|
112
|
|
|
} |
|
113
|
33 |
|
$this->migrationConfiguration = $configHelper->getMigrationConfig($input, $this->getOutputWriter($output)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
33 |
|
return $this->migrationConfiguration; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $question |
|
121
|
|
|
* @param InputInterface $input |
|
122
|
|
|
* @param OutputInterface $output |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
6 |
|
protected function askConfirmation($question, InputInterface $input, OutputInterface $output) |
|
126
|
|
|
{ |
|
127
|
6 |
|
return $this->getHelper('question')->ask($input, $output, new ConfirmationQuestion($question)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
|
132
|
|
|
* |
|
133
|
|
|
* @return \Doctrine\DBAL\Migrations\OutputWriter |
|
134
|
|
|
*/ |
|
135
|
33 |
|
private function getOutputWriter(OutputInterface $output) |
|
136
|
|
|
{ |
|
137
|
33 |
|
if (!$this->outputWriter) { |
|
138
|
33 |
|
$this->outputWriter = new OutputWriter(function($message) use ($output) { |
|
139
|
31 |
|
return $output->writeln($message); |
|
140
|
33 |
|
}); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
33 |
|
return $this->outputWriter; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Doctrine\DBAL\Connection |
|
150
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
151
|
|
|
*/ |
|
152
|
33 |
|
private function getConnection(InputInterface $input) |
|
153
|
|
|
{ |
|
154
|
33 |
|
if ($this->connection) { |
|
155
|
|
|
return $this->connection; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
33 |
|
$chainLoader = new ConnectionConfigurationChainLoader( |
|
159
|
|
|
[ |
|
160
|
33 |
|
new ArrayConnectionConfigurationLoader($input->getOption('db-configuration')), |
|
161
|
33 |
|
new ArrayConnectionConfigurationLoader('migrations-db.php'), |
|
162
|
33 |
|
new ConnectionHelperLoader($this->getHelperSet(), 'connection'), |
|
163
|
33 |
|
new ConnectionConfigurationLoader($this->configuration), |
|
164
|
|
|
] |
|
165
|
|
|
); |
|
166
|
33 |
|
$connection = $chainLoader->chosen(); |
|
167
|
|
|
|
|
168
|
33 |
|
if ($connection) { |
|
169
|
32 |
|
return $this->connection = $connection; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
1 |
|
throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.'); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|