1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Migrations; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\Migrations\Configuration\Configuration; |
9
|
|
|
use Doctrine\Migrations\Configuration\Connection\ConnectionLoader; |
10
|
|
|
use Doctrine\Migrations\Configuration\EntityManager\EntityManagerLoader; |
11
|
|
|
use Doctrine\Migrations\Configuration\Migration\ConfigurationLoader; |
12
|
|
|
use Doctrine\Migrations\Exception\FrozenDependencies; |
13
|
|
|
use Doctrine\Migrations\Exception\MissingDependency; |
14
|
|
|
use Doctrine\Migrations\Finder\GlobFinder; |
15
|
|
|
use Doctrine\Migrations\Finder\MigrationFinder; |
16
|
|
|
use Doctrine\Migrations\Finder\RecursiveRegexFinder; |
17
|
|
|
use Doctrine\Migrations\Generator\ClassNameGenerator; |
18
|
|
|
use Doctrine\Migrations\Generator\ConcatenationFileBuilder; |
19
|
|
|
use Doctrine\Migrations\Generator\DiffGenerator; |
20
|
|
|
use Doctrine\Migrations\Generator\FileBuilder; |
21
|
|
|
use Doctrine\Migrations\Generator\Generator; |
22
|
|
|
use Doctrine\Migrations\Generator\SqlGenerator; |
23
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
24
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage; |
25
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
26
|
|
|
use Doctrine\Migrations\Provider\DBALSchemaDiffProvider; |
27
|
|
|
use Doctrine\Migrations\Provider\LazySchemaDiffProvider; |
28
|
|
|
use Doctrine\Migrations\Provider\OrmSchemaProvider; |
29
|
|
|
use Doctrine\Migrations\Provider\SchemaDiffProvider; |
30
|
|
|
use Doctrine\Migrations\Provider\SchemaProvider; |
31
|
|
|
use Doctrine\Migrations\Tools\Console\ConsoleInputMigratorConfigurationFactory; |
32
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper; |
33
|
|
|
use Doctrine\Migrations\Tools\Console\MigratorConfigurationFactory; |
34
|
|
|
use Doctrine\Migrations\Version\AliasResolver; |
35
|
|
|
use Doctrine\Migrations\Version\AlphabeticalComparator; |
36
|
|
|
use Doctrine\Migrations\Version\Comparator; |
37
|
|
|
use Doctrine\Migrations\Version\CurrentMigrationStatusCalculator; |
38
|
|
|
use Doctrine\Migrations\Version\DbalExecutor; |
39
|
|
|
use Doctrine\Migrations\Version\DbalMigrationFactory; |
40
|
|
|
use Doctrine\Migrations\Version\DefaultAliasResolver; |
41
|
|
|
use Doctrine\Migrations\Version\Executor; |
42
|
|
|
use Doctrine\Migrations\Version\MigrationFactory; |
43
|
|
|
use Doctrine\Migrations\Version\MigrationPlanCalculator; |
44
|
|
|
use Doctrine\Migrations\Version\MigrationStatusCalculator; |
45
|
|
|
use Doctrine\Migrations\Version\SortedMigrationPlanCalculator; |
46
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
47
|
|
|
use Psr\Log\LoggerInterface; |
48
|
|
|
use Psr\Log\NullLogger; |
49
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
50
|
|
|
use function array_key_exists; |
51
|
|
|
use function preg_quote; |
52
|
|
|
use function sprintf; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The DependencyFactory is responsible for wiring up and managing internal class dependencies. |
56
|
|
|
*/ |
57
|
|
|
class DependencyFactory |
58
|
|
|
{ |
59
|
|
|
/** @var Configuration */ |
60
|
|
|
private $configuration; |
61
|
|
|
|
62
|
|
|
/** @var object[]|callable[] */ |
63
|
|
|
private $dependencies = []; |
64
|
|
|
|
65
|
|
|
/** @var Connection */ |
66
|
|
|
private $connection; |
67
|
|
|
|
68
|
|
|
/** @var EntityManagerInterface|null */ |
69
|
|
|
private $em; |
70
|
|
|
|
71
|
|
|
/** @var bool */ |
72
|
|
|
private $frozen = false; |
73
|
|
|
|
74
|
|
|
/** @var ConfigurationLoader */ |
75
|
|
|
private $configurationLoader; |
76
|
|
|
|
77
|
|
|
/** @var ConnectionLoader */ |
78
|
|
|
private $connectionLoader; |
79
|
|
|
|
80
|
|
|
/** @var EntityManagerLoader|null */ |
81
|
|
|
private $emLoader; |
82
|
|
|
|
83
|
44 |
|
public static function fromConnection( |
84
|
|
|
ConfigurationLoader $configurationLoader, |
85
|
|
|
ConnectionLoader $connectionLoader, |
86
|
|
|
?LoggerInterface $logger = null |
87
|
|
|
) : self { |
88
|
44 |
|
$dependencyFactory = new self($logger); |
89
|
44 |
|
$dependencyFactory->configurationLoader = $configurationLoader; |
90
|
44 |
|
$dependencyFactory->connectionLoader = $connectionLoader; |
91
|
|
|
|
92
|
44 |
|
return $dependencyFactory; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public static function fromEntityManager( |
96
|
|
|
ConfigurationLoader $configurationLoader, |
97
|
|
|
EntityManagerLoader $emLoader, |
98
|
|
|
?LoggerInterface $logger = null |
99
|
|
|
) : self { |
100
|
1 |
|
$dependencyFactory = new self($logger); |
101
|
1 |
|
$dependencyFactory->configurationLoader = $configurationLoader; |
102
|
1 |
|
$dependencyFactory->emLoader = $emLoader; |
103
|
|
|
|
104
|
1 |
|
return $dependencyFactory; |
105
|
|
|
} |
106
|
|
|
|
107
|
45 |
|
private function __construct(?LoggerInterface $logger) |
108
|
|
|
{ |
109
|
45 |
|
if ($logger === null) { |
110
|
30 |
|
return; |
111
|
|
|
} |
112
|
|
|
|
113
|
15 |
|
$this->setService(LoggerInterface::class, $logger); |
114
|
15 |
|
} |
115
|
|
|
|
116
|
36 |
|
public function isFrozen() : bool |
117
|
|
|
{ |
118
|
36 |
|
return $this->frozen; |
119
|
|
|
} |
120
|
|
|
|
121
|
36 |
|
public function freeze() : void |
122
|
|
|
{ |
123
|
36 |
|
$this->frozen = true; |
124
|
36 |
|
$this->getConfiguration()->freeze(); |
125
|
36 |
|
} |
126
|
|
|
|
127
|
39 |
|
private function assertNotFrozen() : void |
128
|
|
|
{ |
129
|
39 |
|
if ($this->frozen) { |
130
|
1 |
|
throw FrozenDependencies::new(); |
131
|
|
|
} |
132
|
38 |
|
} |
133
|
|
|
|
134
|
38 |
|
public function hasEntityManager() : bool |
135
|
|
|
{ |
136
|
38 |
|
return $this->emLoader !== null; |
137
|
|
|
} |
138
|
|
|
|
139
|
40 |
|
public function getConfiguration() : Configuration |
140
|
|
|
{ |
141
|
40 |
|
if ($this->configuration === null) { |
142
|
40 |
|
$this->configuration = $this->configurationLoader->getConfiguration(); |
143
|
|
|
} |
144
|
|
|
|
145
|
40 |
|
return $this->configuration; |
146
|
|
|
} |
147
|
|
|
|
148
|
38 |
|
public function getConnection() : Connection |
149
|
|
|
{ |
150
|
38 |
|
if ($this->connection === null) { |
151
|
38 |
|
$this->connection = $this->hasEntityManager() |
152
|
1 |
|
? $this->getEntityManager()->getConnection() |
153
|
37 |
|
: $this->connectionLoader->getConnection(); |
154
|
|
|
} |
155
|
|
|
|
156
|
38 |
|
return $this->connection; |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
public function getEntityManager() : EntityManagerInterface |
160
|
|
|
{ |
161
|
2 |
|
if ($this->em === null) { |
162
|
2 |
|
if ($this->emLoader === null) { |
163
|
1 |
|
throw MissingDependency::noEntityManager(); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
$this->em = $this->emLoader->getEntityManager(); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
return $this->em; |
170
|
|
|
} |
171
|
|
|
|
172
|
36 |
|
public function getVersionComparator() : Comparator |
173
|
|
|
{ |
174
|
|
|
return $this->getDependency(Comparator::class, static function () : AlphabeticalComparator { |
175
|
36 |
|
return new AlphabeticalComparator(); |
176
|
36 |
|
}); |
177
|
|
|
} |
178
|
|
|
|
179
|
38 |
|
public function getLogger() : LoggerInterface |
180
|
|
|
{ |
181
|
|
|
return $this->getDependency(LoggerInterface::class, static function () : LoggerInterface { |
182
|
23 |
|
return new NullLogger(); |
183
|
38 |
|
}); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getEventDispatcher() : EventDispatcher |
187
|
|
|
{ |
188
|
|
|
return $this->getDependency(EventDispatcher::class, function () : EventDispatcher { |
189
|
|
|
return new EventDispatcher( |
190
|
|
|
$this->getConnection(), |
191
|
|
|
$this->getConnection()->getEventManager() |
192
|
|
|
); |
193
|
|
|
}); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function getClassNameGenerator() : ClassNameGenerator |
197
|
|
|
{ |
198
|
|
|
return $this->getDependency(ClassNameGenerator::class, static function () : ClassNameGenerator { |
199
|
|
|
return new ClassNameGenerator(); |
200
|
|
|
}); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getSchemaDumper() : SchemaDumper |
204
|
|
|
{ |
205
|
|
|
return $this->getDependency(SchemaDumper::class, function () : SchemaDumper { |
206
|
|
|
$excludedTables = []; |
207
|
|
|
|
208
|
|
|
$metadataConfig = $this->getConfiguration()->getMetadataStorageConfiguration(); |
209
|
|
|
if ($metadataConfig instanceof TableMetadataStorageConfiguration) { |
210
|
|
|
$excludedTables[] = sprintf('/^%s$/', preg_quote($metadataConfig->getTableName(), '/')); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return new SchemaDumper( |
214
|
|
|
$this->getConnection()->getDatabasePlatform(), |
215
|
|
|
$this->getConnection()->getSchemaManager(), |
216
|
|
|
$this->getMigrationGenerator(), |
217
|
|
|
$this->getMigrationSqlGenerator(), |
218
|
|
|
$excludedTables |
219
|
|
|
); |
220
|
|
|
}); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
private function getSchemaProvider() : SchemaProvider |
224
|
|
|
{ |
225
|
|
|
return $this->getDependency(SchemaProvider::class, function () : SchemaProvider { |
226
|
|
|
return new OrmSchemaProvider($this->getEntityManager()); |
227
|
|
|
}); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function getDiffGenerator() : DiffGenerator |
231
|
|
|
{ |
232
|
|
|
return $this->getDependency(DiffGenerator::class, function () : DiffGenerator { |
233
|
|
|
return new DiffGenerator( |
234
|
|
|
$this->getConnection()->getConfiguration(), |
235
|
|
|
$this->getConnection()->getSchemaManager(), |
236
|
|
|
$this->getSchemaProvider(), |
237
|
|
|
$this->getConnection()->getDatabasePlatform(), |
238
|
|
|
$this->getMigrationGenerator(), |
239
|
|
|
$this->getMigrationSqlGenerator() |
240
|
|
|
); |
241
|
|
|
}); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function getSchemaDiffProvider() : SchemaDiffProvider |
245
|
|
|
{ |
246
|
|
|
return $this->getDependency(SchemaDiffProvider::class, function () : LazySchemaDiffProvider { |
247
|
|
|
return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration( |
248
|
|
|
new DBALSchemaDiffProvider( |
249
|
|
|
$this->getConnection()->getSchemaManager(), |
250
|
|
|
$this->getConnection()->getDatabasePlatform() |
251
|
|
|
) |
252
|
|
|
); |
253
|
|
|
}); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
private function getFileBuilder() : FileBuilder |
257
|
|
|
{ |
258
|
|
|
return $this->getDependency(FileBuilder::class, static function () : FileBuilder { |
259
|
|
|
return new ConcatenationFileBuilder(); |
260
|
|
|
}); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
private function getParameterFormatter() : ParameterFormatter |
264
|
|
|
{ |
265
|
|
|
return $this->getDependency(ParameterFormatter::class, function () : ParameterFormatter { |
266
|
|
|
return new InlineParameterFormatter($this->getConnection()); |
267
|
|
|
}); |
268
|
|
|
} |
269
|
|
|
|
270
|
39 |
|
public function getMigrationsFinder() : MigrationFinder |
271
|
|
|
{ |
272
|
|
|
return $this->getDependency(MigrationFinder::class, function () : MigrationFinder { |
273
|
39 |
|
$configs = $this->getConfiguration(); |
274
|
39 |
|
$needsRecursiveFinder = $configs->areMigrationsOrganizedByYear() || $configs->areMigrationsOrganizedByYearAndMonth(); |
275
|
|
|
|
276
|
39 |
|
return $needsRecursiveFinder ? new RecursiveRegexFinder() : new GlobFinder(); |
277
|
39 |
|
}); |
278
|
|
|
} |
279
|
|
|
|
280
|
36 |
|
public function getMigrationRepository() : MigrationRepository |
281
|
|
|
{ |
282
|
|
|
return $this->getDependency(MigrationRepository::class, function () : MigrationRepository { |
283
|
36 |
|
return new MigrationRepository( |
284
|
36 |
|
$this->getConfiguration()->getMigrationClasses(), |
285
|
36 |
|
$this->getConfiguration()->getMigrationDirectories(), |
286
|
36 |
|
$this->getMigrationsFinder(), |
287
|
36 |
|
$this->getMigrationFactory(), |
288
|
36 |
|
$this->getVersionComparator() |
289
|
|
|
); |
290
|
36 |
|
}); |
291
|
|
|
} |
292
|
|
|
|
293
|
36 |
|
public function getMigrationFactory() : MigrationFactory |
294
|
|
|
{ |
295
|
|
|
return $this->getDependency(MigrationFactory::class, function () : MigrationFactory { |
296
|
36 |
|
return new DbalMigrationFactory($this->getConnection(), $this->getLogger()); |
297
|
36 |
|
}); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param object|callable $service |
302
|
|
|
*/ |
303
|
39 |
|
public function setService(string $id, $service) : void |
304
|
|
|
{ |
305
|
39 |
|
$this->assertNotFrozen(); |
306
|
38 |
|
$this->dependencies[$id] = $service; |
307
|
38 |
|
} |
308
|
|
|
|
309
|
36 |
|
public function getMetadataStorage() : MetadataStorage |
310
|
|
|
{ |
311
|
|
|
return $this->getDependency(MetadataStorage::class, function () : MetadataStorage { |
312
|
36 |
|
return new TableMetadataStorage( |
313
|
36 |
|
$this->getConnection(), |
314
|
36 |
|
$this->getConfiguration()->getMetadataStorageConfiguration(), |
315
|
36 |
|
$this->getMigrationRepository() |
316
|
|
|
); |
317
|
36 |
|
}); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
private function getVersionExecutor() : Executor |
321
|
|
|
{ |
322
|
|
|
return $this->getDependency(Executor::class, function () : Executor { |
323
|
|
|
return new DbalExecutor( |
324
|
|
|
$this->getMetadataStorage(), |
325
|
|
|
$this->getEventDispatcher(), |
326
|
|
|
$this->getConnection(), |
327
|
|
|
$this->getSchemaDiffProvider(), |
328
|
|
|
$this->getLogger(), |
329
|
|
|
$this->getParameterFormatter(), |
330
|
|
|
$this->getStopwatch() |
331
|
|
|
); |
332
|
|
|
}); |
333
|
|
|
} |
334
|
|
|
|
335
|
2 |
|
public function getQueryWriter() : QueryWriter |
336
|
|
|
{ |
337
|
|
|
return $this->getDependency(QueryWriter::class, function () : QueryWriter { |
338
|
|
|
return new FileQueryWriter( |
339
|
|
|
$this->getFileBuilder(), |
340
|
|
|
$this->getLogger() |
341
|
|
|
); |
342
|
2 |
|
}); |
343
|
|
|
} |
344
|
|
|
|
345
|
16 |
|
public function getVersionAliasResolver() : AliasResolver |
346
|
|
|
{ |
347
|
|
|
return $this->getDependency(AliasResolver::class, function () : AliasResolver { |
348
|
16 |
|
return new DefaultAliasResolver( |
349
|
16 |
|
$this->getMigrationRepository(), |
350
|
16 |
|
$this->getMetadataStorage(), |
351
|
16 |
|
$this->getMigrationStatusCalculator() |
352
|
|
|
); |
353
|
16 |
|
}); |
354
|
|
|
} |
355
|
|
|
|
356
|
22 |
|
public function getMigrationStatusCalculator() : MigrationStatusCalculator |
357
|
|
|
{ |
358
|
|
|
return $this->getDependency(MigrationStatusCalculator::class, function () : MigrationStatusCalculator { |
359
|
22 |
|
return new CurrentMigrationStatusCalculator( |
360
|
22 |
|
$this->getMigrationRepository(), |
361
|
22 |
|
$this->getMetadataStorage() |
362
|
|
|
); |
363
|
22 |
|
}); |
364
|
|
|
} |
365
|
|
|
|
366
|
10 |
|
public function getMigrationPlanCalculator() : MigrationPlanCalculator |
367
|
|
|
{ |
368
|
|
|
return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator { |
369
|
10 |
|
return new SortedMigrationPlanCalculator( |
370
|
10 |
|
$this->getMigrationRepository(), |
371
|
10 |
|
$this->getMetadataStorage() |
372
|
|
|
); |
373
|
10 |
|
}); |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
public function getMigrationGenerator() : Generator |
377
|
|
|
{ |
378
|
|
|
return $this->getDependency(Generator::class, function () : Generator { |
379
|
|
|
return new Generator($this->getConfiguration()); |
380
|
|
|
}); |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
public function getMigrationSqlGenerator() : SqlGenerator |
384
|
|
|
{ |
385
|
|
|
return $this->getDependency(SqlGenerator::class, function () : SqlGenerator { |
386
|
|
|
return new SqlGenerator( |
387
|
|
|
$this->getConfiguration(), |
388
|
|
|
$this->getConnection()->getDatabasePlatform() |
389
|
|
|
); |
390
|
|
|
}); |
391
|
|
|
} |
392
|
|
|
|
393
|
14 |
|
public function getConsoleInputMigratorConfigurationFactory() : MigratorConfigurationFactory |
394
|
|
|
{ |
395
|
|
|
return $this->getDependency(MigratorConfigurationFactory::class, function () : MigratorConfigurationFactory { |
396
|
14 |
|
return new ConsoleInputMigratorConfigurationFactory( |
397
|
14 |
|
$this->getConfiguration() |
398
|
|
|
); |
399
|
14 |
|
}); |
400
|
|
|
} |
401
|
|
|
|
402
|
3 |
|
public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper |
403
|
|
|
{ |
404
|
|
|
return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper { |
405
|
3 |
|
return new MigrationStatusInfosHelper( |
406
|
3 |
|
$this->getConfiguration(), |
407
|
3 |
|
$this->getConnection(), |
408
|
3 |
|
$this->getVersionAliasResolver(), |
409
|
3 |
|
$this->getMigrationRepository(), |
410
|
3 |
|
$this->getMetadataStorage() |
411
|
|
|
); |
412
|
3 |
|
}); |
413
|
|
|
} |
414
|
|
|
|
415
|
6 |
|
public function getMigrator() : Migrator |
416
|
|
|
{ |
417
|
|
|
return $this->getDependency(Migrator::class, function () : Migrator { |
418
|
|
|
return new DbalMigrator( |
419
|
|
|
$this->getConnection(), |
420
|
|
|
$this->getEventDispatcher(), |
421
|
|
|
$this->getVersionExecutor(), |
422
|
|
|
$this->getLogger(), |
423
|
|
|
$this->getStopwatch() |
424
|
|
|
); |
425
|
6 |
|
}); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
public function getStopwatch() : Stopwatch |
429
|
|
|
{ |
430
|
|
|
return $this->getDependency(Stopwatch::class, static function () : Stopwatch { |
431
|
|
|
return new Stopwatch(true); |
432
|
|
|
}); |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
public function getRollup() : Rollup |
436
|
|
|
{ |
437
|
|
|
return $this->getDependency(Rollup::class, function () : Rollup { |
438
|
|
|
return new Rollup( |
439
|
|
|
$this->getMetadataStorage(), |
440
|
|
|
$this->getMigrationRepository() |
441
|
|
|
); |
442
|
|
|
}); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* @return mixed |
447
|
|
|
*/ |
448
|
46 |
|
private function getDependency(string $id, callable $callback) |
449
|
|
|
{ |
450
|
46 |
|
if (! array_key_exists($id, $this->dependencies)) { |
451
|
44 |
|
$this->dependencies[$id] = $callback(); |
452
|
|
|
} |
453
|
|
|
|
454
|
46 |
|
return $this->dependencies[$id]; |
455
|
|
|
} |
456
|
|
|
} |
457
|
|
|
|