1313use NirjharLo \WP_Plugin_Framework \Engine \Src \RestApi as RestApi ;
1414
1515use NirjharLo \WP_Plugin_Framework \Engine \Lib \Cron as Cron ;
16+ use NirjharLo \WP_Plugin_Framework \Support \Container ;
1617
1718if ( ! defined ( 'ABSPATH ' ) ) {
1819 exit ;
2627 * @package wp-plugin-framework
2728 */
2829
29- final class PluginLoader {
30+ final class PluginLoader {
3031
3132 /**
3233 * @var String
@@ -79,12 +80,56 @@ final class PluginLoader {
7980 *
8081 * @var Array
8182 */
82- protected static $ pluginPageLinks = array (
83- array (
84- 'slug ' => '' ,
85- 'label ' => '' ,
86- ),
87- );
83+ protected static $ pluginPageLinks = array (
84+ array (
85+ 'slug ' => '' ,
86+ 'label ' => '' ,
87+ ),
88+ );
89+
90+ public function __construct () {
91+ $ this ->container = new Container ();
92+ $ this ->registerBindings ();
93+ }
94+
95+ /**
96+ * Register bindings with the container.
97+ *
98+ * @return void
99+ */
100+ protected function registerBindings () {
101+ $ this ->container ->singleton ( Cpt::class, function () {
102+ return new Cpt ();
103+ } );
104+ $ this ->container ->singleton ( Settings::class, function () {
105+ return new Settings ();
106+ } );
107+ $ this ->container ->singleton ( Widget::class, function () {
108+ return new Widget ();
109+ } );
110+ $ this ->container ->singleton ( Metabox::class, function () {
111+ return new Metabox ();
112+ } );
113+ $ this ->container ->singleton ( Shortcode::class, function () {
114+ return new Shortcode ();
115+ } );
116+ $ this ->container ->singleton ( RestApi::class, function () {
117+ return new RestApi ();
118+ } );
119+ $ this ->container ->singleton ( Cron::class, function () {
120+ return new Cron ();
121+ } );
122+ $ this ->container ->singleton ( Db::class, function () {
123+ return new Db ();
124+ } );
125+ }
126+
127+ /**
128+ * Service container instance.
129+ *
130+ * @var Container
131+ */
132+ protected $ container ;
88133
89134
90135 /**
@@ -121,7 +166,7 @@ public function installation() {
121166 */
122167 public function dbInstall () {
123168
124- $ db = new Db ( );
169+ $ db = $ this -> container -> make ( Db::class );
125170 $ db ->table = self ::$ pluginTable ;
126171 $ db ->sql = '`ID` mediumint(9) NOT NULL AUTO_INCREMENT,
127172 `date` date NOT NULL,
@@ -158,11 +203,11 @@ public function dbNotInstalledErrorMsg() { ?>
158203 */
159204 public function flushPermalinks () {
160205
161- if ( class_exists ( 'NirjharLo \\WP_Plugin_Framework \\Engine \\Src \\Cpt ' ) ) {
162- new Cpt ( );
163- flush_rewrite_rules ();
164- }
165- }
206+ if ( class_exists ( 'NirjharLo \\WP_Plugin_Framework \\Engine \\Src \\Cpt ' ) ) {
207+ $ this -> container -> make ( Cpt::class );
208+ flush_rewrite_rules ();
209+ }
210+ }
166211
167212
168213 /**
@@ -179,7 +224,7 @@ public function scripts() {
179224 */
180225 public function cronActivation () {
181226
182- $ cron = new Cron ( );
227+ $ cron = $ this -> container -> make ( Cron::class );
183228 $ schedule = $ cron ->schedule (
184229 array (
185230 'timestamp ' => current_time ( 'timestamp ' ),
@@ -248,7 +293,7 @@ public function cronUninstall() {
248293 */
249294 public function cpt () {
250295
251- new Cpt ( );
296+ $ this -> container -> make ( Cpt::class );
252297 }
253298
254299
@@ -257,7 +302,7 @@ public function cpt() {
257302 */
258303 private function settings () {
259304
260- new Settings ( );
305+ $ this -> container -> make ( Settings::class );
261306 }
262307
263308
@@ -266,7 +311,7 @@ private function settings() {
266311 */
267312 private function widgets () {
268313
269- new Widget ( );
314+ $ this -> container -> make ( Widget::class );
270315 }
271316
272317
@@ -275,7 +320,7 @@ private function widgets() {
275320 */
276321 private function metabox () {
277322
278- new Metabox ( );
323+ $ this -> container -> make ( Metabox::class );
279324 }
280325
281326
@@ -284,7 +329,7 @@ private function metabox() {
284329 */
285330 private function shortcode () {
286331
287- new Shortcode ( );
332+ $ this -> container -> make ( Shortcode::class );
288333 }
289334
290335
@@ -293,7 +338,7 @@ private function shortcode() {
293338 */
294339 private function rest_api () {
295340
296- new RestApi ( );
341+ $ this -> container -> make ( RestApi::class );
297342 }
298343
299344
0 commit comments