|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Configuration; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Exception\FileAlreadyLoaded; |
|
8
|
|
|
use Doctrine\Migrations\Configuration\Exception\InvalidConfigurationKey; |
|
9
|
|
|
use Doctrine\Migrations\Configuration\Exception\UnknownConfigurationValue; |
|
10
|
|
|
use Doctrine\Migrations\Exception\MigrationException; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use function dirname; |
|
13
|
|
|
use function file_exists; |
|
14
|
|
|
use function getcwd; |
|
15
|
|
|
use function in_array; |
|
16
|
|
|
use function realpath; |
|
17
|
|
|
use function strcasecmp; |
|
18
|
|
|
|
|
19
|
|
|
abstract class AbstractFileConfiguration extends Configuration |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array */ |
|
22
|
|
|
private const ALLOWED_CONFIGURATION_KEYS = [ |
|
23
|
|
|
'migrations_namespace', |
|
24
|
|
|
'table_name', |
|
25
|
|
|
'column_name', |
|
26
|
|
|
'column_length', |
|
27
|
|
|
'executed_at_column_name', |
|
28
|
|
|
'organize_migrations', |
|
29
|
|
|
'name', |
|
30
|
|
|
'migrations_directory', |
|
31
|
|
|
'migrations', |
|
32
|
|
|
'custom_template', |
|
33
|
|
|
'all_or_nothing', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
private const CONFIGURATION_METHOD_MAP = [ |
|
37
|
|
|
'migrations_namespace' => 'setMigrationsNamespace', |
|
38
|
|
|
'table_name' => 'setMigrationsTableName', |
|
39
|
|
|
'column_name' => 'setMigrationsColumnName', |
|
40
|
|
|
'column_length' => 'setMigrationsColumnLength', |
|
41
|
|
|
'executed_at_column_name' => 'setMigrationsExecutedAtColumnName', |
|
42
|
|
|
'organize_migrations' => 'setMigrationOrganization', |
|
43
|
|
|
'name' => 'setName', |
|
44
|
|
|
'migrations_directory' => 'loadMigrationsFromDirectory', |
|
45
|
|
|
'migrations' => 'loadMigrations', |
|
46
|
|
|
'custom_template' => 'setCustomTemplate', |
|
47
|
|
|
'all_or_nothing' => 'setAllOrNothing', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** @var string */ |
|
51
|
|
|
private $file; |
|
52
|
|
|
|
|
53
|
|
|
/** @var bool */ |
|
54
|
|
|
private $loaded = false; |
|
55
|
|
|
|
|
56
|
|
|
/** @throws MigrationException */ |
|
57
|
101 |
|
public function load(string $file) : void |
|
58
|
|
|
{ |
|
59
|
101 |
|
if ($this->loaded) { |
|
60
|
4 |
|
throw FileAlreadyLoaded::new(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
101 |
|
$path = getcwd() . '/' . $file; |
|
64
|
|
|
|
|
65
|
101 |
|
if (file_exists($path)) { |
|
66
|
1 |
|
$file = $path; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
101 |
|
$this->file = $file; |
|
70
|
|
|
|
|
71
|
101 |
|
if (! file_exists($file)) { |
|
72
|
6 |
|
throw new InvalidArgumentException('Given config file does not exist'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
95 |
|
$this->doLoad($file); |
|
76
|
83 |
|
$this->loaded = true; |
|
77
|
83 |
|
} |
|
78
|
|
|
|
|
79
|
11 |
|
public function getFile() : string |
|
80
|
|
|
{ |
|
81
|
11 |
|
return $this->file; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param mixed[] $config |
|
86
|
|
|
*/ |
|
87
|
94 |
|
protected function setConfiguration(array $config) : void |
|
88
|
|
|
{ |
|
89
|
94 |
|
foreach ($config as $configurationKey => $configurationValue) { |
|
90
|
94 |
|
if (! in_array($configurationKey, self::ALLOWED_CONFIGURATION_KEYS, true)) { |
|
91
|
94 |
|
throw InvalidConfigurationKey::new($configurationKey); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
90 |
|
foreach (self::CONFIGURATION_METHOD_MAP as $key => $method) { |
|
96
|
90 |
|
if (! isset($config[$key])) { |
|
97
|
89 |
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
90 |
|
$this->$method($config[$key]); |
|
101
|
|
|
} |
|
102
|
83 |
|
} |
|
103
|
|
|
|
|
104
|
64 |
|
protected function getDirectoryRelativeToFile(string $file, string $input) : string |
|
105
|
|
|
{ |
|
106
|
64 |
|
$path = realpath(dirname($file) . '/' . $input); |
|
107
|
|
|
|
|
108
|
64 |
|
return ($path !== false) ? $path : $input; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Abstract method that each file configuration driver must implement to |
|
113
|
|
|
* load the given configuration file whether it be xml, yaml, etc. or something |
|
114
|
|
|
* else. |
|
115
|
|
|
*/ |
|
116
|
|
|
abstract protected function doLoad(string $file) : void; |
|
117
|
|
|
|
|
118
|
65 |
|
private function loadMigrationsFromDirectory(string $migrationsDirectory) : void |
|
119
|
|
|
{ |
|
120
|
65 |
|
$this->setMigrationsDirectory($migrationsDirectory); |
|
121
|
65 |
|
$this->registerMigrationsFromDirectory($migrationsDirectory); |
|
122
|
65 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** @param string[][] $migrations */ |
|
125
|
51 |
|
private function loadMigrations(array $migrations) : void |
|
126
|
|
|
{ |
|
127
|
51 |
|
foreach ($migrations as $migration) { |
|
128
|
14 |
|
$this->registerMigration( |
|
129
|
14 |
|
$migration['version'], |
|
130
|
14 |
|
$migration['class'] |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
51 |
|
} |
|
134
|
|
|
|
|
135
|
21 |
|
private function setMigrationOrganization(string $migrationOrganization) : void |
|
136
|
|
|
{ |
|
137
|
21 |
|
if (strcasecmp($migrationOrganization, static::VERSIONS_ORGANIZATION_BY_YEAR) === 0) { |
|
138
|
8 |
|
$this->setMigrationsAreOrganizedByYear(); |
|
139
|
13 |
|
} elseif (strcasecmp($migrationOrganization, static::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) === 0) { |
|
140
|
10 |
|
$this->setMigrationsAreOrganizedByYearAndMonth(); |
|
141
|
|
|
} else { |
|
142
|
3 |
|
throw UnknownConfigurationValue::new('organize_migrations', $migrationOrganization); |
|
143
|
|
|
} |
|
144
|
14 |
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|