Skip to content

Commit 9cda8a8

Browse files
committed
Use ArrayNodeDefinition::acceptAndWrap
Enable more specific types Requires symfony/symfony#51273
1 parent 3b0b446 commit 9cda8a8

File tree

1 file changed

+40
-80
lines changed

1 file changed

+40
-80
lines changed

src/DependencyInjection/Configuration.php

Lines changed: 40 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -53,40 +53,22 @@ public function getConfigTreeBuilder(): TreeBuilder
5353
->scalarNode('auto_generate_proxy_classes')
5454
->defaultValue(ODMConfiguration::AUTOGENERATE_EVAL)
5555
->beforeNormalization()
56-
->always(static function ($v) {
57-
if ($v === false) {
58-
return ODMConfiguration::AUTOGENERATE_EVAL;
59-
}
60-
61-
if ($v === true) {
62-
return ODMConfiguration::AUTOGENERATE_FILE_NOT_EXISTS;
63-
}
64-
65-
return $v;
66-
})
56+
->ifTrue()->then(static fn ($v) => ODMConfiguration::AUTOGENERATE_FILE_NOT_EXISTS)
57+
->ifFalse()->then(static fn ($v) => ODMConfiguration::AUTOGENERATE_NEVER)
6758
->end()
6859
->end()
6960
->scalarNode('hydrator_namespace')->defaultValue('Hydrators')->end()
7061
->scalarNode('hydrator_dir')->defaultValue('%kernel.cache_dir%/doctrine/odm/mongodb/Hydrators')->end()
7162
->scalarNode('auto_generate_hydrator_classes')
7263
->defaultValue(ODMConfiguration::AUTOGENERATE_NEVER)
7364
->beforeNormalization()
74-
->always(static function ($v) {
75-
if ($v === false) {
76-
return ODMConfiguration::AUTOGENERATE_NEVER;
77-
}
78-
79-
if ($v === true) {
80-
return ODMConfiguration::AUTOGENERATE_ALWAYS;
81-
}
82-
83-
return $v;
84-
})
65+
->ifTrue()->then(static fn ($v) => ODMConfiguration::AUTOGENERATE_ALWAYS)
66+
->ifFalse()->then(static fn ($v) => ODMConfiguration::AUTOGENERATE_NEVER)
8567
->end()
8668
->end()
8769
->scalarNode('persistent_collection_namespace')->defaultValue('PersistentCollections')->end()
8870
->scalarNode('persistent_collection_dir')->defaultValue('%kernel.cache_dir%/doctrine/odm/mongodb/PersistentCollections')->end()
89-
->scalarNode('auto_generate_persistent_collection_classes')->defaultValue(ODMConfiguration::AUTOGENERATE_NEVER)->end()
71+
->integerNode('auto_generate_persistent_collection_classes')->defaultValue(ODMConfiguration::AUTOGENERATE_NEVER)->end()
9072
->scalarNode('default_document_manager')->end()
9173
->scalarNode('default_connection')->end()
9274
->scalarNode('default_database')->defaultValue('default')->end()
@@ -149,20 +131,15 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode): void
149131
->useAttributeAsKey('name')
150132
->prototype('array')
151133
->fixXmlConfig('parameter')
152-
->beforeNormalization()
153-
->ifString()
154-
->then(static function ($v) {
155-
return ['class' => $v];
156-
})
157-
->end()
134+
->acceptAndWrap(['string'], 'class')
158135
->beforeNormalization()
159136
// The content of the XML node is returned as the "value" key so we need to rename it
160-
->ifTrue(static function ($v): bool {
161-
return is_array($v) && isset($v['value']);
162-
})
137+
->ifArray()
163138
->then(static function ($v) {
164-
$v['class'] = $v['value'];
165-
unset($v['value']);
139+
if (isset($v['value']) && ! isset($v['class'])) {
140+
$v['class'] = $v['value'];
141+
unset($v['value']);
142+
}
166143

167144
return $v;
168145
})
@@ -175,13 +152,15 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode): void
175152
->useAttributeAsKey('name')
176153
->prototype('variable')
177154
->beforeNormalization()
178-
// Detect JSON object and array syntax (for XML)
179-
->ifTrue(static function ($v): bool {
180-
return is_string($v) && (preg_match('/\[.*\]/', $v) || preg_match('/\{.*\}/', $v));
181-
})
182-
// Decode objects to associative arrays for consistency with YAML
155+
->ifString()
183156
->then(static function ($v) {
184-
return json_decode($v, true);
157+
// Detect JSON object and array syntax (for XML)
158+
// Decode objects to associative arrays for consistency with YAML
159+
if (is_string($v) && (preg_match('/\[.*\]/', $v) || preg_match('/\{.*\}/', $v))) {
160+
return json_decode($v, true);
161+
}
162+
163+
return $v;
185164
})
186165
->end()
187166
->end()
@@ -191,12 +170,7 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode): void
191170
->end()
192171
->arrayNode('metadata_cache_driver')
193172
->addDefaultsIfNotSet()
194-
->beforeNormalization()
195-
->ifString()
196-
->then(static function ($v) {
197-
return ['type' => $v];
198-
})
199-
->end()
173+
->acceptAndWrap(['string'], 'type')
200174
->children()
201175
->scalarNode('type')->defaultValue('array')->end()
202176
->scalarNode('class')->end()
@@ -214,12 +188,7 @@ private function addDocumentManagersSection(ArrayNodeDefinition $rootNode): void
214188
->arrayNode('mappings')
215189
->useAttributeAsKey('name')
216190
->prototype('array')
217-
->beforeNormalization()
218-
->ifString()
219-
->then(static function ($v) {
220-
return ['type' => $v];
221-
})
222-
->end()
191+
->acceptAndWrap(['string'], 'type')
223192
->treatNullLike([])
224193
->treatFalseLike(['mapping' => false])
225194
->performNoDeepMerging()
@@ -276,17 +245,18 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
276245
->performNoDeepMerging()
277246
->prototype('array')
278247
->beforeNormalization()
279-
// Handle readPreferenceTag XML nodes
280-
->ifTrue(static function ($v): bool {
281-
return isset($v['readPreferenceTag']);
282-
})
248+
->ifArray()
283249
->then(static function ($v) {
284250
// Equivalent of fixXmlConfig() for inner node
285251
if (isset($v['readPreferenceTag']['name'])) {
286-
$v['readPreferenceTag'] = [$v['readPreferenceTag']];
252+
return [$v['readPreferenceTag']];
253+
}
254+
255+
if (isset($v['readPreferenceTag'])) {
256+
return $v['readPreferenceTag'];
287257
}
288258

289-
return $v['readPreferenceTag'];
259+
return $v;
290260
})
291261
->end()
292262
->useAttributeAsKey('name')
@@ -323,11 +293,11 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
323293
->integerNode('wTimeoutMS')->end()
324294
->end()
325295
->validate()
326-
->ifTrue(static function ($v): bool {
327-
return count($v['readPreferenceTags']) === 0;
328-
})
296+
->ifArray()
329297
->then(static function ($v) {
330-
unset($v['readPreferenceTags']);
298+
if (! $v['readPreferenceTags']) {
299+
unset($v['readPreferenceTags']);
300+
}
331301

332302
return $v;
333303
})
@@ -399,16 +369,12 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
399369
->arrayNode('encryptedFieldsMap')
400370
->useAttributeAsKey('name', false)
401371
->beforeNormalization()
402-
->always(static function ($v) {
403-
// Create a PHP array representation of the Extended BSON that is later
404-
// converted to JSON string to create a BSON document from this JSON.
405-
// This lets the DI dumper transform the parameters in the string and dump it.
406-
if (is_string($v)) {
407-
return json_decode($v, true, 512, JSON_THROW_ON_ERROR);
408-
}
409-
410-
return $v;
411-
})->end()
372+
->ifString()
373+
// Create a PHP array representation of the Extended BSON that is later
374+
// converted to JSON string to create a BSON document from this JSON.
375+
// This lets the DI dumper transform the parameters in the string and dump it.
376+
->then(static fn ($v) => json_decode($v, true, 512, JSON_THROW_ON_ERROR))
377+
->end()
412378
->prototype('array')
413379
->children()
414380
->arrayNode('fields')
@@ -440,10 +406,7 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
440406
->booleanNode('mongocryptdBypassSpawn')->end()
441407
->scalarNode('mongocryptdSpawnPath')->end()
442408
->arrayNode('mongocryptdSpawnArgs')
443-
->beforeNormalization()
444-
->ifString()
445-
->then(static fn ($v) => [$v])
446-
->end()
409+
->acceptAndWrap(['string'])
447410
->prototype('scalar')->cannotBeEmpty()->end()
448411
->end()
449412
->scalarNode('cryptSharedLibPath')->end()
@@ -517,10 +480,7 @@ private function addTypesSection(ArrayNodeDefinition $rootNode): void
517480
->arrayNode('types')
518481
->useAttributeAsKey('name')
519482
->prototype('array')
520-
->beforeNormalization()
521-
->ifString()
522-
->then(static fn ($v) => ['class' => $v])
523-
->end()
483+
->acceptAndWrap(['string'], 'class')
524484
->children()
525485
->scalarNode('class')->isRequired()->end()
526486
->end()

0 commit comments

Comments
 (0)