/
DoctrineCommand.php
139 lines (113 loc) · 4.51 KB
/
DoctrineCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
namespace Doctrine\Migrations\Tools\Console\Command;
use Doctrine\Migrations\Configuration\Connection\ConfigurationFile;
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\ConsoleLogger;
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied;
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function is_string;
/**
* The DoctrineCommand class provides base functionality for the other migrations commands to extend from.
*/
abstract class DoctrineCommand extends Command
{
private ?DependencyFactory $dependencyFactory = null;
/** @var StyleInterface */
protected $io;
public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null)
{
$this->dependencyFactory = $dependencyFactory;
parent::__construct($name);
}
protected function configure(): void
{
$this->addOption(
'configuration',
null,
InputOption::VALUE_REQUIRED,
'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>',
);
$this->addOption(
'em',
null,
InputOption::VALUE_REQUIRED,
'The name of the entity manager to use.',
);
$this->addOption(
'conn',
null,
InputOption::VALUE_REQUIRED,
'The name of the connection to use.',
);
if ($this->dependencyFactory !== null) {
return;
}
$this->addOption(
'db-configuration',
null,
InputOption::VALUE_REQUIRED,
'The path to a database connection configuration file.',
'migrations-db.php',
);
}
protected function initialize(InputInterface $input, OutputInterface $output): void
{
$this->io = new SymfonyStyle($input, $output);
$configurationParameter = $input->getOption('configuration');
if ($this->dependencyFactory === null) {
$configurationLoader = new ConfigurationFileWithFallback(
is_string($configurationParameter)
? $configurationParameter
: null,
);
$connectionLoader = new ConfigurationFile($input->getOption('db-configuration'));
$this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader);
} elseif (is_string($configurationParameter)) {
$configurationLoader = new ConfigurationFileWithFallback($configurationParameter);
$this->dependencyFactory->setConfigurationLoader($configurationLoader);
}
$this->setNamedEmOrConnection($input);
if ($this->dependencyFactory->isFrozen()) {
return;
}
$logger = new ConsoleLogger($output);
$this->dependencyFactory->setService(LoggerInterface::class, $logger);
$this->dependencyFactory->freeze();
}
protected function getDependencyFactory(): DependencyFactory
{
if ($this->dependencyFactory === null) {
throw DependenciesNotSatisfied::new();
}
return $this->dependencyFactory;
}
protected function canExecute(string $question, InputInterface $input): bool
{
return ! $input->isInteractive() || $this->io->confirm($question);
}
private function setNamedEmOrConnection(InputInterface $input): void
{
$emName = $input->getOption('em');
$connName = $input->getOption('conn');
if ($emName !== null && $connName !== null) {
throw new InvalidOptionUsage('You can specify only one of the --em and --conn options.');
}
if ($this->dependencyFactory->hasEntityManager() && $emName !== null) {
$this->dependencyFactory->getConfiguration()->setEntityManagerName($emName);
return;
}
if ($connName !== null) {
$this->dependencyFactory->getConfiguration()->setConnectionName($connName);
return;
}
}
}