Skip to content

Commit 0a1e671

Browse files
committed
Completed Container
0 parents  commit 0a1e671

11 files changed

Lines changed: 535 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor
2+
/.idea

Index.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
3+
require_once './vendor/autoload.php';
4+
5+
require_once './test/Project.php';
6+
require_once './test/User.php';
7+
8+
9+
$container = new \PluginMaster\Container\Container();
10+
11+
12+
$container->set('User', function () {
13+
return new User();
14+
});
15+
16+
/** @var User $user */
17+
$user = $container->get('User') ;
18+
$user->setName('Afiqul Islam');
19+
echo $user->getName().PHP_EOL;
20+
21+
22+
/** @var User $user */
23+
$user = $container->call('Project@user', ['name' => 'PHP IS POWER']);
24+
echo $user->getName().PHP_EOL;
25+
26+
27+
/** @var User $user */
28+
$user = $container->call('Project#user', ['name' => 'AL EMRAN'], ['methodSeparator'=>'#']);
29+
30+
echo $user->getName().PHP_EOL;
31+
32+
33+
/** @var User $user */
34+
$user = $container->call([Project::class, 'user'], ['name' => 'AL EMRAN']);
35+
36+
echo $user->getName().PHP_EOL;
37+
38+
39+
/** @var Project $user */
40+
$user = $container->make(Project::class, ['name' => 'Make User']);
41+
if($container->has(Project::class)) {
42+
echo $container->get(Project::class)->user()->getname().PHP_EOL;
43+
}
44+
45+
46+
/** @var User $user */
47+
$user = $container->get(User::class);
48+
49+
$user->setName('Get User');
50+
echo $user->getName().PHP_EOL;

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
### Simplified Dependency Injection Container For PluginMaster
2+
3+
#### Simplified dependency injection container for PHP project. We create this package for PluginMaster(WordPress Plugin Development Framework)
4+
5+
6+
Example:
7+
8+
```injectablephp
9+
10+
$container = new \PluginMaster\Container\Container();
11+
12+
```
13+
14+
#### 1. Set Alias
15+
16+
```injectablephp
17+
18+
$container->set('User', function () {
19+
return new User();
20+
});
21+
```
22+
23+
#### 2. Get Alias
24+
25+
```injectablephp
26+
$user = $container->get('User') ;
27+
```
28+
#### 3. Call A method of class or callback function
29+
30+
```injectablephp
31+
$container->call('Project@user', ['name' => 'PHP IS POWER' ]);
32+
```
33+
34+
```injectablephp
35+
$container->call('Project#user', ['name' => 'AL EMRAN' ], ['methodSeparator'=>'#']);
36+
```
37+
38+
```injectablephp
39+
$container->call([Project::class, 'user'], ['name' => 'AL EMRAN' ]);
40+
```
41+
42+
#### 4. Make A Object from Class
43+
44+
```injectablephp
45+
$project = $container->make(Project::class, ['name' => 'Make User' ]);
46+
```
47+
48+
```injectablephp
49+
$user = $container->make(User::class);
50+
```
51+
52+
#### 5. get Object from provided class
53+
54+
```injectablephp
55+
$user = $container->get(User::class);
56+
```
57+
#### 6. Check Resolved Object or alias exist
58+
```injectablephp
59+
$user = $container->has(User::class);
60+
```
61+
62+
63+
### Test
64+
65+
### 1. Clone Repo
66+
```shell
67+
git clone ''
68+
```
69+
70+
#### 2. Install Dependency
71+
```shell
72+
composer install
73+
```
74+
75+
#### 3. Run Index.php file
76+
```php
77+
php Index.php
78+
```
79+

Test/Project.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
class Project
4+
{
5+
public function __construct(protected User $user, $name)
6+
{
7+
$this->user->setName($name);
8+
}
9+
10+
public function get(): string
11+
{
12+
return 'Running...';
13+
}
14+
15+
public function user(): User
16+
{
17+
return $this->user;
18+
}
19+
20+
public function call(): string
21+
{
22+
return 'Calling...';
23+
}
24+
25+
public function make(): string
26+
{
27+
return 'Calling...';
28+
}
29+
30+
}

Test/User.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
class User
4+
{
5+
private string $name;
6+
7+
public function getName(): string
8+
{
9+
return $this->name;
10+
}
11+
12+
public function setName(string $name): string
13+
{
14+
return $this->name = $name;
15+
}
16+
}

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "plugin-master/container",
3+
"description": "The PluginMaster DI Container.",
4+
"license": "MIT",
5+
"homepage": "https://github.com/WP-Plugin-Master/PluginMaster-Foundation",
6+
"support": {
7+
"issues": "https://github.com/WP-Plugin-Master/PluginMaster-Foundation/issues",
8+
"source": "https://github.com/WP-Plugin-Master/PluginMaster-Foundation"
9+
},
10+
"authors": [
11+
{
12+
"name": "AL EMRAN",
13+
"email": "emrancu1@gmail.com"
14+
}
15+
],
16+
"keywords": [
17+
"Plugin Master",
18+
"WordPress Plugin Development framework"
19+
],
20+
"autoload": {
21+
"classmap": [],
22+
"psr-4": {
23+
"PluginMaster\\Container\\": "src/"
24+
}
25+
},
26+
"require": {
27+
"php": "^8.0",
28+
"psr/container": "^2.0"
29+
},
30+
"require-dev": {
31+
},
32+
"minimum-stability": "dev",
33+
"prefer-stable": true,
34+
"config": {
35+
"allow-plugins": {
36+
"automattic/jetpack-autoloader": true
37+
}
38+
}
39+
}

composer.lock

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)