1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Doctrine Naming Strategy Bundle, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2017 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\Bundle\DoctrineNamingStrategy\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\Bundle\DoctrineNamingStrategy\NamingStrategy\NamerCollection; |
13
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
14
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
15
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
16
|
|
|
|
17
|
|
|
class Configuration implements ConfigurationInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
6 |
|
public function getConfigTreeBuilder() |
23
|
|
|
{ |
24
|
6 |
|
$treeBuilder = new TreeBuilder(); |
25
|
|
|
|
26
|
6 |
|
$rootNode = $treeBuilder->root('run_open_code_doctrine_naming_strategy'); |
27
|
|
|
|
28
|
|
|
$rootNode |
29
|
6 |
|
->children() |
30
|
6 |
|
->append($this->getUnderscoredBundlePrefixDefinition()) |
31
|
6 |
|
->append($this->getUnderscoredClassNamespacePrefixDefinition()) |
32
|
6 |
|
->append($this->getNamerCollectionDefinition()) |
33
|
6 |
|
->end() |
34
|
6 |
|
->end(); |
35
|
|
|
|
36
|
6 |
|
return $treeBuilder; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Configure underscored bundle prefix naming strategy |
41
|
|
|
* |
42
|
|
|
* @return ArrayNodeDefinition |
43
|
|
|
*/ |
44
|
6 |
View Code Duplication |
protected function getUnderscoredBundlePrefixDefinition() |
|
|
|
|
45
|
|
|
{ |
46
|
6 |
|
$node = new ArrayNodeDefinition('underscored_bundle_prefix'); |
47
|
|
|
|
48
|
|
|
$node |
49
|
6 |
|
->addDefaultsIfNotSet() |
50
|
6 |
|
->children() |
51
|
6 |
|
->enumNode('case') |
52
|
6 |
|
->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
53
|
6 |
|
->values(array('lowercase', 'uppercase')) |
54
|
6 |
|
->defaultValue('lowercase') |
55
|
6 |
|
->end() |
56
|
6 |
|
->booleanNode('join_table_field_suffix') |
57
|
6 |
|
->defaultTrue() |
58
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
59
|
6 |
|
->end() |
60
|
6 |
|
->arrayNode('map') |
61
|
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.') |
62
|
6 |
|
->useAttributeAsKey('bundle') |
63
|
6 |
|
->prototype('scalar')->end() |
64
|
6 |
|
->end() |
65
|
6 |
|
->arrayNode('whitelist') |
66
|
6 |
|
->beforeNormalization() |
67
|
6 |
|
->ifString() |
68
|
|
|
->then(function ($value) { |
69
|
|
|
return [$value]; |
70
|
6 |
|
}) |
71
|
6 |
|
->end() |
72
|
6 |
|
->info('Define for which bundles to apply prefixes.') |
73
|
6 |
|
->prototype('scalar')->end() |
74
|
6 |
|
->end() |
75
|
6 |
|
->arrayNode('blacklist') |
76
|
6 |
|
->beforeNormalization() |
77
|
6 |
|
->ifString() |
78
|
|
|
->then(function ($value) { |
79
|
1 |
|
return [$value]; |
80
|
6 |
|
}) |
81
|
6 |
|
->end() |
82
|
6 |
|
->info('Define for which bundles not to apply prefixes.') |
83
|
6 |
|
->prototype('scalar')->end() |
84
|
6 |
|
->end() |
85
|
6 |
|
->end() |
86
|
6 |
|
->end(); |
87
|
|
|
|
88
|
6 |
|
return $node; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Configure underscored class namespace prefix naming strategy |
93
|
|
|
* |
94
|
|
|
* @return ArrayNodeDefinition |
95
|
|
|
*/ |
96
|
6 |
View Code Duplication |
protected function getUnderscoredClassNamespacePrefixDefinition() |
|
|
|
|
97
|
|
|
{ |
98
|
6 |
|
$node = new ArrayNodeDefinition('underscored_class_namespace_prefix'); |
99
|
|
|
|
100
|
|
|
$node |
101
|
6 |
|
->addDefaultsIfNotSet() |
102
|
6 |
|
->children() |
103
|
6 |
|
->enumNode('case') |
104
|
6 |
|
->info('Which case to use, lowercase or uppercase. Default is lowercase.') |
105
|
6 |
|
->values(array('lowercase', 'uppercase')) |
106
|
6 |
|
->defaultValue('lowercase') |
107
|
6 |
|
->end() |
108
|
6 |
|
->booleanNode('join_table_field_suffix') |
109
|
6 |
|
->defaultTrue() |
110
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
111
|
6 |
|
->end() |
112
|
6 |
|
->arrayNode('map') |
113
|
6 |
|
->requiresAtLeastOneElement() |
114
|
6 |
|
->info('Map of FQCNs prefixes and table prefixes to use for naming.') |
115
|
6 |
|
->useAttributeAsKey('namespace') |
116
|
6 |
|
->prototype('scalar')->end() |
117
|
6 |
|
->end() |
118
|
6 |
|
->arrayNode('whitelist') |
119
|
6 |
|
->beforeNormalization() |
120
|
6 |
|
->ifString() |
121
|
|
|
->then(function ($value) { |
122
|
|
|
return [$value]; |
123
|
6 |
|
}) |
124
|
6 |
|
->end() |
125
|
6 |
|
->info('Define for which FQCNs prefixes table prefixes should be applied.') |
126
|
6 |
|
->prototype('scalar')->end() |
127
|
6 |
|
->end() |
128
|
6 |
|
->arrayNode('blacklist') |
129
|
6 |
|
->beforeNormalization() |
130
|
6 |
|
->ifString() |
131
|
|
|
->then(function ($value) { |
132
|
|
|
return [$value]; |
133
|
6 |
|
}) |
134
|
6 |
|
->end() |
135
|
6 |
|
->info('Define for which FQCNs prefixes not to apply table prefixes.') |
136
|
6 |
|
->prototype('scalar')->end() |
137
|
6 |
|
->end() |
138
|
6 |
|
->end() |
139
|
6 |
|
->end(); |
140
|
|
|
|
141
|
6 |
|
return $node; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Configure namer collection |
146
|
|
|
* |
147
|
|
|
* @return ArrayNodeDefinition |
148
|
|
|
*/ |
149
|
6 |
|
protected function getNamerCollectionDefinition() |
150
|
|
|
{ |
151
|
6 |
|
$node = new ArrayNodeDefinition('underscored_namer_collection'); |
152
|
|
|
|
153
|
|
|
$node |
154
|
6 |
|
->addDefaultsIfNotSet() |
155
|
6 |
|
->fixXmlConfig('namer') |
156
|
|
|
|
157
|
6 |
|
->children() |
158
|
6 |
|
->scalarNode('default') |
159
|
6 |
|
->info('Default namer which will determine default name.') |
160
|
6 |
|
->defaultValue('doctrine.orm.naming_strategy.underscore') |
161
|
6 |
|
->end() |
162
|
6 |
|
->arrayNode('namers') |
163
|
6 |
|
->beforeNormalization() |
164
|
6 |
|
->ifString() |
165
|
6 |
|
->then(function ($value) { |
166
|
|
|
return [$value]; |
167
|
6 |
|
}) |
168
|
6 |
|
->end() |
169
|
6 |
|
->requiresAtLeastOneElement() |
170
|
6 |
|
->info('Concurrent namers (referenced as service) which will propose different name, if applicable.') |
171
|
6 |
|
->prototype('scalar')->end() |
172
|
6 |
|
->defaultValue([ |
173
|
6 |
|
'runopencode.doctrine.orm.naming_strategy.underscored_class_namespace_prefix', |
174
|
|
|
'runopencode.doctrine.orm.naming_strategy.underscored_bundle_prefix', |
175
|
|
|
]) |
176
|
6 |
|
->end() |
177
|
6 |
|
->booleanNode('join_table_field_suffix') |
178
|
6 |
|
->defaultTrue() |
179
|
6 |
|
->info('Join table will get field name suffix enabling you to have multiple many-to-many relations without stating explicitly table names.') |
180
|
6 |
|
->end() |
181
|
6 |
|
->end() |
182
|
6 |
|
->end(); |
183
|
|
|
|
184
|
6 |
|
return $node; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
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.