|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
|
8
|
|
|
use Doctrine\Migrations\DependencyFactory; |
|
9
|
|
|
use Doctrine\Migrations\Metadata\Storage\MetadataStorage; |
|
10
|
|
|
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use Symfony\Component\Config\FileLocator; |
|
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
19
|
|
|
use function sprintf; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* DoctrineMigrationsExtension. |
|
23
|
|
|
*/ |
|
24
|
|
|
class DoctrineMigrationsExtension extends Extension |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Responds to the migrations configuration parameter. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string[][] $configs |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function load(array $configs, ContainerBuilder $container) : void |
|
32
|
|
|
{ |
|
33
|
6 |
|
$configuration = new Configuration(); |
|
34
|
|
|
|
|
35
|
6 |
|
$config = $this->processConfiguration($configuration, $configs); |
|
36
|
|
|
|
|
37
|
6 |
|
$locator = new FileLocator(__DIR__ . '/../Resources/config/'); |
|
38
|
6 |
|
$loader = new XmlFileLoader($container, $locator); |
|
39
|
|
|
|
|
40
|
6 |
|
$loader->load('services.xml'); |
|
41
|
|
|
|
|
42
|
6 |
|
$configurationDefinition = $container->getDefinition('doctrine.migrations.configuration'); |
|
43
|
|
|
|
|
44
|
6 |
|
$configurationDefinition->addMethodCall('setName', [$config['name']]); |
|
45
|
|
|
|
|
46
|
6 |
|
foreach ($config['migrations_paths'] as $ns => $path) { |
|
47
|
6 |
|
$configurationDefinition->addMethodCall('addMigrationsDirectory', [$ns, $path]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
6 |
|
foreach ($config['migrations'] as $migrationClass) { |
|
51
|
1 |
|
$configurationDefinition->addMethodCall('addMigrationClass', [$migrationClass]); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
6 |
|
if ($config['organize_migrations'] !== false) { |
|
55
|
1 |
|
$configurationDefinition->addMethodCall('setMigrationOrganization', [$config['organize_migrations']]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
if ($config['custom_template'] !== null) { |
|
59
|
|
|
$configurationDefinition->addMethodCall('setCustomTemplate', [$config['custom_template']]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
6 |
|
$configurationDefinition->addMethodCall('setAllOrNothing', [$config['all_or_nothing']]); |
|
63
|
6 |
|
$configurationDefinition->addMethodCall('setCheckDatabasePlatform', [$config['check_database_platform']]); |
|
64
|
|
|
|
|
65
|
6 |
|
$diDefinition = $container->getDefinition('doctrine.migrations.di'); |
|
66
|
|
|
|
|
67
|
6 |
|
if ($config['sorter'] !== null) { |
|
68
|
1 |
|
$diDefinition->addMethodCall('setService', [DependencyFactory::MIGRATIONS_SORTER, new Reference($config['sorter'])]); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
6 |
|
if ($config['storage']['id'] !== null) { |
|
72
|
1 |
|
$diDefinition->addMethodCall('setService', [MetadataStorage::class, new Reference($config['storage']['id'])]); |
|
73
|
|
|
} else { |
|
74
|
5 |
|
$storageConfiguration = $config['storage']['table_storage']; |
|
75
|
|
|
|
|
76
|
5 |
|
$storageDefinition = new Definition(TableMetadataStorageConfiguration::class); |
|
77
|
5 |
|
$container->setDefinition('doctrine.migrations.storage.table_storage', $storageDefinition); |
|
78
|
5 |
|
$container->setAlias('doctrine.migrations.metadata_storage', 'doctrine.migrations.storage.table_storage'); |
|
79
|
|
|
|
|
80
|
5 |
|
if ($storageConfiguration['table_name']!== null) { |
|
81
|
1 |
|
$storageDefinition->addMethodCall('setTableName', [$storageConfiguration['table_name']]); |
|
82
|
|
|
} |
|
83
|
5 |
|
if ($storageConfiguration['version_column_name']!== null) { |
|
84
|
1 |
|
$storageDefinition->addMethodCall('setVersionColumnName', [$storageConfiguration['version_column_name']]); |
|
85
|
|
|
} |
|
86
|
5 |
|
if ($storageConfiguration['version_column_length']!== null) { |
|
87
|
1 |
|
$storageDefinition->addMethodCall('setVersionColumnLength', [$storageConfiguration['version_column_length']]); |
|
88
|
|
|
} |
|
89
|
5 |
|
if ($storageConfiguration['executed_at_column_name']!== null) { |
|
90
|
1 |
|
$storageDefinition->addMethodCall('setExecutedAtColumnName', [$storageConfiguration['executed_at_column_name']]); |
|
91
|
|
|
} |
|
92
|
5 |
|
if ($storageConfiguration['execution_time_column_name']!== null) { |
|
93
|
1 |
|
$storageDefinition->addMethodCall('setExecutionTimeColumnName', [$storageConfiguration['execution_time_column_name']]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
$configurationDefinition->addMethodCall('setMetadataStorageConfiguration', [new Reference('doctrine.migrations.storage.table_storage')]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
6 |
|
if ($config['em']!==null && $config['connection']!==null) { |
|
100
|
1 |
|
throw new InvalidArgumentException('You can not specify both "connection" and "em" in the DoctrineMigrationsBundle configurations.'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
5 |
|
$emID = $config['em']!== null ? sprintf('doctrine.orm.%s_entity_manager', $config['em']) : null; |
|
104
|
|
|
|
|
105
|
5 |
|
if ($emID !== null) { |
|
106
|
1 |
|
$connectionDef = new Definition(Connection::class); |
|
107
|
1 |
|
$connectionDef->setFactory([new Reference($emID), 'getConnection']); |
|
108
|
|
|
|
|
109
|
1 |
|
$connectionId = sprintf('doctrine.migrations.connection.%s', $emID); |
|
110
|
1 |
|
$container->setDefinition($connectionId, $connectionDef); |
|
111
|
|
|
|
|
112
|
1 |
|
$em = new Reference($emID); |
|
113
|
1 |
|
$connection = new Reference($connectionId); |
|
114
|
|
|
} else { |
|
115
|
4 |
|
$connectionId = sprintf('doctrine.dbal.%s_connection', $config['connection'] ?? 'default'); |
|
116
|
4 |
|
$connection = new Reference($connectionId); |
|
117
|
4 |
|
$em = null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
5 |
|
$diDefinition->setArgument(1, $connection); |
|
121
|
5 |
|
$diDefinition->setArgument(2, $em); |
|
122
|
5 |
|
$diDefinition->setArgument(3, new Reference('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE)); |
|
123
|
5 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Returns the base path for the XSD files. |
|
127
|
|
|
* |
|
128
|
|
|
* @return string The XSD base path |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getXsdValidationBasePath() : string |
|
131
|
|
|
{ |
|
132
|
|
|
return __DIR__ . '/../Resources/config/schema'; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getNamespace() : string |
|
136
|
|
|
{ |
|
137
|
|
|
return 'http://symfony.com/schema/dic/doctrine/migrations'; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|