@@ -13,26 +13,68 @@ class ConfigProvider
1313 public const delegators = 'delegators ' ;
1414 public const services = 'services ' ;
1515 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+
1738 public function __invoke (): array
1839 {
1940 $ array = [self ::dependencies => array_filter (
2041 $ this ->getDependencies (), static fn ($ v ) => !empty ($ v )
2142 )];
2243
23- return ($ cfg = $ this ->getConfig ()) !== [] ? array_merge ($ array , $ cfg ) : $ array ;
44+ return ($ cfg = $ this ->getMergedConfig ()) !== [] ? array_merge ($ array , $ cfg ) : $ array ;
2445 }
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+
2660 protected function getDependencies (): array
2761 {
28- return [
62+ $ dependencies = [
2963 self ::factories => $ this ->getFactories (),
3064 self ::invokables => $ this ->getInvokables (),
3165 self ::autowires => $ this ->getAutowires (),
3266 self ::aliases => $ this ->getAliases (),
3367 self ::delegators => $ this ->getDelegators (),
3468 self ::services => $ this ->getServices (),
3569 ];
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 ;
3678 }
3779
3880 protected function getConfig (): array
0 commit comments