|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\ArrayConfiguration; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
10
|
|
|
use Doctrine\Migrations\Configuration\JsonConfiguration; |
|
11
|
|
|
use Doctrine\Migrations\Configuration\XmlConfiguration; |
|
12
|
|
|
use Doctrine\Migrations\Configuration\YamlConfiguration; |
|
13
|
|
|
use Doctrine\Migrations\OutputWriter; |
|
14
|
|
|
use InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Console\Helper\Helper; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
17
|
|
|
use function file_exists; |
|
18
|
|
|
use function pathinfo; |
|
19
|
|
|
use function sprintf; |
|
20
|
|
|
|
|
21
|
|
|
class ConfigurationHelper extends Helper implements ConfigurationHelperInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var Connection */ |
|
24
|
|
|
private $connection; |
|
25
|
|
|
|
|
26
|
|
|
/** @var null|Configuration */ |
|
27
|
|
|
private $configuration; |
|
28
|
|
|
|
|
29
|
50 |
|
public function __construct( |
|
30
|
|
|
Connection $connection, |
|
31
|
|
|
?Configuration $configuration = null |
|
32
|
|
|
) { |
|
33
|
50 |
|
$this->connection = $connection; |
|
34
|
50 |
|
$this->configuration = $configuration; |
|
35
|
50 |
|
} |
|
36
|
|
|
|
|
37
|
49 |
|
public function getMigrationConfig(InputInterface $input, OutputWriter $outputWriter) : Configuration |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* If a configuration option is passed to the command line, use that configuration |
|
41
|
|
|
* instead of any other one. |
|
42
|
|
|
*/ |
|
43
|
49 |
|
if ($input->getOption('configuration')) { |
|
44
|
14 |
|
$outputWriter->write( |
|
45
|
14 |
|
sprintf( |
|
46
|
14 |
|
'Loading configuration from command option: %s', |
|
47
|
14 |
|
$input->getOption('configuration') |
|
48
|
|
|
) |
|
49
|
|
|
); |
|
50
|
|
|
|
|
51
|
14 |
|
return $this->loadConfig($input->getOption('configuration'), $outputWriter); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* If a configuration has already been set using DI or a Setter use it. |
|
56
|
|
|
*/ |
|
57
|
35 |
|
if ($this->configuration) { |
|
58
|
27 |
|
$outputWriter->write( |
|
59
|
27 |
|
'Loading configuration from the integration code of your framework (setter).' |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
27 |
|
$this->configuration->setOutputWriter($outputWriter); |
|
63
|
|
|
|
|
64
|
27 |
|
return $this->configuration; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* If no any other config has been found, look for default config file in the path. |
|
69
|
|
|
*/ |
|
70
|
|
|
$defaultConfig = [ |
|
71
|
8 |
|
'migrations.xml', |
|
72
|
|
|
'migrations.yml', |
|
73
|
|
|
'migrations.yaml', |
|
74
|
|
|
'migrations.json', |
|
75
|
|
|
'migrations.php', |
|
76
|
|
|
]; |
|
77
|
|
|
|
|
78
|
8 |
|
foreach ($defaultConfig as $config) { |
|
79
|
8 |
|
if ($this->configExists($config)) { |
|
80
|
5 |
|
$outputWriter->write(sprintf('Loading configuration from file: %s', $config)); |
|
81
|
|
|
|
|
82
|
8 |
|
return $this->loadConfig($config, $outputWriter); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
return new Configuration($this->connection, $outputWriter); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
8 |
|
private function configExists(string $config) : bool |
|
91
|
|
|
{ |
|
92
|
8 |
|
return file_exists($config); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** @throws InvalidArgumentException */ |
|
96
|
19 |
|
private function loadConfig(string $config, OutputWriter $outputWriter) : Configuration |
|
97
|
|
|
{ |
|
98
|
|
|
$map = [ |
|
99
|
19 |
|
'xml' => XmlConfiguration::class, |
|
100
|
|
|
'yaml' => YamlConfiguration::class, |
|
101
|
|
|
'yml' => YamlConfiguration::class, |
|
102
|
|
|
'php' => ArrayConfiguration::class, |
|
103
|
|
|
'json' => JsonConfiguration::class, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
19 |
|
$info = pathinfo($config); |
|
107
|
|
|
|
|
108
|
|
|
// check we can support this file type |
|
109
|
19 |
|
if (empty($map[$info['extension']])) { |
|
110
|
1 |
|
throw new InvalidArgumentException('Given config file type is not supported'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
18 |
|
$class = $map[$info['extension']]; |
|
114
|
18 |
|
$configuration = new $class($this->connection, $outputWriter); |
|
115
|
18 |
|
$configuration->load($config); |
|
116
|
|
|
|
|
117
|
18 |
|
return $configuration; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getName() : string |
|
124
|
|
|
{ |
|
125
|
1 |
|
return 'configuration'; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|