44
55namespace MaplePHP \Core ;
66
7+ use MaplePHP \Container \Autowire ;
78use MaplePHP \Core \Configs \LoadConfigFiles ;
89use MaplePHP \Core \Support \ServiceProvider ;
910use MaplePHP \DTO \Format \Clock ;
@@ -42,14 +43,16 @@ public function __construct(string $dir)
4243 ->loadFiles ($ this ->dir . "/configs/ " );
4344
4445 $ this ->config = $ config ->fetch ();
45-
4646 $ app = App::boot (new Dir ($ this ->dir ), $ this ->config );
4747 $ this ->container = new Container ();
48- $ this ->container ->set ("config " , $ this ->config );
4948 $ this ->container ->set ("app " , $ app );
5049
51- Clock::setDefaultLocale ($ this ->config ['configs ' ]['locale ' ]);
52- Clock::setDefaultTimezone ($ this ->config ['configs ' ]['timezone ' ]);
50+ if (App::get ()->getApp ('locale ' ) !== null ) {
51+ Clock::setDefaultLocale (App::get ()->getApp ('locale ' ));
52+ }
53+ if (App::get ()->getApp ('timezone ' ) !== null ) {
54+ Clock::setDefaultTimezone (App::get ()->getApp ('timezone ' ));
55+ }
5356 }
5457
5558 /**
@@ -62,39 +65,63 @@ public function __construct(string $dir)
6265 */
6366 protected function load (ServerRequestInterface $ request , ?DispatchConfigInterface $ config = null ): KernelInterface
6467 {
65- $ this ->bootServiceProviders ();
68+
69+ if (isset ($ this ->config ['providers ' ]) && is_array ($ this ->config ['providers ' ])) {
70+ $ this ->bootServiceProviders ($ this ->config ['providers ' ]);
71+ }
72+
73+ if (isset ($ this ->config ['services ' ]['providers ' ]) && is_array ($ this ->config ['services ' ]['providers ' ])) {
74+ $ this ->bootServiceProviders ($ this ->config ['services ' ]['providers ' ]);
75+ }
76+
77+ if (isset ($ this ->config ['services ' ]['bindings ' ]) && is_array ($ this ->config ['services ' ]['bindings ' ])) {
78+ $ this ->bootBindings ($ this ->config ['services ' ]['bindings ' ]);
79+ }
80+
6681 return new Kernel ($ this ->container , $ this ->middlewares , $ config );
6782 }
6883
84+
85+ public function bootBindings (array $ bindings ): void
86+ {
87+ Autowire::interfaceWiring ($ bindings );
88+ }
89+
6990 /**
7091 * Boot service providers
7192 *
7293 * @return void
7394 */
74- protected function bootServiceProviders ()
95+ protected function bootServiceProviders (array $ providers )
7596 {
76- if (isset ($ this ->config ['providers ' ])) {
77- $ providers = [];
78-
79- // We want to register first, that way the providers could talk to eachother
80- // through the container or event listners if you want.
81- foreach ($ this ->config ['providers ' ] as $ providerClass ) {
82- $ provider = new $ providerClass ();
83- if (!($ provider instanceof ServiceProvider)) {
84- throw new \RuntimeException (
85- "$ providerClass is not an instance of " . ServiceProvider::class . "! "
86- );
87- }
88- $ provider ->register ($ this ->container );
89- $ providers [] = $ provider ;
97+ if ($ providers !== []) {
98+ $ set = [];
99+
100+ // We want to register first, that way the providers could talk to each other
101+ // through the container or event listeners if you want.
102+ foreach ($ providers as $ providerClass ) {
103+ $ this ->registerProvider ($ providerClass , $ set );
90104 }
91105
92- foreach ($ providers as $ provider ) {
106+ foreach ($ set as $ provider ) {
93107 $ provider ->boot ();
94108 }
95109 }
96110 }
97111
112+
113+ private function registerProvider (string $ providerClass , array &$ providers ): void
114+ {
115+ $ provider = new $ providerClass ();
116+ if (!($ provider instanceof ServiceProvider)) {
117+ throw new \RuntimeException (
118+ "$ providerClass is not an instance of " . ServiceProvider::class . "! "
119+ );
120+ }
121+ $ provider ->register ($ this ->container );
122+ $ providers [] = $ provider ;
123+ }
124+
98125 /**
99126 * @param Stream $stream
100127 * @return $this
0 commit comments