@@ -29,17 +29,17 @@ class ConfigPostProcessor
29
29
'zf-apigility ' => 'api-tools ' ,
30
30
];
31
31
32
- /** @var Replacements */
32
+ /**
33
+ * @psalm-suppress PropertyNotSetInConstructor Initialized during call to the only public method __invoke()
34
+ * @var Replacements
35
+ */
33
36
private $ replacements ;
34
37
35
38
/** @var callable[] */
36
39
private $ rulesets ;
37
40
38
41
public function __construct ()
39
42
{
40
- // This value will be reset during __invoke(); setting here to prevent Psalm errors.
41
- $ this ->replacements = new Replacements ();
42
-
43
43
/* Define the rulesets for replacements.
44
44
*
45
45
* Each ruleset has the following signature:
@@ -86,7 +86,7 @@ function ($value) {
86
86
// Array values
87
87
function ($ value , array $ keys ) {
88
88
return $ keys !== [] && is_array ($ value )
89
- ? [$ this , '__invoke ' ]
89
+ ? [$ this , 'processConfig ' ]
90
90
: null ;
91
91
},
92
92
];
@@ -100,51 +100,7 @@ function ($value, array $keys) {
100
100
public function __invoke (array $ config , array $ keys = [])
101
101
{
102
102
$ this ->replacements = $ this ->initializeReplacements ($ config );
103
- $ rewritten = [];
104
-
105
- foreach ($ config as $ key => $ value ) {
106
- // Do not rewrite configuration for the bridge
107
- if ($ key === 'laminas-zendframework-bridge ' ) {
108
- $ rewritten [$ key ] = $ value ;
109
- continue ;
110
- }
111
-
112
- // Determine new key from replacements
113
- $ newKey = is_string ($ key ) ? $ this ->replace ($ key , $ keys ) : $ key ;
114
-
115
- // Keep original values with original key, if the key has changed, but only at the top-level.
116
- if (empty ($ keys ) && $ newKey !== $ key ) {
117
- $ rewritten [$ key ] = $ value ;
118
- }
119
-
120
- // Perform value replacements, if any
121
- $ newValue = $ this ->replace ($ value , $ keys , $ newKey );
122
-
123
- // Key does not already exist and/or is not an array value
124
- if (! array_key_exists ($ newKey , $ rewritten ) || ! is_array ($ rewritten [$ newKey ])) {
125
- // Do not overwrite existing values with null values
126
- $ rewritten [$ newKey ] = array_key_exists ($ newKey , $ rewritten ) && null === $ newValue
127
- ? $ rewritten [$ newKey ]
128
- : $ newValue ;
129
- continue ;
130
- }
131
-
132
- // New value is null; nothing to do.
133
- if (null === $ newValue ) {
134
- continue ;
135
- }
136
-
137
- // Key already exists as an array value, but $value is not an array
138
- if (! is_array ($ newValue )) {
139
- $ rewritten [$ newKey ][] = $ newValue ;
140
- continue ;
141
- }
142
-
143
- // Key already exists as an array value, and $value is also an array
144
- $ rewritten [$ newKey ] = static ::merge ($ rewritten [$ newKey ], $ newValue );
145
- }
146
-
147
- return $ rewritten ;
103
+ return $ this ->processConfig ($ config , $ keys );
148
104
}
149
105
150
106
/**
@@ -270,7 +226,7 @@ private function replaceDependencyConfiguration(array $config)
270
226
continue ;
271
227
}
272
228
273
- $ config [$ key ] = is_array ($ data ) ? $ this ->__invoke ($ data , [$ key ]) : $ data ;
229
+ $ config [$ key ] = is_array ($ data ) ? $ this ->processConfig ($ data , [$ key ]) : $ data ;
274
230
}
275
231
276
232
return $ config ;
@@ -414,7 +370,7 @@ private function replaceDependencyServices(array $config)
414
370
}
415
371
416
372
$ replacedService = $ this ->replacements ->replace ($ service );
417
- $ serviceInstance = is_array ($ serviceInstance ) ? $ this ->__invoke ($ serviceInstance ) : $ serviceInstance ;
373
+ $ serviceInstance = is_array ($ serviceInstance ) ? $ this ->processConfig ($ serviceInstance ) : $ serviceInstance ;
418
374
419
375
$ config ['services ' ][$ replacedService ] = $ serviceInstance ;
420
376
@@ -461,4 +417,57 @@ private function initializeReplacements(array $config): Replacements
461
417
462
418
return new Replacements ($ replacements );
463
419
}
420
+
421
+ /**
422
+ * @param string[] $keys Hierarchy of keys, for determining location in
423
+ * nested configuration.
424
+ */
425
+ private function processConfig (array $ config , array $ keys = []): array
426
+ {
427
+ $ rewritten = [];
428
+
429
+ foreach ($ config as $ key => $ value ) {
430
+ // Do not rewrite configuration for the bridge
431
+ if ($ key === 'laminas-zendframework-bridge ' ) {
432
+ $ rewritten [$ key ] = $ value ;
433
+ continue ;
434
+ }
435
+
436
+ // Determine new key from replacements
437
+ $ newKey = is_string ($ key ) ? $ this ->replace ($ key , $ keys ) : $ key ;
438
+
439
+ // Keep original values with original key, if the key has changed, but only at the top-level.
440
+ if (empty ($ keys ) && $ newKey !== $ key ) {
441
+ $ rewritten [$ key ] = $ value ;
442
+ }
443
+
444
+ // Perform value replacements, if any
445
+ $ newValue = $ this ->replace ($ value , $ keys , $ newKey );
446
+
447
+ // Key does not already exist and/or is not an array value
448
+ if (!array_key_exists ($ newKey , $ rewritten ) || !is_array ($ rewritten [$ newKey ])) {
449
+ // Do not overwrite existing values with null values
450
+ $ rewritten [$ newKey ] = array_key_exists ($ newKey , $ rewritten ) && null === $ newValue
451
+ ? $ rewritten [$ newKey ]
452
+ : $ newValue ;
453
+ continue ;
454
+ }
455
+
456
+ // New value is null; nothing to do.
457
+ if (null === $ newValue ) {
458
+ continue ;
459
+ }
460
+
461
+ // Key already exists as an array value, but $value is not an array
462
+ if (!is_array ($ newValue )) {
463
+ $ rewritten [$ newKey ][] = $ newValue ;
464
+ continue ;
465
+ }
466
+
467
+ // Key already exists as an array value, and $value is also an array
468
+ $ rewritten [$ newKey ] = static ::merge ($ rewritten [$ newKey ], $ newValue );
469
+ }
470
+
471
+ return $ rewritten ;
472
+ }
464
473
}
0 commit comments