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