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\DependencyFactory; |
10
|
|
|
use Doctrine\Migrations\MigrationRepository; |
11
|
|
|
use Doctrine\Migrations\Tools\Console\ConnectionLoader; |
12
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelper; |
13
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\ConfigurationHelperInterface; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Helper\HelperSet; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
20
|
|
|
use function escapeshellarg; |
21
|
|
|
use function proc_open; |
22
|
|
|
use function str_repeat; |
23
|
|
|
use function strlen; |
24
|
|
|
|
25
|
|
|
abstract class AbstractCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** @var Configuration */ |
28
|
|
|
protected $configuration; |
29
|
|
|
|
30
|
|
|
/** @var Connection */ |
31
|
|
|
protected $connection; |
32
|
|
|
|
33
|
|
|
/** @var DependencyFactory */ |
34
|
|
|
protected $dependencyFactory; |
35
|
|
|
|
36
|
|
|
/** @var MigrationRepository */ |
37
|
|
|
protected $migrationRepository; |
38
|
|
|
|
39
|
|
|
/** @var Configuration|null */ |
40
|
|
|
protected $migrationConfiguration; |
41
|
|
|
|
42
|
22 |
|
public function setMigrationConfiguration(Configuration $configuration) : void |
43
|
|
|
{ |
44
|
22 |
|
$this->configuration = $configuration; |
45
|
|
|
|
46
|
22 |
|
$this->initializeDependencies(); |
47
|
22 |
|
} |
48
|
|
|
|
49
|
1 |
|
public function setConnection(Connection $connection) : void |
50
|
|
|
{ |
51
|
1 |
|
$this->connection = $connection; |
52
|
1 |
|
} |
53
|
|
|
|
54
|
17 |
|
public function setDependencyFactory(DependencyFactory $dependencyFactory) : void |
55
|
|
|
{ |
56
|
17 |
|
$this->dependencyFactory = $dependencyFactory; |
57
|
17 |
|
} |
58
|
|
|
|
59
|
23 |
|
public function setMigrationRepository(MigrationRepository $migrationRepository) : void |
60
|
|
|
{ |
61
|
23 |
|
$this->migrationRepository = $migrationRepository; |
62
|
23 |
|
} |
63
|
|
|
|
64
|
24 |
|
public function initialize( |
65
|
|
|
InputInterface $input, |
66
|
|
|
OutputInterface $output |
67
|
|
|
) : void { |
68
|
24 |
|
$this->configuration = $this->getMigrationConfiguration($input, $output); |
69
|
|
|
|
70
|
24 |
|
$this->initializeDependencies(); |
71
|
|
|
|
72
|
24 |
|
$this->configuration->validate(); |
73
|
24 |
|
$this->configuration->createMigrationTable(); |
74
|
24 |
|
} |
75
|
|
|
|
76
|
59 |
|
protected function configure() : void |
77
|
|
|
{ |
78
|
59 |
|
$this->addOption( |
79
|
59 |
|
'configuration', |
80
|
59 |
|
null, |
81
|
59 |
|
InputOption::VALUE_OPTIONAL, |
82
|
59 |
|
'The path to a migrations configuration file.' |
83
|
|
|
); |
84
|
|
|
|
85
|
59 |
|
$this->addOption( |
86
|
59 |
|
'db-configuration', |
87
|
59 |
|
null, |
88
|
59 |
|
InputOption::VALUE_OPTIONAL, |
89
|
59 |
|
'The path to a database connection configuration file.' |
90
|
|
|
); |
91
|
59 |
|
} |
92
|
|
|
|
93
|
13 |
|
protected function outputHeader( |
94
|
|
|
OutputInterface $output |
95
|
|
|
) : void { |
96
|
13 |
|
$name = $this->configuration->getName(); |
97
|
13 |
|
$name = $name ?? 'Doctrine Database Migrations'; |
98
|
13 |
|
$name = str_repeat(' ', 20) . $name . str_repeat(' ', 20); |
99
|
13 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
100
|
13 |
|
$output->writeln('<question>' . $name . '</question>'); |
101
|
13 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
102
|
13 |
|
$output->writeln(''); |
103
|
13 |
|
} |
104
|
|
|
|
105
|
20 |
|
protected function getMigrationConfiguration( |
106
|
|
|
InputInterface $input, |
107
|
|
|
OutputInterface $output |
108
|
|
|
) : Configuration { |
109
|
20 |
|
if ($this->migrationConfiguration === null) { |
110
|
20 |
|
if ($this->hasConfigurationHelper()) { |
111
|
|
|
/** @var ConfigurationHelper $configHelper */ |
112
|
1 |
|
$configHelper = $this->getHelperSet()->get('configuration'); |
113
|
|
|
} else { |
114
|
19 |
|
$configHelper = new ConfigurationHelper( |
115
|
19 |
|
$this->getConnection($input), |
116
|
18 |
|
$this->configuration |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
19 |
|
$this->migrationConfiguration = $configHelper->getMigrationConfig($input); |
121
|
|
|
|
122
|
19 |
|
$this->migrationConfiguration->getOutputWriter()->setCallback( |
123
|
|
|
function (string $message) use ($output) : void { |
124
|
1 |
|
$output->writeln($message); |
125
|
19 |
|
} |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
19 |
|
if ($this->migrationConfiguration === null && $this->configuration instanceof Configuration) { |
130
|
|
|
$this->migrationConfiguration = $this->configuration; |
131
|
|
|
} |
132
|
|
|
|
133
|
19 |
|
return $this->migrationConfiguration; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
protected function askConfirmation( |
137
|
|
|
string $question, |
138
|
|
|
InputInterface $input, |
139
|
|
|
OutputInterface $output |
140
|
|
|
) : bool { |
141
|
1 |
|
return $this->getHelper('question')->ask( |
142
|
1 |
|
$input, |
143
|
1 |
|
$output, |
144
|
1 |
|
new ConfirmationQuestion($question) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
protected function canExecute( |
149
|
|
|
string $question, |
150
|
|
|
InputInterface $input, |
151
|
|
|
OutputInterface $output |
152
|
|
|
) : bool { |
153
|
1 |
|
if ($input->isInteractive() && ! $this->askConfirmation($question, $input, $output)) { |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return true; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
protected function procOpen(string $editorCommand, string $path) : void |
161
|
|
|
{ |
162
|
1 |
|
proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes); |
163
|
1 |
|
} |
164
|
|
|
|
165
|
46 |
|
private function initializeDependencies() : void |
166
|
|
|
{ |
167
|
46 |
|
$this->connection = $this->configuration->getConnection(); |
168
|
46 |
|
$this->dependencyFactory = $this->configuration->getDependencyFactory(); |
169
|
46 |
|
$this->migrationRepository = $this->dependencyFactory->getMigrationRepository(); |
170
|
46 |
|
} |
171
|
|
|
|
172
|
20 |
|
private function hasConfigurationHelper() : bool |
173
|
|
|
{ |
174
|
20 |
|
$helperSet = $this->getHelperSet(); |
175
|
|
|
|
176
|
20 |
|
if (! $helperSet instanceof HelperSet) { |
|
|
|
|
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
|
180
|
19 |
|
if (! $helperSet->has('configuration')) { |
181
|
18 |
|
return false; |
182
|
|
|
} |
183
|
|
|
|
184
|
1 |
|
return $helperSet->get('configuration') instanceof ConfigurationHelperInterface; |
185
|
|
|
} |
186
|
|
|
|
187
|
19 |
|
private function getConnection(InputInterface $input) : Connection |
188
|
|
|
{ |
189
|
19 |
|
if ($this->connection === null) { |
190
|
14 |
|
$this->connection = (new ConnectionLoader($this->configuration)) |
191
|
14 |
|
->getConnection($input, $this->getHelperSet()); |
192
|
|
|
} |
193
|
|
|
|
194
|
18 |
|
return $this->connection; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|