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