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\Console\Style\SymfonyStyle; |
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
|
|
|
/** @var \Symfony\Component\Console\Style\StyleInterface */ |
|
|
|
|
30
|
|
|
protected $io; |
31
|
|
|
|
32
|
55 |
|
public function __construct(?DependencyFactory $dependencyFactory = null, ?string $name = null) |
33
|
|
|
{ |
34
|
55 |
|
parent::__construct($name); |
35
|
55 |
|
$this->dependencyFactory = $dependencyFactory; |
36
|
55 |
|
} |
37
|
|
|
|
38
|
55 |
|
protected function configure() : void |
39
|
|
|
{ |
40
|
55 |
|
if ($this->dependencyFactory !== null) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
55 |
|
$this->addOption( |
45
|
55 |
|
'configuration', |
46
|
55 |
|
null, |
47
|
55 |
|
InputOption::VALUE_REQUIRED, |
48
|
55 |
|
'The path to a migrations configuration file. <comment>[default: any of migrations.{php,xml,json,yml,yaml}]</comment>' |
49
|
|
|
); |
50
|
|
|
|
51
|
55 |
|
$this->addOption( |
52
|
55 |
|
'db-configuration', |
53
|
55 |
|
null, |
54
|
55 |
|
InputOption::VALUE_REQUIRED, |
55
|
55 |
|
'The path to a database connection configuration file.', |
56
|
55 |
|
'migrations-db.php' |
57
|
|
|
); |
58
|
55 |
|
} |
59
|
|
|
|
60
|
55 |
|
protected function initialize(InputInterface $input, OutputInterface $output) : void |
61
|
|
|
{ |
62
|
55 |
|
$this->io = new SymfonyStyle($input, $output); |
63
|
|
|
|
64
|
55 |
|
if ($this->dependencyFactory === null) { |
65
|
|
|
$configurationLoader = new ConfigurationFileWithFallback( |
66
|
|
|
is_string($input->getOption('configuration')) |
67
|
|
|
? $input->getOption('configuration') |
68
|
|
|
: null |
69
|
|
|
); |
70
|
|
|
$connectionLoader = new ConfigurationFile((string) $input->getOption('db-configuration')); |
71
|
|
|
$this->dependencyFactory = DependencyFactory::fromConnection($configurationLoader, $connectionLoader); |
72
|
|
|
} |
73
|
|
|
|
74
|
55 |
|
if ($this->dependencyFactory->isFrozen()) { |
75
|
1 |
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
55 |
|
$logger = new ConsoleLogger($output); |
79
|
55 |
|
$this->dependencyFactory->setService(LoggerInterface::class, $logger); |
80
|
55 |
|
$this->dependencyFactory->freeze(); |
81
|
55 |
|
} |
82
|
|
|
|
83
|
49 |
|
protected function getDependencyFactory() : DependencyFactory |
84
|
|
|
{ |
85
|
49 |
|
if ($this->dependencyFactory === null) { |
86
|
|
|
throw DependenciesNotSatisfied::new(); |
87
|
|
|
} |
88
|
|
|
|
89
|
49 |
|
return $this->dependencyFactory; |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
protected function canExecute(string $question, InputInterface $input) : bool |
93
|
|
|
{ |
94
|
14 |
|
return ! $input->isInteractive() || $this->io->confirm($question); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|