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