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