DependencyFactory::getMigrationGenerator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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