9
9
10
10
namespace Zend \ServiceManager ;
11
11
12
- use Zend \Stdlib \ArrayUtils ;
12
+ use Zend \Stdlib \ArrayUtils \MergeRemoveKey ;
13
+ use Zend \Stdlib \ArrayUtils \MergeReplaceKeyInterface ;
13
14
15
+ /**
16
+ * Object for defining configuration and configuring an existing service manager instance.
17
+ *
18
+ * In order to provide configuration merging capabilities, this class implements
19
+ * the same functionality as `Zend\Stdlib\ArrayUtils::merge()`. That routine
20
+ * allows developers to specifically shape how values are merged:
21
+ *
22
+ * - A value which is an instance of `MergeRemoveKey` indicates the value should
23
+ * be removed during merge.
24
+ * - A value that is an instance of `MergeReplaceKeyInterface` indicates that the
25
+ * value it contains should be used to replace any previous versions.
26
+ *
27
+ * These features are advanced, and not typically used. If you wish to use them,
28
+ * you will need to require the zend-stdlib package in your application.
29
+ */
14
30
class Config implements ConfigInterface
15
31
{
16
32
/**
17
- * Allowed configuration keys
18
- *
19
33
* @var array
20
34
*/
21
- protected $ allowedKeys = [
35
+ private $ allowedKeys = [
22
36
'abstract_factories ' => true ,
23
- 'aliases ' => true ,
24
- 'delegators ' => true ,
25
- 'factories ' => true ,
26
- 'initializers ' => true ,
27
- 'invokables ' => true ,
28
- 'lazy_services ' => true ,
29
- 'services ' => true ,
30
- 'shared ' => true ,
37
+ 'aliases ' => true ,
38
+ 'delegators ' => true ,
39
+ 'factories ' => true ,
40
+ 'initializers ' => true ,
41
+ 'invokables ' => true ,
42
+ 'lazy_services ' => true ,
43
+ 'services ' => true ,
44
+ 'shared ' => true ,
31
45
];
32
46
33
47
/**
@@ -46,8 +60,6 @@ class Config implements ConfigInterface
46
60
];
47
61
48
62
/**
49
- * Constructor
50
- *
51
63
* @param array $config
52
64
*/
53
65
public function __construct (array $ config = [])
@@ -58,15 +70,11 @@ public function __construct(array $config = [])
58
70
unset($ config [$ key ]);
59
71
}
60
72
}
61
-
62
- $ this ->config = ArrayUtils::merge ($ this ->config , $ config );
73
+ $ this ->config = $ this ->merge ($ this ->config , $ config );
63
74
}
64
75
65
76
/**
66
- * Configure service manager
67
- *
68
- * @param ServiceManager $serviceManager
69
- * @return ServiceManager Returns the updated service manager instance.
77
+ * @inheritdoc
70
78
*/
71
79
public function configureServiceManager (ServiceManager $ serviceManager )
72
80
{
@@ -80,4 +88,34 @@ public function toArray()
80
88
{
81
89
return $ this ->config ;
82
90
}
91
+
92
+ /**
93
+ * Copy paste from https://github.com/zendframework/zend-stdlib/commit/26fcc32a358aa08de35625736095cb2fdaced090
94
+ * to keep compatibility with previous version
95
+ *
96
+ * @link https://github.com/zendframework/zend-servicemanager/pull/68
97
+ */
98
+ private function merge (array $ a , array $ b )
99
+ {
100
+ foreach ($ b as $ key => $ value ) {
101
+ if ($ value instanceof MergeReplaceKeyInterface) {
102
+ $ a [$ key ] = $ value ->getData ();
103
+ } elseif (isset ($ a [$ key ]) || array_key_exists ($ key , $ a )) {
104
+ if ($ value instanceof MergeRemoveKey) {
105
+ unset($ a [$ key ]);
106
+ } elseif (is_int ($ key )) {
107
+ $ a [] = $ value ;
108
+ } elseif (is_array ($ value ) && is_array ($ a [$ key ])) {
109
+ $ a [$ key ] = $ this ->merge ($ a [$ key ], $ value );
110
+ } else {
111
+ $ a [$ key ] = $ value ;
112
+ }
113
+ } else {
114
+ if (!$ value instanceof MergeRemoveKey) {
115
+ $ a [$ key ] = $ value ;
116
+ }
117
+ }
118
+ }
119
+ return $ a ;
120
+ }
83
121
}
0 commit comments