1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Command; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration\ConfigurationFileWithFallback; |
8
|
|
|
use Doctrine\Migrations\Configuration\Connection\ConfigurationFile; |
9
|
|
|
use Doctrine\Migrations\DependencyFactory; |
10
|
|
|
use Doctrine\Migrations\Tools\Console\ConsoleLogger; |
11
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Symfony\Component\Console\Command\Command; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
18
|
|
|
use function escapeshellarg; |
19
|
|
|
use function is_string; |
20
|
|
|
use function proc_open; |
21
|
|
|
use function str_repeat; |
22
|
|
|
use function strlen; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from. |
26
|
|
|
*/ |
27
|
|
|
abstract class DoctrineCommand extends Command |
28
|
|
|
{ |
29
|
|
|
/** @var DependencyFactory|null */ |
30
|
|
|
private $dependencyFactory; |
31
|
|
|
|
32
|
48 |
|
public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null) |
33
|
|
|
{ |
34
|
48 |
|
parent::__construct($name); |
35
|
48 |
|
$this->dependencyFactory = $dependencyFactory; |
36
|
48 |
|
} |
37
|
|
|
|
38
|
48 |
|
protected function configure() : void |
39
|
|
|
{ |
40
|
48 |
|
if ($this->dependencyFactory !== null) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
48 |
|
$this->addOption( |
44
|
48 |
|
'configuration', |
45
|
48 |
|
null, |
46
|
48 |
|
InputOption::VALUE_REQUIRED, |
47
|
48 |
|
'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>' |
48
|
|
|
); |
49
|
|
|
|
50
|
48 |
|
$this->addOption( |
51
|
48 |
|
'db-configuration', |
52
|
48 |
|
null, |
53
|
48 |
|
InputOption::VALUE_REQUIRED, |
54
|
48 |
|
'The path to a database connection configuration file.', |
55
|
48 |
|
'migrations-db.php' |
56
|
|
|
); |
57
|
48 |
|
} |
58
|
|
|
|
59
|
12 |
|
protected function outputHeader( |
60
|
|
|
OutputInterface $output |
61
|
|
|
) : void { |
62
|
12 |
|
$name = $this->getDependencyFactory()->getConfiguration()->getName(); |
63
|
12 |
|
$name = $name ?? 'Doctrine Database Migrations'; |
64
|
12 |
|
$name = str_repeat(' ', 20) . $name . str_repeat(' ', 20); |
65
|
12 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
66
|
12 |
|
$output->writeln('<question>' . $name . '</question>'); |
67
|
12 |
|
$output->writeln('<question>' . str_repeat(' ', strlen($name)) . '</question>'); |
68
|
12 |
|
$output->writeln(''); |
69
|
12 |
|
} |
70
|
|
|
|
71
|
40 |
|
protected function initialize(InputInterface $input, OutputInterface $output) : void |
72
|
|
|
{ |
73
|
40 |
|
if ($this->dependencyFactory === null) { |
74
|
|
|
$configurationLoader = new ConfigurationFileWithFallback( |
75
|
|
|
is_string($input->getOption('configuration')) |
76
|
|
|
? $input->getOption('configuration') |
77
|
|
|
: null |
78
|
|
|
); |
79
|
|
|
$connectionLoader = new ConfigurationFile((string) $input->getOption('db-configuration')); |
80
|
|
|
$this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader); |
81
|
|
|
} |
82
|
|
|
|
83
|
40 |
|
if ($this->dependencyFactory->isFrozen()) { |
84
|
|
|
return; |
85
|
|
|
} |
86
|
|
|
|
87
|
40 |
|
$logger = new ConsoleLogger($output); |
88
|
40 |
|
$this->dependencyFactory->setService(LoggerInterface::class, $logger); |
89
|
40 |
|
$this->dependencyFactory->freeze(); |
90
|
40 |
|
} |
91
|
|
|
|
92
|
42 |
|
protected function getDependencyFactory() : DependencyFactory |
93
|
|
|
{ |
94
|
42 |
|
if ($this->dependencyFactory === null) { |
95
|
|
|
throw DependenciesNotSatisfied::new(); |
96
|
|
|
} |
97
|
|
|
|
98
|
42 |
|
return $this->dependencyFactory; |
99
|
|
|
} |
100
|
|
|
|
101
|
4 |
|
protected function askConfirmation( |
102
|
|
|
string $question, |
103
|
|
|
InputInterface $input, |
104
|
|
|
OutputInterface $output |
105
|
|
|
) : bool { |
106
|
4 |
|
return $this->getHelper('question')->ask( |
107
|
4 |
|
$input, |
108
|
4 |
|
$output, |
109
|
4 |
|
new ConfirmationQuestion($question) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
3 |
|
protected function canExecute( |
114
|
|
|
string $question, |
115
|
|
|
InputInterface $input, |
116
|
|
|
OutputInterface $output |
117
|
|
|
) : bool { |
118
|
3 |
|
return ! $input->isInteractive() || $this->askConfirmation($question, $input, $output); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
protected function procOpen(string $editorCommand, string $path) : void |
122
|
|
|
{ |
123
|
1 |
|
proc_open($editorCommand . ' ' . escapeshellarg($path), [], $pipes); |
124
|
1 |
|
} |
125
|
|
|
} |
126
|
|
|
|