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