Newer
Older
Klaranouba7
a validé
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
<?php
declare(strict_types=1);
namespace Doctrine\Bundle\MigrationsBundle\DependencyInjection;
use ReflectionClass;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use function array_filter;
use function array_keys;
use function constant;
use function count;
use function in_array;
use function is_string;
use function method_exists;
use function strlen;
use function strpos;
use function strtoupper;
use function substr;
/**
* DoctrineMigrationsExtension configuration structure.
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree.
*
* @return TreeBuilder The config tree builder
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('doctrine_migrations');
if (method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->getRootNode();
} else {
// BC layer for symfony/config 4.1 and older
$rootNode = $treeBuilder->root('doctrine_migrations', 'array');
}
$organizeMigrationModes = $this->getOrganizeMigrationsModes();
$rootNode
->fixXmlConfig('migration', 'migrations')
->fixXmlConfig('migrations_path', 'migrations_paths')
->children()
->arrayNode('migrations_paths')
->info('A list of namespace/path pairs where to look for migrations.')
->defaultValue([])
->useAttributeAsKey('namespace')
->prototype('scalar')->end()
->end()
->arrayNode('services')
->info('A set of services to pass to the underlying doctrine/migrations library, allowing to change its behaviour.')
->useAttributeAsKey('service')
->defaultValue([])
->validate()
->ifTrue(static function ($v): bool {
return count(array_filter(array_keys($v), static function (string $doctrineService): bool {
return strpos($doctrineService, 'Doctrine\Migrations\\') !== 0;
})) !== 0;
})
->thenInvalid('Valid services for the DoctrineMigrationsBundle must be in the "Doctrine\Migrations" namespace.')
->end()
->prototype('scalar')->end()
->end()
->arrayNode('factories')
->info('A set of callables to pass to the underlying doctrine/migrations library as services, allowing to change its behaviour.')
->useAttributeAsKey('factory')
->defaultValue([])
->validate()
->ifTrue(static function ($v): bool {
return count(array_filter(array_keys($v), static function (string $doctrineService): bool {
return strpos($doctrineService, 'Doctrine\Migrations\\') !== 0;
})) !== 0;
})
->thenInvalid('Valid callables for the DoctrineMigrationsBundle must be in the "Doctrine\Migrations" namespace.')
->end()
->prototype('scalar')->end()
->end()
->arrayNode('storage')
->addDefaultsIfNotSet()
->info('Storage to use for migration status metadata.')
->children()
->arrayNode('table_storage')
->addDefaultsIfNotSet()
->info('The default metadata storage, implemented as a table in the database.')
->children()
->scalarNode('table_name')->defaultValue(null)->cannotBeEmpty()->end()
->scalarNode('version_column_name')->defaultValue(null)->end()
->scalarNode('version_column_length')->defaultValue(null)->end()
->scalarNode('executed_at_column_name')->defaultValue(null)->end()
->scalarNode('execution_time_column_name')->defaultValue(null)->end()
->end()
->end()
->end()
->end()
->arrayNode('migrations')
->info('A list of migrations to load in addition to the one discovered via "migrations_paths".')
->prototype('scalar')->end()
->defaultValue([])
->end()
->scalarNode('connection')
->info('Connection name to use for the migrations database.')
->defaultValue(null)
->end()
->scalarNode('em')
->info('Entity manager name to use for the migrations database (available when doctrine/orm is installed).')
->defaultValue(null)
->end()
->scalarNode('all_or_nothing')
->info('Run all migrations in a transaction.')
->defaultValue(false)
->end()
->scalarNode('check_database_platform')
->info('Adds an extra check in the generated migrations to allow execution only on the same platform as they were initially generated on.')
->defaultValue(true)
->end()
->scalarNode('custom_template')
->info('Custom template path for generated migration classes.')
->defaultValue(null)
->end()
->scalarNode('organize_migrations')
->defaultValue(false)
->info('Organize migrations mode. Possible values are: "BY_YEAR", "BY_YEAR_AND_MONTH", false')
->validate()
->ifTrue(static function ($v) use ($organizeMigrationModes): bool {
if ($v === false) {
return false;
}
return ! is_string($v) || ! in_array(strtoupper($v), $organizeMigrationModes, true);
})
->thenInvalid('Invalid organize migrations mode value %s')
->end()
->validate()
->ifString()
->then(static function ($v) {
return constant('Doctrine\Migrations\Configuration\Configuration::VERSIONS_ORGANIZATION_' . strtoupper($v));
})
->end()
->end()
->booleanNode('enable_profiler')
->info('Use profiler to calculate and visualize migration status.')
->defaultFalse()
->end()
->booleanNode('transactional')
->info('Whether or not to wrap migrations in a single transaction.')
->defaultTrue()
->end()
->end();
return $treeBuilder;
}
/**
* Find organize migrations modes for their names
*
* @return string[]
*/
private function getOrganizeMigrationsModes(): array
{
$constPrefix = 'VERSIONS_ORGANIZATION_';
$prefixLen = strlen($constPrefix);
$refClass = new ReflectionClass('Doctrine\Migrations\Configuration\Configuration');
$constsArray = $refClass->getConstants();
$namesArray = [];
foreach ($constsArray as $key => $value) {
if (strpos($key, $constPrefix) !== 0) {
continue;
}
$namesArray[] = substr($key, $prefixLen);
}
return $namesArray;
}
}