1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the AntiMattr MongoDB Migrations Library, a library by Matthew Fitzgerald. |
7
|
|
|
* |
8
|
|
|
* (c) 2014 Matthew Fitzgerald |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace AntiMattr\MongoDB\Migrations\Configuration; |
15
|
|
|
|
16
|
|
|
use AntiMattr\MongoDB\Migrations\OutputWriter; |
17
|
|
|
use MongoDB\Client; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Douglas Reith <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ConfigurationBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var \MongoDB\Client |
27
|
|
|
*/ |
28
|
|
|
private $connection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var OutputWriter |
32
|
|
|
*/ |
33
|
|
|
private $outputWriter; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $configParams; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $configFile; |
44
|
|
|
|
45
|
3 |
|
private function __construct() |
46
|
|
|
{ |
47
|
3 |
|
$this->configParams = [ |
48
|
|
|
'name' => null, |
49
|
|
|
'database' => null, |
50
|
|
|
'collection_name' => null, |
51
|
|
|
'migrations_namespace' => null, |
52
|
|
|
'migrations_directory' => null, |
53
|
|
|
'migrations_script_directory' => null, |
54
|
|
|
'migrations' => [], |
55
|
|
|
]; |
56
|
3 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return ConfigurationBuilder |
60
|
|
|
*/ |
61
|
3 |
|
public static function create(): ConfigurationBuilder |
62
|
|
|
{ |
63
|
3 |
|
return new static(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Client $connection |
68
|
|
|
* |
69
|
|
|
* @return ConfigurationBuilder |
70
|
|
|
*/ |
71
|
3 |
|
public function setConnection(Client $connection): ConfigurationBuilder |
72
|
|
|
{ |
73
|
3 |
|
$this->connection = $connection; |
74
|
|
|
|
75
|
3 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param OutputWriter $outputWriter |
80
|
|
|
* |
81
|
|
|
* @return ConfigurationBuilder |
82
|
|
|
*/ |
83
|
3 |
|
public function setOutputWriter(OutputWriter $outputWriter): ConfigurationBuilder |
84
|
|
|
{ |
85
|
3 |
|
$this->outputWriter = $outputWriter; |
86
|
|
|
|
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string|null $configFile |
92
|
|
|
* |
93
|
|
|
* @return ConfigurationBuilder |
94
|
|
|
*/ |
95
|
3 |
|
public function setOnDiskConfiguration(?string $configFile = null): ConfigurationBuilder |
96
|
|
|
{ |
97
|
3 |
|
$this->configFile = $configFile; |
98
|
|
|
|
99
|
3 |
|
if ($this->configFile) { |
100
|
2 |
|
if (file_exists($path = getcwd() . '/' . $this->configFile)) { |
101
|
|
|
$this->configFile = $path; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
if (!file_exists($this->configFile)) { |
105
|
|
|
throw new \InvalidArgumentException('The specified config file is not a valid file.'); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
$info = pathinfo($this->configFile); |
109
|
|
|
|
110
|
2 |
|
$fileExt = strtolower($info['extension']); |
111
|
|
|
|
112
|
2 |
|
switch ($fileExt) { |
113
|
2 |
|
case 'xml': |
114
|
1 |
|
$diskConfig = $this->loadXmlFile($this->configFile); |
115
|
1 |
|
break; |
116
|
|
|
|
117
|
1 |
|
case 'yml': |
118
|
|
|
case 'yaml': |
119
|
1 |
|
$diskConfig = Yaml::parse(file_get_contents($this->configFile)); |
120
|
1 |
|
break; |
121
|
|
|
|
122
|
|
|
default: |
123
|
|
|
throw new \InvalidArgumentException( |
124
|
|
|
sprintf( |
125
|
|
|
'The specified config file should end in .xml or .yml/.yaml. Unrecognized extension [%s]', |
126
|
|
|
$fileExt |
127
|
|
|
) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
$this->configParams = array_merge($this->configParams, $diskConfig); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return Configuration |
139
|
|
|
*/ |
140
|
3 |
|
public function build(): Configuration |
141
|
|
|
{ |
142
|
3 |
|
$config = new Configuration($this->connection, $this->outputWriter); |
143
|
|
|
|
144
|
3 |
|
$config->setName($this->configParams['name']) |
145
|
3 |
|
->setFile($this->configFile) |
146
|
3 |
|
->setMigrationsDatabaseName($this->configParams['database']) |
147
|
3 |
|
->setMigrationsCollectionName($this->configParams['collection_name']) |
148
|
3 |
|
->setMigrationsNamespace($this->configParams['migrations_namespace']) |
149
|
|
|
; |
150
|
|
|
|
151
|
3 |
|
if (!empty($this->configParams['migrations_directory'])) { |
152
|
2 |
|
$migrationsDirectory = $this->getDirectoryRelativeToFile( |
153
|
2 |
|
$this->configFile, |
154
|
2 |
|
$this->configParams['migrations_directory'] |
155
|
|
|
); |
156
|
|
|
|
157
|
2 |
|
$config->setMigrationsDirectory($migrationsDirectory) |
158
|
2 |
|
->registerMigrationsFromDirectory($migrationsDirectory); |
159
|
|
|
} |
160
|
|
|
|
161
|
3 |
|
if (!empty($this->configParams['migrations_script_directory'])) { |
162
|
2 |
|
$scriptsDirectory = $this->getDirectoryRelativeToFile( |
163
|
2 |
|
$this->configFile, |
164
|
2 |
|
$this->configParams['migrations_script_directory'] |
165
|
|
|
); |
166
|
|
|
|
167
|
2 |
|
$config->setMigrationsScriptDirectory($scriptsDirectory); |
168
|
|
|
} |
169
|
|
|
|
170
|
3 |
|
foreach ($this->configParams['migrations'] as $migration) { |
171
|
|
|
$config->registerMigration( |
172
|
|
|
$migration['version'], |
173
|
|
|
$migration['class'] |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
3 |
|
return $config; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $configFile |
182
|
|
|
* |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
1 |
|
private function loadXmlFile(string $configFile): array |
186
|
|
|
{ |
187
|
1 |
|
$xml = simplexml_load_file($configFile); |
188
|
1 |
|
$configArr = []; |
189
|
|
|
|
190
|
1 |
|
if (isset($xml->name)) { |
191
|
1 |
|
$configArr['name'] = (string) $xml->name; |
192
|
|
|
} |
193
|
|
|
|
194
|
1 |
|
if (isset($xml->database['name'])) { |
195
|
1 |
|
$configArr['database'] = (string) $xml->database['name']; |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
if (isset($xml->collection['name'])) { |
199
|
1 |
|
$configArr['collection_name'] = (string) $xml->collection['name']; |
200
|
|
|
} |
201
|
|
|
|
202
|
1 |
|
if (isset($xml->{'migrations-namespace'})) { |
203
|
1 |
|
$configArr['migrations_namespace'] = (string) $xml->{'migrations-namespace'}; |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
if (isset($xml->{'migrations-directory'})) { |
207
|
1 |
|
$configArr['migrations_directory'] = (string) $xml->{'migrations-directory'}; |
208
|
|
|
} |
209
|
|
|
|
210
|
1 |
|
if (isset($xml->{'migrations-script-directory'})) { |
211
|
1 |
|
$configArr['migrations_script_directory'] = (string) $xml->{'migrations-script-directory'}; |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
if (isset($xml->migrations->migration)) { |
215
|
|
|
foreach ($xml->migrations->migration as $migration) { |
216
|
|
|
$configArr['migrations'][] = [ |
217
|
|
|
'version' => $migration['version'], |
218
|
|
|
'class' => $migration['class'], |
219
|
|
|
]; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
1 |
|
return $configArr; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Get the path to the directory relative to the config file. |
228
|
|
|
* |
229
|
|
|
* @param string $configFile |
230
|
|
|
* @param string|null $directory |
231
|
|
|
* |
232
|
|
|
* @return string|null |
233
|
|
|
*/ |
234
|
2 |
|
protected function getDirectoryRelativeToFile(string $configFile, ?string $directory = null): ?string |
235
|
|
|
{ |
236
|
2 |
|
if (!$directory) { |
237
|
|
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
2 |
|
$path = realpath(dirname($configFile) . '/' . $directory); |
241
|
|
|
|
242
|
2 |
|
if (false !== $path) { |
243
|
|
|
return $path; |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
return $directory; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|