1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations\Configuration; |
6
|
|
|
|
7
|
|
|
use Doctrine\Migrations\Configuration\Exception\FrozenConfiguration; |
8
|
|
|
use Doctrine\Migrations\Configuration\Exception\MissingNamespaceConfiguration; |
9
|
|
|
use Doctrine\Migrations\Configuration\Exception\UnknownConfigurationValue; |
10
|
|
|
use Doctrine\Migrations\Exception\MigrationException; |
11
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorageConfiguration; |
12
|
|
|
use function count; |
13
|
|
|
use function strcasecmp; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The Configuration class is responsible for defining migration configuration information. |
17
|
|
|
*/ |
18
|
|
|
final class Configuration |
19
|
|
|
{ |
20
|
|
|
public const VERSIONS_ORGANIZATION_BY_YEAR = 'year'; |
21
|
|
|
public const VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH = 'year_and_month'; |
22
|
|
|
|
23
|
|
|
/** @var array<string, string> */ |
24
|
|
|
private $migrationsDirectories = []; |
25
|
|
|
|
26
|
|
|
/** @var string[] */ |
27
|
|
|
private $migrationClasses = []; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
private $migrationsAreOrganizedByYear = false; |
31
|
|
|
|
32
|
|
|
/** @var bool */ |
33
|
|
|
private $migrationsAreOrganizedByYearAndMonth = false; |
34
|
|
|
|
35
|
|
|
/** @var string|null */ |
36
|
|
|
private $customTemplate; |
37
|
|
|
|
38
|
|
|
/** @var bool */ |
39
|
|
|
private $isDryRun = false; |
40
|
|
|
|
41
|
|
|
/** @var bool */ |
42
|
|
|
private $allOrNothing = false; |
43
|
|
|
|
44
|
|
|
/** @var bool */ |
45
|
|
|
private $checkDbPlatform = true; |
46
|
|
|
|
47
|
|
|
/** @var MetadataStorageConfiguration */ |
48
|
|
|
private $metadataStorageConfiguration; |
49
|
|
|
|
50
|
|
|
/** @var bool */ |
51
|
|
|
private $frozen = false; |
52
|
|
|
|
53
|
38 |
|
public function freeze() : void |
54
|
|
|
{ |
55
|
38 |
|
if (! $this->frozen) { |
56
|
38 |
|
$this->validate(); |
57
|
|
|
} |
58
|
|
|
|
59
|
37 |
|
$this->frozen = true; |
60
|
37 |
|
} |
61
|
|
|
|
62
|
115 |
|
private function assertNotFrozen() : void |
63
|
|
|
{ |
64
|
115 |
|
if ($this->frozen) { |
65
|
1 |
|
throw FrozenConfiguration::new(); |
66
|
|
|
} |
67
|
115 |
|
} |
68
|
|
|
|
69
|
63 |
|
public function setMetadataStorageConfiguration(MetadataStorageConfiguration $metadataStorageConfiguration) : void |
70
|
|
|
{ |
71
|
63 |
|
$this->assertNotFrozen(); |
72
|
63 |
|
$this->metadataStorageConfiguration = $metadataStorageConfiguration; |
73
|
63 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
44 |
|
public function getMigrationClasses() : array |
79
|
|
|
{ |
80
|
44 |
|
return $this->migrationClasses; |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
public function addMigrationClass(string $className) : void |
84
|
|
|
{ |
85
|
10 |
|
$this->assertNotFrozen(); |
86
|
10 |
|
$this->migrationClasses[] = $className; |
87
|
10 |
|
} |
88
|
|
|
|
89
|
31 |
|
public function getMetadataStorageConfiguration() : ?MetadataStorageConfiguration |
90
|
|
|
{ |
91
|
31 |
|
return $this->metadataStorageConfiguration; |
92
|
|
|
} |
93
|
|
|
|
94
|
91 |
|
public function addMigrationsDirectory(string $namespace, string $path) : void |
95
|
|
|
{ |
96
|
91 |
|
$this->assertNotFrozen(); |
97
|
91 |
|
$this->migrationsDirectories[$namespace] = $path; |
98
|
91 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return array<string,string> |
102
|
|
|
*/ |
103
|
56 |
|
public function getMigrationDirectories() : array |
104
|
|
|
{ |
105
|
56 |
|
return $this->migrationsDirectories; |
106
|
|
|
} |
107
|
|
|
|
108
|
10 |
|
public function setCustomTemplate(?string $customTemplate) : void |
109
|
|
|
{ |
110
|
10 |
|
$this->assertNotFrozen(); |
111
|
10 |
|
$this->customTemplate = $customTemplate; |
112
|
10 |
|
} |
113
|
|
|
|
114
|
12 |
|
public function getCustomTemplate() : ?string |
115
|
|
|
{ |
116
|
12 |
|
return $this->customTemplate; |
117
|
|
|
} |
118
|
|
|
|
119
|
58 |
|
public function areMigrationsOrganizedByYear() : bool |
120
|
|
|
{ |
121
|
58 |
|
return $this->migrationsAreOrganizedByYear; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @throws MigrationException |
126
|
|
|
*/ |
127
|
7 |
|
public function setMigrationsAreOrganizedByYear( |
128
|
|
|
bool $migrationsAreOrganizedByYear = true |
129
|
|
|
) : void { |
130
|
7 |
|
$this->assertNotFrozen(); |
131
|
7 |
|
$this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYear; |
132
|
7 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @throws MigrationException |
136
|
|
|
*/ |
137
|
7 |
|
public function setMigrationsAreOrganizedByYearAndMonth( |
138
|
|
|
bool $migrationsAreOrganizedByYearAndMonth = true |
139
|
|
|
) : void { |
140
|
7 |
|
$this->assertNotFrozen(); |
141
|
7 |
|
$this->migrationsAreOrganizedByYear = $migrationsAreOrganizedByYearAndMonth; |
142
|
7 |
|
$this->migrationsAreOrganizedByYearAndMonth = $migrationsAreOrganizedByYearAndMonth; |
143
|
7 |
|
} |
144
|
|
|
|
145
|
56 |
|
public function areMigrationsOrganizedByYearAndMonth() : bool |
146
|
|
|
{ |
147
|
56 |
|
return $this->migrationsAreOrganizedByYearAndMonth; |
148
|
|
|
} |
149
|
|
|
|
150
|
38 |
|
private function validate() : void |
151
|
|
|
{ |
152
|
38 |
|
if (count($this->migrationsDirectories) === 0) { |
153
|
1 |
|
throw MissingNamespaceConfiguration::new(); |
154
|
|
|
} |
155
|
37 |
|
} |
156
|
|
|
|
157
|
1 |
|
public function setIsDryRun(bool $isDryRun) : void |
158
|
|
|
{ |
159
|
1 |
|
$this->assertNotFrozen(); |
160
|
1 |
|
$this->isDryRun = $isDryRun; |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
public function isDryRun() : bool |
164
|
|
|
{ |
165
|
1 |
|
return $this->isDryRun; |
166
|
|
|
} |
167
|
|
|
|
168
|
11 |
|
public function setAllOrNothing(bool $allOrNothing) : void |
169
|
|
|
{ |
170
|
11 |
|
$this->assertNotFrozen(); |
171
|
10 |
|
$this->allOrNothing = $allOrNothing; |
172
|
10 |
|
} |
173
|
|
|
|
174
|
6 |
|
public function isAllOrNothing() : bool |
175
|
|
|
{ |
176
|
6 |
|
return $this->allOrNothing; |
177
|
|
|
} |
178
|
|
|
|
179
|
11 |
|
public function setCheckDatabasePlatform(bool $checkDbPlatform) : void |
180
|
|
|
{ |
181
|
11 |
|
$this->checkDbPlatform = $checkDbPlatform; |
182
|
11 |
|
} |
183
|
|
|
|
184
|
3 |
|
public function isDatabasePlatformChecked() : bool |
185
|
|
|
{ |
186
|
3 |
|
return $this->checkDbPlatform; |
187
|
|
|
} |
188
|
|
|
|
189
|
14 |
|
public function setMigrationOrganization(string $migrationOrganization) : void |
190
|
|
|
{ |
191
|
14 |
|
$this->assertNotFrozen(); |
192
|
14 |
|
if (strcasecmp($migrationOrganization, self::VERSIONS_ORGANIZATION_BY_YEAR) === 0) { |
193
|
5 |
|
$this->setMigrationsAreOrganizedByYear(); |
194
|
9 |
|
} elseif (strcasecmp($migrationOrganization, self::VERSIONS_ORGANIZATION_BY_YEAR_AND_MONTH) === 0) { |
195
|
5 |
|
$this->setMigrationsAreOrganizedByYearAndMonth(); |
196
|
|
|
} else { |
197
|
4 |
|
throw UnknownConfigurationValue::new('organize_migrations', $migrationOrganization); |
198
|
|
|
} |
199
|
10 |
|
} |
200
|
|
|
} |
201
|
|
|
|