|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Tools\Console\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\ConfigurationLoader; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Exception\UnknownLoader; |
|
10
|
|
|
use Doctrine\Migrations\Tools\Console\Exception\FileTypeNotSupported; |
|
11
|
|
|
use Symfony\Component\Console\Helper\Helper; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use const PATHINFO_EXTENSION; |
|
14
|
|
|
use function file_exists; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
use function pathinfo; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The MigrationsConfigurationHelper class is responsible for getting the Configuration instance from one of the supported methods |
|
20
|
|
|
* for defining the configuration for your migrations. |
|
21
|
|
|
*/ |
|
22
|
|
|
final class MigrationsConfigurationHelper extends Helper implements ConfigurationHelper |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ConfigurationLoader */ |
|
25
|
|
|
private $loader; |
|
26
|
|
|
|
|
27
|
4 |
|
public function __construct(?ConfigurationLoader $loader = null) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->loader = $loader ?: new ConfigurationLoader(); |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
public function getConfiguration(InputInterface $input) : Configuration |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* If a configuration option is passed to the command line, use that configuration |
|
36
|
|
|
* instead of any other one. |
|
37
|
|
|
*/ |
|
38
|
4 |
|
$configurationFile = $input->getOption('configuration'); |
|
39
|
|
|
|
|
40
|
4 |
|
if ($configurationFile !== null && is_string($configurationFile)) { |
|
41
|
2 |
|
return $this->loadConfig($configurationFile); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* If no any other config has been found, look for default config file in the path. |
|
46
|
|
|
*/ |
|
47
|
|
|
$defaultConfig = [ |
|
48
|
2 |
|
'migrations.xml', |
|
49
|
|
|
'migrations.yml', |
|
50
|
|
|
'migrations.yaml', |
|
51
|
|
|
'migrations.json', |
|
52
|
|
|
'migrations.php', |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
2 |
|
foreach ($defaultConfig as $config) { |
|
56
|
2 |
|
if ($this->configExists($config)) { |
|
57
|
1 |
|
return $this->loadConfig($config); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
return $this->loader->getLoader('array')->load([]); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
private function configExists(string $config) : bool |
|
65
|
|
|
{ |
|
66
|
2 |
|
return file_exists($config); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @throws FileTypeNotSupported |
|
71
|
|
|
*/ |
|
72
|
3 |
|
private function loadConfig(string $configFile) : Configuration |
|
73
|
|
|
{ |
|
74
|
3 |
|
$extension = pathinfo($configFile, PATHINFO_EXTENSION); |
|
75
|
|
|
|
|
76
|
|
|
try { |
|
77
|
3 |
|
return $this->loader->getLoader($extension)->load($configFile); |
|
78
|
1 |
|
} catch (UnknownLoader $e) { |
|
79
|
1 |
|
throw FileTypeNotSupported::new(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getName() : string |
|
87
|
|
|
{ |
|
88
|
|
|
return 'configuration'; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|