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