You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-1Lines changed: 75 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,15 @@
1
1
2
+
2
3
# Container, Factories and the dependency injector
3
4
PSR Container built for MaplePHP framework
4
5
5
6
Container, Factories and dependency injectors will help to make your PHP code more maintainable, flexible, and testable by reducing coupling between objects and centralizing the management of dependencies.
6
7
8
+
-[Container](#container)
9
+
-[Factory](#factory)
10
+
-[Dependency injector](#dependency-injector)
11
+
-[Event handler](#event-handler)
12
+
7
13
## Container
8
14
Containers allowing you to easily create and retrieve objects that are needed throughout your application.
echo $container->get("factoryKey"); // Will return TestClassC
27
33
```
28
-
## The dependency injector
34
+
## Dependency injector
29
35
Dependency injection is a technique for managing dependencies between objects in an application. Instead of creating objects directly in your code, you can pass them in as dependencies when you instantiate an object. This makes your code more modular and easier to test, as you can easily swap out dependencies for mock objects or other implementations.
30
36
31
37
You can use the **Dependency injector** just like create any other container, as long as you dont add arguments or try to access method, if you do that then it will automatically disable **Dependency injector**. It is design like this becouse it will load in all class reclusive into endlessly.
@@ -61,3 +67,71 @@ class YourClass {
61
67
62
68
}
63
69
```
70
+
71
+
## Event handler
72
+
73
+
### Initiate
74
+
```php
75
+
use MaplePHP\Container\EventHandler;
76
+
```
77
+
78
+
### Example 1 - (Callable)
79
+
```php
80
+
$logger = new EventHandler();
81
+
$logger->addHandler(new Logger());
82
+
$logger->addEvent(function() {
83
+
var_dump("Executed in conjunction with logger every method");
84
+
// You could add a mail function that will send log message to you,
var_dump("Executed in conjunction with logger event method");
96
+
// You could add a mail function that will send log message to you,
97
+
});
98
+
echo $logger->error("A error message to log");
99
+
// Will only log message
100
+
echo $logger->alert("A error message to log");
101
+
// Will log message AND execute the event
102
+
```
103
+
104
+
### Example 3 - (Add service to handler)
105
+
Set the namespace to the **EventInterface**.
106
+
```php
107
+
use MaplePHP\Container\Interfaces\EventInterface;
108
+
```
109
+
Then extend a class with implements to the interface **EventInterface** and add the method **resolve** to that class. I am using a "Anonymous Function" bellow as an example just to show that **EventInterface** is required, you can use a regular class.
110
+
```php
111
+
$callableFunction = new class implements EventInterface {
0 commit comments