Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6a6096ea11 | |||
| b2a89a6f72 | |||
| c08c1731d1 | |||
| 846e195efa | |||
| 85df6626c1 | |||
| 79f430d898 |
+1
-1
@@ -42,7 +42,7 @@ class EventDispatch extends Component implements EventDispatcherInterface
|
|||||||
try {
|
try {
|
||||||
call_user_func($item, $event);
|
call_user_func($item, $event);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
error($exception);
|
\Kiri::getLogger()->json_log($exception);
|
||||||
}
|
}
|
||||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||||
break;
|
break;
|
||||||
|
|||||||
+7
-6
@@ -4,10 +4,11 @@ namespace Kiri\Events;
|
|||||||
|
|
||||||
interface EventInterface
|
interface EventInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param object $event
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function process(object $event): void;
|
||||||
|
|
||||||
/**
|
}
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function process(): void;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Kiri\Events;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Di\Interface\InjectTargetInterface;
|
||||||
|
|
||||||
|
#[\Attribute]
|
||||||
|
readonly class EventListener implements InjectTargetInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $event
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function __construct(public string $event)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $class
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function dispatch(string $class): void
|
||||||
|
{
|
||||||
|
// TODO: Implement dispatch() method.
|
||||||
|
$target = \Kiri::getDi()->getReflectionClass($class)->newInstanceWithoutConstructor();
|
||||||
|
|
||||||
|
if (!($target instanceof EventInterface)) {
|
||||||
|
throw new Exception("Event listen must implement " . EventInterface::class);
|
||||||
|
}
|
||||||
|
on($this->event, [$target, "process"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+8
-7
@@ -4,14 +4,15 @@ namespace Kiri\Events;
|
|||||||
|
|
||||||
class TestListener implements EventInterface
|
class TestListener implements EventInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @param object $event
|
||||||
*/
|
* @return void
|
||||||
public function process(): void
|
*/
|
||||||
|
public function process(object $event): void
|
||||||
{
|
{
|
||||||
// TODO: Implement process() method.
|
// TODO: Implement process() method.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.0",
|
"php": ">=8.5",
|
||||||
"psr/event-dispatcher": "^1.0"
|
"psr/event-dispatcher": "^1.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
Reference in New Issue
Block a user