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\Exception\MissingDependency; |
10
|
|
|
use Doctrine\Migrations\Finder\GlobFinder; |
11
|
|
|
use Doctrine\Migrations\Finder\MigrationFinder; |
12
|
|
|
use Doctrine\Migrations\Finder\RecursiveRegexFinder; |
13
|
|
|
use Doctrine\Migrations\Generator\ClassNameGenerator; |
14
|
|
|
use Doctrine\Migrations\Generator\ConcatenationFileBuilder; |
15
|
|
|
use Doctrine\Migrations\Generator\DiffGenerator; |
16
|
|
|
use Doctrine\Migrations\Generator\Generator; |
17
|
|
|
use Doctrine\Migrations\Generator\SqlGenerator; |
18
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
19
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorageConfiguration; |
20
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorage; |
21
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
22
|
|
|
use Doctrine\Migrations\Provider\DBALSchemaDiffProvider; |
23
|
|
|
use Doctrine\Migrations\Provider\LazySchemaDiffProvider; |
24
|
|
|
use Doctrine\Migrations\Provider\OrmSchemaProvider; |
25
|
|
|
use Doctrine\Migrations\Provider\SchemaDiffProvider; |
26
|
|
|
use Doctrine\Migrations\Provider\SchemaProvider; |
27
|
|
|
use Doctrine\Migrations\Tools\Console\ConsoleInputMigratorConfigurationFactory; |
28
|
|
|
use Doctrine\Migrations\Tools\Console\Helper\MigrationStatusInfosHelper; |
29
|
|
|
use Doctrine\Migrations\Tools\Console\MigratorConfigurationFactory; |
30
|
|
|
use Doctrine\Migrations\Version\AliasResolver; |
31
|
|
|
use Doctrine\Migrations\Version\DbalExecutor; |
32
|
|
|
use Doctrine\Migrations\Version\DefaultAliasResolver; |
33
|
|
|
use Doctrine\Migrations\Version\MigrationFactory; |
34
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
35
|
|
|
use Psr\Log\LoggerInterface; |
36
|
|
|
use Psr\Log\NullLogger; |
37
|
|
|
use Symfony\Component\Stopwatch\Stopwatch as SymfonyStopwatch; |
38
|
|
|
use function preg_quote; |
39
|
|
|
use function sprintf; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The DependencyFactory is responsible for wiring up and managing internal class dependencies. |
43
|
|
|
* |
44
|
|
|
* @internal |
45
|
|
|
*/ |
46
|
|
|
class DependencyFactory |
47
|
|
|
{ |
48
|
|
|
/** @var Configuration */ |
49
|
|
|
private $configuration; |
50
|
|
|
|
51
|
|
|
/** @var object[] */ |
52
|
|
|
private $dependencies = []; |
53
|
|
|
|
54
|
|
|
/** @var LoggerInterface */ |
55
|
|
|
private $logger; |
56
|
|
|
|
57
|
|
|
/** @var Connection */ |
58
|
|
|
private $connection; |
59
|
|
|
|
60
|
|
|
/** @var callable */ |
61
|
|
|
private $sorter; |
62
|
|
|
|
63
|
|
|
/** @var EntityManagerInterface|null */ |
64
|
|
|
private $em; |
65
|
|
|
|
66
|
37 |
|
public function __construct(Configuration $configuration, Connection $connection, ?EntityManagerInterface $em = null, ?LoggerInterface $logger = null) |
67
|
|
|
{ |
68
|
37 |
|
$this->configuration = $configuration; |
69
|
37 |
|
$this->logger = $logger ?: new NullLogger(); |
70
|
37 |
|
$this->connection = $connection; |
71
|
37 |
|
$this->em = $em; |
72
|
37 |
|
} |
73
|
|
|
|
74
|
37 |
|
public function getConfiguration() : Configuration |
75
|
|
|
{ |
76
|
37 |
|
return $this->configuration; |
77
|
|
|
} |
78
|
|
|
|
79
|
34 |
|
private function getConnection() : Connection |
80
|
|
|
{ |
81
|
34 |
|
return $this->connection; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getEventDispatcher() : EventDispatcher |
85
|
|
|
{ |
86
|
|
|
return $this->getDependency(EventDispatcher::class, function () : EventDispatcher { |
87
|
|
|
return new EventDispatcher( |
88
|
|
|
$this->getConnection(), |
89
|
|
|
$this->getConnection()->getEventManager() |
90
|
|
|
); |
91
|
|
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getClassNameGenerator() : ClassNameGenerator |
95
|
|
|
{ |
96
|
|
|
return $this->getDependency(ClassNameGenerator::class, static function () : ClassNameGenerator { |
97
|
|
|
return new ClassNameGenerator(); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getSchemaDumper() : SchemaDumper |
102
|
|
|
{ |
103
|
|
|
return $this->getDependency(SchemaDumper::class, function () : SchemaDumper { |
104
|
|
|
$excludedTables = []; |
105
|
|
|
|
106
|
|
|
$metadataConfig = $this->configuration->getMetadataStorageConfiguration(); |
107
|
|
|
if ($metadataConfig instanceof TableMetadataStorageConfiguration) { |
108
|
|
|
$excludedTables[] = sprintf('/^%s$/', preg_quote($metadataConfig->getTableName(), '/')); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return new SchemaDumper( |
112
|
|
|
$this->getConnection()->getDatabasePlatform(), |
113
|
|
|
$this->getConnection()->getSchemaManager(), |
114
|
|
|
$this->getMigrationGenerator(), |
115
|
|
|
$this->getMigrationSqlGenerator(), |
116
|
|
|
$excludedTables |
117
|
|
|
); |
118
|
|
|
}); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
private function getSchemaProvider() : SchemaProvider |
122
|
|
|
{ |
123
|
|
|
return $this->getDependency(SchemaProvider::class, function () : SchemaProvider { |
124
|
|
|
if ($this->em === null) { |
125
|
|
|
throw new MissingDependency('The doctrine entity manager should be provided in order to be able to instantiate SchemaProvider'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new OrmSchemaProvider($this->em); |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getDiffGenerator() : DiffGenerator |
133
|
|
|
{ |
134
|
|
|
return $this->getDependency(DiffGenerator::class, function () : DiffGenerator { |
135
|
|
|
return new DiffGenerator( |
136
|
|
|
$this->getConnection()->getConfiguration(), |
137
|
|
|
$this->getConnection()->getSchemaManager(), |
138
|
|
|
$this->getSchemaProvider(), |
139
|
|
|
$this->getConnection()->getDatabasePlatform(), |
140
|
|
|
$this->getMigrationGenerator(), |
141
|
|
|
$this->getMigrationSqlGenerator() |
142
|
|
|
); |
143
|
|
|
}); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getSchemaDiffProvider() : SchemaDiffProvider |
147
|
|
|
{ |
148
|
|
|
return $this->getDependency(SchemaDiffProvider::class, function () : LazySchemaDiffProvider { |
149
|
|
|
return LazySchemaDiffProvider::fromDefaultProxyFactoryConfiguration( |
150
|
|
|
new DBALSchemaDiffProvider( |
151
|
|
|
$this->connection->getSchemaManager(), |
152
|
|
|
$this->connection->getDatabasePlatform() |
153
|
|
|
) |
154
|
|
|
); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getFileBuilder() : ConcatenationFileBuilder |
159
|
|
|
{ |
160
|
|
|
return $this->getDependency(ConcatenationFileBuilder::class, static function () : ConcatenationFileBuilder { |
161
|
|
|
return new ConcatenationFileBuilder(); |
162
|
|
|
}); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getParameterFormatter() : ParameterFormatter |
166
|
|
|
{ |
167
|
|
|
return $this->getDependency(InlineParameterFormatter::class, function () : InlineParameterFormatter { |
168
|
|
|
return new InlineParameterFormatter($this->connection); |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
37 |
|
public function getMigrationsFinder() : MigrationFinder |
173
|
|
|
{ |
174
|
|
|
return $this->getDependency(GlobFinder::class, function () : MigrationFinder { |
175
|
37 |
|
$configs = $this->getConfiguration(); |
176
|
37 |
|
$needsRecursiveFinder = $configs->areMigrationsOrganizedByYear() || $configs->areMigrationsOrganizedByYearAndMonth(); |
177
|
|
|
|
178
|
37 |
|
return $needsRecursiveFinder ? new RecursiveRegexFinder() : new GlobFinder(); |
179
|
37 |
|
}); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function setSorter(callable $sorter) : void |
183
|
|
|
{ |
184
|
|
|
$this->sorter = $sorter; |
185
|
|
|
} |
186
|
|
|
|
187
|
34 |
|
public function getMigrationRepository() : MigrationRepository |
188
|
|
|
{ |
189
|
|
|
return $this->getDependency(MigrationRepository::class, function () : MigrationRepository { |
190
|
34 |
|
return new MigrationRepository( |
191
|
34 |
|
$this->getConfiguration()->getMigrationClasses(), |
192
|
34 |
|
$this->getConfiguration()->getMigrationDirectories(), |
193
|
34 |
|
$this->getMigrationsFinder(), |
194
|
34 |
|
new MigrationFactory($this->getConnection(), $this->getLogger()), |
195
|
34 |
|
$this->sorter |
196
|
|
|
); |
197
|
34 |
|
}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function setMetadataStorageConfiguration(MetadataStorageConfiguration $metadataStorageConfigration) : void |
201
|
|
|
{ |
202
|
|
|
$this->dependencies[MetadataStorageConfiguration::class] = $metadataStorageConfigration; |
203
|
|
|
} |
204
|
|
|
|
205
|
34 |
|
private function getMetadataStorageConfiguration() : MetadataStorageConfiguration |
206
|
|
|
{ |
207
|
|
|
return $this->getDependency(MetadataStorageConfiguration::class, static function () : MetadataStorageConfiguration { |
208
|
34 |
|
return new TableMetadataStorageConfiguration(); |
209
|
34 |
|
}); |
210
|
|
|
} |
211
|
|
|
|
212
|
34 |
|
public function getMetadataStorage() : MetadataStorage |
213
|
|
|
{ |
214
|
|
|
return $this->getDependency(TableMetadataStorage::class, function () : MetadataStorage { |
215
|
34 |
|
return new TableMetadataStorage( |
216
|
34 |
|
$this->connection, |
217
|
34 |
|
$this->getMetadataStorageConfiguration() |
218
|
|
|
); |
219
|
34 |
|
}); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
public function getEntityManager() : ?EntityManagerInterface |
223
|
|
|
{ |
224
|
|
|
return $this->em; |
225
|
|
|
} |
226
|
|
|
|
227
|
34 |
|
public function getLogger() : LoggerInterface |
228
|
|
|
{ |
229
|
34 |
|
return $this->logger; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function getVersionExecutor() : DbalExecutor |
233
|
|
|
{ |
234
|
|
|
return $this->getDependency(DbalExecutor::class, function () : DbalExecutor { |
235
|
|
|
return new DbalExecutor( |
236
|
|
|
$this->getMetadataStorage(), |
237
|
|
|
$this->getEventDispatcher(), |
238
|
|
|
$this->connection, |
239
|
|
|
$this->getSchemaDiffProvider(), |
240
|
|
|
$this->getLogger(), |
241
|
|
|
$this->getParameterFormatter(), |
242
|
|
|
$this->getStopwatch() |
243
|
|
|
); |
244
|
|
|
}); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function getQueryWriter() : QueryWriter |
248
|
|
|
{ |
249
|
|
|
return $this->getDependency(QueryWriter::class, function () : QueryWriter { |
250
|
|
|
return new FileQueryWriter( |
251
|
|
|
$this->getFileBuilder(), |
252
|
|
|
$this->logger |
253
|
|
|
); |
254
|
|
|
}); |
255
|
|
|
} |
256
|
|
|
|
257
|
15 |
|
public function getVersionAliasResolver() : AliasResolver |
258
|
|
|
{ |
259
|
|
|
return $this->getDependency(AliasResolver::class, function () : AliasResolver { |
260
|
15 |
|
return new DefaultAliasResolver( |
261
|
15 |
|
$this->getMigrationRepository(), |
262
|
15 |
|
$this->getMetadataStorage(), |
263
|
15 |
|
$this->getMigrationPlanCalculator() |
264
|
|
|
); |
265
|
15 |
|
}); |
266
|
|
|
} |
267
|
|
|
|
268
|
21 |
|
public function getMigrationPlanCalculator() : MigrationPlanCalculator |
269
|
|
|
{ |
270
|
|
|
return $this->getDependency(MigrationPlanCalculator::class, function () : MigrationPlanCalculator { |
271
|
21 |
|
return new MigrationPlanCalculator( |
272
|
21 |
|
$this->getMigrationRepository(), |
273
|
21 |
|
$this->getMetadataStorage() |
274
|
|
|
); |
275
|
21 |
|
}); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public function getMigrationGenerator() : Generator |
279
|
|
|
{ |
280
|
|
|
return $this->getDependency(Generator::class, function () : Generator { |
281
|
|
|
return new Generator($this->getConfiguration()); |
282
|
|
|
}); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function getMigrationSqlGenerator() : SqlGenerator |
286
|
|
|
{ |
287
|
|
|
return $this->getDependency(SqlGenerator::class, function () : SqlGenerator { |
288
|
|
|
return new SqlGenerator( |
289
|
|
|
$this->getConfiguration(), |
290
|
|
|
$this->connection->getDatabasePlatform() |
291
|
|
|
); |
292
|
|
|
}); |
293
|
|
|
} |
294
|
|
|
|
295
|
13 |
|
public function getConsoleInputMigratorConfigurationFactory() : MigratorConfigurationFactory |
296
|
|
|
{ |
297
|
|
|
return $this->getDependency(MigratorConfigurationFactory::class, function () : MigratorConfigurationFactory { |
298
|
13 |
|
return new ConsoleInputMigratorConfigurationFactory( |
299
|
13 |
|
$this->getConfiguration() |
300
|
|
|
); |
301
|
13 |
|
}); |
302
|
|
|
} |
303
|
|
|
|
304
|
2 |
|
public function getMigrationStatusInfosHelper() : MigrationStatusInfosHelper |
305
|
|
|
{ |
306
|
|
|
return $this->getDependency(MigrationStatusInfosHelper::class, function () : MigrationStatusInfosHelper { |
307
|
2 |
|
return new MigrationStatusInfosHelper( |
308
|
2 |
|
$this->getConfiguration(), |
309
|
2 |
|
$this->connection, |
310
|
2 |
|
$this->getVersionAliasResolver() |
311
|
|
|
); |
312
|
2 |
|
}); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function getMigrator() : Migrator |
316
|
|
|
{ |
317
|
|
|
return $this->getDependency(Migrator::class, function () : Migrator { |
318
|
|
|
return new DbalMigrator( |
319
|
|
|
$this->connection, |
320
|
|
|
$this->getEventDispatcher(), |
321
|
|
|
$this->getVersionExecutor(), |
322
|
|
|
$this->logger, |
323
|
|
|
$this->getStopwatch() |
324
|
|
|
); |
325
|
|
|
}); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getStopwatch() : Stopwatch |
329
|
|
|
{ |
330
|
|
|
return $this->getDependency(Stopwatch::class, static function () : Stopwatch { |
331
|
|
|
$symfonyStopwatch = new SymfonyStopwatch(true); |
332
|
|
|
|
333
|
|
|
return new Stopwatch($symfonyStopwatch); |
334
|
|
|
}); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
public function getRollup() : Rollup |
338
|
|
|
{ |
339
|
|
|
return $this->getDependency(Rollup::class, function () : Rollup { |
340
|
|
|
return new Rollup( |
341
|
|
|
$this->getMetadataStorage(), |
342
|
|
|
$this->getMigrationRepository() |
343
|
|
|
); |
344
|
|
|
}); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return mixed |
349
|
|
|
*/ |
350
|
41 |
|
private function getDependency(string $className, callable $callback) |
351
|
|
|
{ |
352
|
41 |
|
if (! isset($this->dependencies[$className])) { |
353
|
41 |
|
$this->dependencies[$className] = $callback(); |
354
|
|
|
} |
355
|
|
|
|
356
|
41 |
|
return $this->dependencies[$className]; |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
|