@@ -13,26 +13,68 @@ class ConfigProvider
13
13
public const delegators = 'delegators ' ;
14
14
public const services = 'services ' ;
15
15
public const bootstrap = 'bootstrap ' ;
16
-
16
+
17
+ /**
18
+ * @var ConfigProvider[]
19
+ */
20
+ protected array $ providers = [];
21
+
22
+ final public function __construct ()
23
+ {
24
+ foreach ($ this ->getProviders () as $ provider ) {
25
+ $ this ->providers [] = new $ provider ;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * @template T of ConfigProvider
31
+ * @return class-string<T>[]
32
+ */
33
+ protected function getProviders (): array
34
+ {
35
+ return [];
36
+ }
37
+
17
38
public function __invoke (): array
18
39
{
19
40
$ array = [self ::dependencies => array_filter (
20
41
$ this ->getDependencies (), static fn ($ v ) => !empty ($ v )
21
42
)];
22
43
23
- return ($ cfg = $ this ->getConfig ()) !== [] ? array_merge ($ array , $ cfg ) : $ array ;
44
+ return ($ cfg = $ this ->getMergedConfig ()) !== [] ? array_merge ($ array , $ cfg ) : $ array ;
24
45
}
25
-
46
+
47
+ private function getMergedConfig (): array
48
+ {
49
+ if ($ this ->providers === []) return $ this ->getConfig ();
50
+
51
+ $ config = $ this ->getConfig ();
52
+ foreach ($ this ->providers as $ provider ) {
53
+ $ config = array_merge_recursive ($ config , $ provider ->getMergedConfig ());
54
+ }
55
+
56
+ return $ config ;
57
+ }
58
+
59
+
26
60
protected function getDependencies (): array
27
61
{
28
- return [
62
+ $ dependencies = [
29
63
self ::factories => $ this ->getFactories (),
30
64
self ::invokables => $ this ->getInvokables (),
31
65
self ::autowires => $ this ->getAutowires (),
32
66
self ::aliases => $ this ->getAliases (),
33
67
self ::delegators => $ this ->getDelegators (),
34
68
self ::services => $ this ->getServices (),
35
69
];
70
+
71
+ if ($ this ->providers !== []) {
72
+ foreach ($ this ->providers as $ provider ) {
73
+ $ dependencies = array_merge_recursive ($ dependencies , $ provider ->getDependencies ());
74
+ }
75
+ }
76
+
77
+ return $ dependencies ;
36
78
}
37
79
38
80
protected function getConfig (): array
0 commit comments