Skip to content

Commit e45c388

Browse files
author
Wazabii
committed
Guide and minor patch
1 parent 8bd8abf commit e45c388

File tree

2 files changed

+87
-10
lines changed

2 files changed

+87
-10
lines changed

EventHandler.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ public function addHandler(object|string $handler, string|array $method = null):
4545
*/
4646
public function addEvent(callable|object|string $event, ?string $bind = null): void
4747
{
48-
if(is_string($event)) {
49-
$reflect = new Reflection($event);
50-
$event = $reflect->get();
51-
}
5248

53-
if(is_object($event) && !($event instanceof EventInterface)) {
54-
throw new \Exception("Event object/class needs to be instance of \"EventInterface\"!", 1);
49+
if(!is_callable($event)) {
50+
if(is_string($event)) {
51+
$reflect = new Reflection($event);
52+
$event = $reflect->get();
53+
}
54+
55+
if(is_object($event) && !($event instanceof EventInterface)) {
56+
throw new \Exception("Event object/class needs to be instance of \"EventInterface\"!", 1);
57+
}
5558
}
5659

5760
if (is_null($bind)) {
@@ -129,10 +132,10 @@ final protected function triggerEvents(): void
129132
*/
130133
final protected function getEvent(callable|object $data): void
131134
{
132-
if(is_object($data)) {
133-
$data->resolve();
134-
} else {
135+
if(is_callable($data)) {
135136
$data();
137+
} else {
138+
$data->resolve();
136139
}
137140
}
138141
}

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11

2+
23
# Container, Factories and the dependency injector
34
PSR Container built for MaplePHP framework
45

56
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.
67

8+
- [Container](#container)
9+
- [Factory](#factory)
10+
- [Dependency injector](#dependency-injector)
11+
- [Event handler](#event-handler)
12+
713
## Container
814
Containers allowing you to easily create and retrieve objects that are needed throughout your application.
915
```php
@@ -25,7 +31,7 @@ $container->factory("factoryKey", function() {
2531
});
2632
echo $container->get("factoryKey"); // Will return TestClassC
2733
```
28-
## The dependency injector
34+
## Dependency injector
2935
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.
3036

3137
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 {
6167

6268
}
6369
```
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,
85+
});
86+
echo $logger->error("A error message to log");
87+
// Will log message AND execute the event
88+
```
89+
90+
### Example 2 - (Bind to method)
91+
```php
92+
$logger = new EventHandler();
93+
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
94+
$logger->addEvent(function() {
95+
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 {
112+
113+
public function someMethod(string $what): string
114+
{
115+
return "Hello {$what}!";
116+
}
117+
118+
// Resolve method will be executed in conjunction
119+
// with logger event method
120+
public function resolve(): void
121+
{
122+
var_dump($this->someMethod("world"));
123+
}
124+
};
125+
126+
$logger = new EventHandler();
127+
$logger->addHandler(new Logger(), ["emergency", "alert", "critical"]);
128+
$logger->addEvent(function() {
129+
var_dump("Executed in conjunction with logger event method");
130+
// You could add a mail function that will send log message to you,
131+
});
132+
echo $logger->error("A error message to log");
133+
// Will only log message
134+
echo $logger->alert("A error message to log");
135+
// Will log message AND execute the event
136+
```
137+

0 commit comments

Comments
 (0)