Skip to content

Commit 41c1fca

Browse files
author
Nir
committed
Add service container and integrate into PluginLoader
1 parent 320e881 commit 41c1fca

3 files changed

Lines changed: 142 additions & 21 deletions

File tree

Plugin/PluginLoader.php

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use NirjharLo\WP_Plugin_Framework\Engine\Src\RestApi as RestApi;
1414

1515
use NirjharLo\WP_Plugin_Framework\Engine\Lib\Cron as Cron;
16+
use NirjharLo\WP_Plugin_Framework\Support\Container;
1617

1718
if ( ! defined( 'ABSPATH' ) ) {
1819
exit;
@@ -26,7 +27,7 @@
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

Plugin/Support/Container.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
namespace NirjharLo\WP_Plugin_Framework\Support;
3+
4+
/**
5+
* Simple dependency injection container inspired by Laravel's container.
6+
*/
7+
class Container
8+
{
9+
/**
10+
* Registered bindings.
11+
*
12+
* @var array<string, callable>
13+
*/
14+
protected $bindings = [];
15+
16+
/**
17+
* Resolved singleton instances.
18+
*
19+
* @var array<string, object>
20+
*/
21+
protected $instances = [];
22+
23+
/**
24+
* Register a binding with the container.
25+
*
26+
* @param string $abstract
27+
* @param callable $concrete
28+
*
29+
* @return void
30+
*/
31+
public function bind($abstract, callable $concrete)
32+
{
33+
$this->bindings[$abstract] = $concrete;
34+
}
35+
36+
/**
37+
* Register a singleton binding with the container.
38+
*
39+
* @param string $abstract
40+
* @param callable $concrete
41+
*
42+
* @return void
43+
*/
44+
public function singleton($abstract, callable $concrete)
45+
{
46+
$this->bindings[$abstract] = function ($container) use ($concrete, $abstract) {
47+
if (!isset($this->instances[$abstract])) {
48+
$this->instances[$abstract] = $concrete($container);
49+
}
50+
51+
return $this->instances[$abstract];
52+
};
53+
}
54+
55+
/**
56+
* Resolve an instance from the container.
57+
*
58+
* @param string $abstract
59+
*
60+
* @return mixed
61+
*/
62+
public function make($abstract)
63+
{
64+
if (isset($this->instances[$abstract])) {
65+
return $this->instances[$abstract];
66+
}
67+
68+
if (isset($this->bindings[$abstract])) {
69+
return $this->bindings[$abstract]($this);
70+
}
71+
72+
return new $abstract();
73+
}
74+
}

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@ For production use `composer install --no-dev`.
1818
## Usage
1919
1. Update namespaces in `wp-plugin-framework.php` and `Plugin/PluginLoader.php` to match your plugin name.
2020
2. Instantiate any of the engine classes or extend them to add your own logic.
21-
3. Register plugin features inside `PluginLoader` to keep everything organised.
21+
3. A lightweight service container now powers `PluginLoader`, keeping class instantiation clean and testable.
22+
4. Register plugin features inside `PluginLoader` to keep everything organised.
2223

2324
## Features
2425
- Settings pages, widgets, metaboxes and shortcodes
2526
- Database migration helpers
2627
- Fluent API integration class
2728
- Cron scheduler wrapper
2829
- REST API controller scaffold
30+
- Simple service container for dependency injection
2931

3032
## Contributing
3133
Pull requests are welcome. For major changes please open an issue first to discuss what you would like to change.

0 commit comments

Comments
 (0)