@@ -38,22 +38,38 @@ public function values(string $path, array $scopes): array
3838 {
3939 $ levels = $ this ->getPathLevels ($ path );
4040
41- if (!$ this ->configCache ) {
42- $ this ->configCache = $ this ->config ->get ('system ' );
43- }
41+ $ configCache = $ this ->getConfigCache ();
4442
4543 $ values = array_fill_keys ($ scopes , []);
46- $ defaultScopeValues = $ this ->getScopeValues ($ this -> configCache ['default ' ], $ levels );
44+ $ defaultScopeValues = $ this ->getScopeValues ($ configCache ['default ' ], $ levels );
4745
48- foreach ($ scopes as $ scope ) {
49- if (!isset ($ this ->configCache [$ scope ])) {
50- throw new InvalidArgumentException (__ ('Invalid scope key: ' . $ scope ));
51- }
52- if ($ scope === 'default ' ) {
53- $ values [$ scope ] = $ defaultScopeValues ;
54- continue ;
46+ if (($ key = array_search ('default ' , $ scopes )) !== false ) {
47+ $ values ['default ' ] = $ defaultScopeValues ;
48+ unset($ scopes [$ key ]);
49+ }
50+
51+ $ values = array_merge_recursive (
52+ $ values ,
53+ $ this ->getNonDefaultScopeValues ($ scopes , $ levels , $ defaultScopeValues )
54+ );
55+
56+ foreach ($ values as $ scope => $ scopeData ) {
57+ if (count ($ scopeData ) == 0 ) {
58+ unset($ values [$ scope ]);
5559 }
60+ }
5661
62+ return $ values ;
63+ }
64+
65+ private function getNonDefaultScopeValues (
66+ array $ scopes ,
67+ array $ levels ,
68+ array $ defaultScopeValues
69+ ): array {
70+ $ values = [];
71+ foreach ($ scopes as $ scope ) {
72+ $ values [$ scope ] = [];
5773 foreach ($ this ->configCache [$ scope ] as $ scopeKey => $ scopeData ) {
5874 if (is_numeric ($ scopeKey ) || in_array ($ scopeKey , ['admin ' , 'default ' ])) {
5975 continue ;
@@ -68,12 +84,6 @@ public function values(string $path, array $scopes): array
6884 }
6985 }
7086
71- foreach ($ values as $ scope => $ scopeData ) {
72- if (count ($ scopeData ) == 0 ) {
73- unset($ values [$ scope ]);
74- }
75- }
76-
7787 return $ values ;
7888 }
7989
@@ -109,33 +119,56 @@ protected function getScopeValues(array $scopeData, array $levels): array
109119 return [];
110120 }
111121
112- $ scopeData = $ scopeData [$ level1 ];
113- if ($ level2 === '* ' && $ level3 === '* ' ) {
114- return [$ level1 => $ scopeData ];
122+ if ($ wildcardScopes = $ this ->getL3WildCardScopes ($ levels , $ scopeData )) {
123+ return $ wildcardScopes ;
115124 }
116125
117- if ($ level2 !== ' * ' && $ level3 === ' * ' ) {
118- return [ $ level1 => [ $ level2 => $ scopeData [ $ level2 ]]] ;
126+ if ($ wildcardScopes = $ this -> getL2WildCardScopes ( $ levels , $ scopeData ) ) {
127+ return $ wildcardScopes ;
119128 }
120129
121- if ($ level2 === '* ' && $ level3 !== '* ' ) {
122- $ values = [$ level1 => []];
123- foreach ($ scopeData as $ level2 => $ l3scopeData ) {
124- foreach ($ l3scopeData as $ key => $ value ) {
125- if ($ key === $ level3 ) {
126- $ values [$ level1 ][$ level2 ] = $ values [$ level1 ][$ level2 ] ?? [];
127- $ values [$ level1 ][$ level2 ][$ level3 ] = $ value ;
128- }
130+ $ scopeData = $ scopeData [$ level1 ];
131+ if (isset ($ scopeData [$ level2 ]) && isset ($ scopeData [$ level2 ][$ level3 ])) {
132+ return [$ level1 => [$ level2 => [$ level3 => $ scopeData [$ level2 ][$ level3 ]]]];
133+ }
134+
135+ return [];
136+ }
137+
138+ private function getL2WildCardScopes (array $ levels , array $ scopeData ): ?array
139+ {
140+ [$ level1 , $ level2 , $ level3 ] = $ levels ;
141+
142+ $ scopeData = $ scopeData [$ level1 ];
143+ if ($ level2 !== '* ' ) {
144+ return null ;
145+ }
146+ $ values = [$ level1 => []];
147+ foreach ($ scopeData as $ level2 => $ l3scopeData ) {
148+ foreach ($ l3scopeData as $ key => $ value ) {
149+ if ($ key === $ level3 ) {
150+ $ values [$ level1 ][$ level2 ] = $ values [$ level1 ][$ level2 ] ?? [];
151+ $ values [$ level1 ][$ level2 ][$ level3 ] = $ value ;
129152 }
130153 }
131- return $ values ;
132154 }
155+ return $ values ;
156+ }
133157
134- if (isset ($ scopeData [$ level2 ]) && isset ($ scopeData [$ level2 ][$ level3 ])) {
135- return [$ level1 => [$ level2 => [$ level3 => $ scopeData [$ level2 ][$ level3 ]]]];
158+ private function getL3WildCardScopes (array $ levels , array $ scopeData ): ?array
159+ {
160+ [$ level1 , $ level2 , $ level3 ] = $ levels ;
161+
162+ $ scopeData = $ scopeData [$ level1 ];
163+ if ($ level2 === '* ' && $ level3 === '* ' ) {
164+ return [$ level1 => $ scopeData ];
136165 }
137166
138- return [];
167+ if ($ level2 !== '* ' && $ level3 === '* ' ) {
168+ return [$ level1 => [$ level2 => $ scopeData [$ level2 ]]];
169+ }
170+
171+ return null ;
139172 }
140173
141174 /**
@@ -158,4 +191,13 @@ protected function getPathLevels(string $path): array
158191
159192 return $ levels ;
160193 }
194+
195+ private function getConfigCache (): array
196+ {
197+ if (!$ this ->configCache ) {
198+ $ this ->configCache = $ this ->config ->get ('system ' );
199+ }
200+
201+ return $ this ->configCache ;
202+ }
161203}
0 commit comments