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\Connection\ConnectionRegistryConnection; |
9
|
|
|
use Doctrine\Migrations\Configuration\EntityManager\ManagerRegistryEntityManager; |
10
|
|
|
use Doctrine\Migrations\Configuration\Migration\ConfigurationFileWithFallback; |
11
|
|
|
use Doctrine\Migrations\DependencyFactory; |
12
|
|
|
use Doctrine\Migrations\Tools\Console\ConsoleLogger; |
13
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\DependenciesNotSatisfied; |
14
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\InvalidOptionUsage; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
21
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
22
|
|
|
use function is_string; |
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
|
71 |
|
/** @var StyleInterface */ |
33
|
|
|
protected $io; |
34
|
71 |
|
|
35
|
71 |
|
public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null) |
36
|
71 |
|
{ |
37
|
|
|
parent::__construct($name); |
38
|
71 |
|
$this->dependencyFactory = $dependencyFactory; |
39
|
|
|
} |
40
|
71 |
|
|
41
|
71 |
|
protected function configure() : void |
42
|
71 |
|
{ |
43
|
71 |
|
$this->addOption( |
44
|
71 |
|
'configuration', |
45
|
|
|
null, |
46
|
|
|
InputOption::VALUE_REQUIRED, |
47
|
71 |
|
'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>' |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$this->addOption( |
51
|
71 |
|
'em', |
52
|
71 |
|
null, |
53
|
71 |
|
InputOption::VALUE_REQUIRED, |
54
|
71 |
|
'The name of the connection to use (available only with ManagerRegistryEntityManager loader).' |
55
|
71 |
|
); |
56
|
71 |
|
|
57
|
|
|
$this->addOption( |
58
|
71 |
|
'conn', |
59
|
|
|
null, |
60
|
71 |
|
InputOption::VALUE_REQUIRED, |
61
|
|
|
'The name of the entity manager to use (available only with ConnectionRegistryConnection loader).' |
62
|
71 |
|
); |
63
|
|
|
|
64
|
71 |
|
if ($this->dependencyFactory !== null) { |
65
|
71 |
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->addOption( |
69
|
|
|
'db-configuration', |
70
|
|
|
null, |
71
|
|
|
InputOption::VALUE_REQUIRED, |
72
|
|
|
'The path to a database connection configuration file.', |
73
|
71 |
|
'migrations-db.php' |
74
|
1 |
|
); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) : void |
78
|
71 |
|
{ |
79
|
26 |
|
$this->io = new SymfonyStyle($input, $output); |
80
|
|
|
|
81
|
|
|
$configurationParameter = $input->getOption('configuration'); |
82
|
45 |
|
if ($this->dependencyFactory === null) { |
83
|
45 |
|
$configurationLoader = new ConfigurationFileWithFallback( |
84
|
45 |
|
is_string($configurationParameter) |
85
|
45 |
|
? $configurationParameter |
86
|
|
|
: null |
87
|
65 |
|
); |
88
|
|
|
$connectionLoader = new ConfigurationFile((string) $input->getOption('db-configuration')); |
89
|
65 |
|
$this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader); |
|
|
|
|
90
|
|
|
} elseif (is_string($configurationParameter)) { |
91
|
|
|
$configurationLoader = new ConfigurationFileWithFallback($configurationParameter); |
92
|
|
|
$this->dependencyFactory->setConfigurationLoader($configurationLoader); |
93
|
65 |
|
} |
94
|
|
|
|
95
|
|
|
if ($input->getOption('em')!== null && $input->getOption('conn')!==null) { |
96
|
32 |
|
throw new InvalidOptionUsage('You can specify only one of the --em and --conn options.'); |
97
|
|
|
} |
98
|
32 |
|
|
99
|
|
|
$connectionLoader = $this->dependencyFactory->getConnectionLoader(); |
100
|
|
|
if ($connectionLoader instanceof ConnectionRegistryConnection && $input->getOption('conn')!==null) { |
101
|
|
|
$connectionName = $input->getOption('conn'); |
102
|
|
|
$connectionLoader->setConnectionName((string) $connectionName); |
103
|
|
|
} elseif ($input->getOption('conn')!==null) { |
104
|
|
|
throw new InvalidOptionUsage('You can use the --conn option only if the connection loader is of type ConnectionRegistryConnection.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$connectionLoader = $this->dependencyFactory->getEntityManagerLoader(); |
108
|
|
|
if ($connectionLoader instanceof ManagerRegistryEntityManager && $input->getOption('em')!==null) { |
109
|
|
|
$emName = $input->getOption('em'); |
110
|
|
|
$connectionLoader->setManagerName((string) $emName); |
111
|
|
|
} elseif ($input->getOption('em')!==null) { |
112
|
|
|
throw new InvalidOptionUsage('You can use the --em option only if the entity loader is of type ManagerRegistryEntityManager.'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->dependencyFactory->isFrozen()) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$logger = new ConsoleLogger($output); |
120
|
|
|
$this->dependencyFactory->setService(LoggerInterface::class, $logger); |
|
|
|
|
121
|
|
|
$this->dependencyFactory->freeze(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function getDependencyFactory() : DependencyFactory |
125
|
|
|
{ |
126
|
|
|
if ($this->dependencyFactory === null) { |
127
|
|
|
throw DependenciesNotSatisfied::new(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $this->dependencyFactory; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function canExecute(string $question, InputInterface $input) : bool |
134
|
|
|
{ |
135
|
|
|
return ! $input->isInteractive() || $this->io->confirm($question); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..