1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
8
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
9
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
10
|
|
|
|
11
|
|
|
final class Configuration implements ConfigurationInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
6 |
|
public function getConfigTreeBuilder(): TreeBuilder |
17
|
|
|
{ |
18
|
6 |
|
$treeBuilder = new TreeBuilder('runopencode_doctrine_naming_strategy'); |
19
|
6 |
|
$rootNode = $treeBuilder->getRootNode(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @psalm-suppress all |
23
|
|
|
*/ |
24
|
|
|
$rootNode |
25
|
6 |
|
->children() |
26
|
6 |
|
->append($this->getUnderscoredBundlePrefixDefinition()) |
27
|
6 |
|
->append($this->getUnderscoredClassNamespacePrefixDefinition()) |
28
|
6 |
|
->append($this->getNamerCollectionDefinition()) |
29
|
6 |
|
->end() |
30
|
6 |
|
->end(); |
31
|
|
|
|
32
|
6 |
|
return $treeBuilder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Configure underscored bundle prefix naming strategy |
37
|
|
|
*/ |
38
|
6 |
View Code Duplication |
private function getUnderscoredBundlePrefixDefinition(): ArrayNodeDefinition |
|
|
|
|
39
|
|
|
{ |
40
|
6 |
|
$node = new ArrayNodeDefinition('underscored_bundle_prefix'); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @psalm-suppress all |
44
|
|
|
*/ |
45
|
|
|
$node |
46
|
6 |
|
->addDefaultsIfNotSet() |
47
|
6 |
|
->children() |
48
|
6 |
|
->enumNode('case') |
49
|
6 |
|
->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
50
|
6 |
|
->values(array('lowercase', 'uppercase')) |
51
|
6 |
|
->defaultValue('lowercase') |
52
|
6 |
|
->end() |
53
|
6 |
|
->booleanNode('join_table_field_suffix') |
54
|
6 |
|
->defaultTrue() |
55
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
56
|
6 |
|
->end() |
57
|
6 |
|
->arrayNode('map') |
58
|
6 |
|
->info('Map of short bundle names and prefixes, if you do not want to use full bundle name in prefix. Useful when bundle name is too long, considering that, per example, MySQL has 60 chars table name limit.') |
59
|
6 |
|
->useAttributeAsKey('bundle') |
60
|
6 |
|
->prototype('scalar')->end() |
61
|
6 |
|
->end() |
62
|
6 |
|
->arrayNode('whitelist') |
63
|
6 |
|
->beforeNormalization() |
64
|
6 |
|
->ifString() |
65
|
|
|
->then(static function ($value) { |
66
|
|
|
return [$value]; |
67
|
6 |
|
}) |
68
|
6 |
|
->end() |
69
|
6 |
|
->info('Define for which bundles to apply prefixes.') |
70
|
6 |
|
->prototype('scalar')->end() |
71
|
6 |
|
->end() |
72
|
6 |
|
->arrayNode('blacklist') |
73
|
6 |
|
->beforeNormalization() |
74
|
6 |
|
->ifString() |
75
|
|
|
->then(static function ($value) { |
76
|
1 |
|
return [$value]; |
77
|
6 |
|
}) |
78
|
6 |
|
->end() |
79
|
6 |
|
->info('Define for which bundles not to apply prefixes.') |
80
|
6 |
|
->prototype('scalar')->end() |
81
|
6 |
|
->end() |
82
|
6 |
|
->end() |
83
|
6 |
|
->end(); |
84
|
|
|
|
85
|
6 |
|
return $node; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Configure underscored class namespace prefix naming strategy |
90
|
|
|
*/ |
91
|
6 |
View Code Duplication |
private function getUnderscoredClassNamespacePrefixDefinition(): ArrayNodeDefinition |
|
|
|
|
92
|
|
|
{ |
93
|
6 |
|
$node = new ArrayNodeDefinition('underscored_class_namespace_prefix'); |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @psalm-suppress all |
97
|
|
|
*/ |
98
|
|
|
$node |
99
|
6 |
|
->addDefaultsIfNotSet() |
100
|
6 |
|
->children() |
101
|
6 |
|
->enumNode('case') |
102
|
6 |
|
->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
103
|
6 |
|
->values(array('lowercase', 'uppercase')) |
104
|
6 |
|
->defaultValue('lowercase') |
105
|
6 |
|
->end() |
106
|
6 |
|
->booleanNode('join_table_field_suffix') |
107
|
6 |
|
->defaultTrue() |
108
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
109
|
6 |
|
->end() |
110
|
6 |
|
->arrayNode('map') |
111
|
6 |
|
->requiresAtLeastOneElement() |
112
|
6 |
|
->info('Map of FQCNs prefixes and table prefixes to use for naming.') |
113
|
6 |
|
->useAttributeAsKey('namespace') |
114
|
6 |
|
->prototype('scalar')->end() |
115
|
6 |
|
->end() |
116
|
6 |
|
->arrayNode('whitelist') |
117
|
6 |
|
->beforeNormalization() |
118
|
6 |
|
->ifString() |
119
|
|
|
->then(static function ($value) { |
120
|
|
|
return [$value]; |
121
|
6 |
|
}) |
122
|
6 |
|
->end() |
123
|
6 |
|
->info('Define for which FQCNs prefixes table prefixes should be applied.') |
124
|
6 |
|
->prototype('scalar')->end() |
125
|
6 |
|
->end() |
126
|
6 |
|
->arrayNode('blacklist') |
127
|
6 |
|
->beforeNormalization() |
128
|
6 |
|
->ifString() |
129
|
|
|
->then(static function ($value) { |
130
|
|
|
return [$value]; |
131
|
6 |
|
}) |
132
|
6 |
|
->end() |
133
|
6 |
|
->info('Define for which FQCNs prefixes not to apply table prefixes.') |
134
|
6 |
|
->prototype('scalar')->end() |
135
|
6 |
|
->end() |
136
|
6 |
|
->end() |
137
|
6 |
|
->end(); |
138
|
|
|
|
139
|
6 |
|
return $node; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Configure namer collection |
144
|
|
|
*/ |
145
|
6 |
|
private function getNamerCollectionDefinition(): ArrayNodeDefinition |
146
|
|
|
{ |
147
|
6 |
|
$node = new ArrayNodeDefinition('underscored_namer_collection'); |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @psalm-suppress all |
151
|
|
|
*/ |
152
|
|
|
$node |
153
|
6 |
|
->addDefaultsIfNotSet() |
154
|
6 |
|
->fixXmlConfig('namer') |
155
|
6 |
|
->children() |
156
|
6 |
|
->scalarNode('default') |
157
|
6 |
|
->info('Default namer which will determine default name.') |
158
|
6 |
|
->defaultValue('doctrine.orm.naming_strategy.underscore') |
159
|
6 |
|
->end() |
160
|
6 |
|
->arrayNode('namers') |
161
|
6 |
|
->beforeNormalization() |
162
|
6 |
|
->ifString() |
163
|
|
|
->then(static function ($value) { |
164
|
|
|
return [$value]; |
165
|
6 |
|
}) |
166
|
6 |
|
->end() |
167
|
6 |
|
->requiresAtLeastOneElement() |
168
|
6 |
|
->info('Concurrent namers (referenced as service) which will propose different name, if applicable.') |
169
|
6 |
|
->prototype('scalar')->end() |
170
|
6 |
|
->defaultValue([ |
171
|
6 |
|
'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix', |
172
|
|
|
'runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix', |
173
|
|
|
]) |
174
|
6 |
|
->end() |
175
|
6 |
|
->booleanNode('join_table_field_suffix') |
176
|
6 |
|
->defaultTrue() |
177
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
178
|
6 |
|
->end() |
179
|
6 |
|
->end() |
180
|
6 |
|
->end(); |
181
|
|
|
|
182
|
6 |
|
return $node; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.