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 {
|
||||
call_user_func($item, $event);
|
||||
} catch (\Throwable $exception) {
|
||||
error($exception);
|
||||
\Kiri::getLogger()->json_log($exception);
|
||||
}
|
||||
if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) {
|
||||
break;
|
||||
|
||||
+2
-1
@@ -6,8 +6,9 @@ interface EventInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function process(): void;
|
||||
public function process(object $event): 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"]);
|
||||
}
|
||||
|
||||
}
|
||||
+2
-1
@@ -7,9 +7,10 @@ class TestListener implements EventInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param object $event
|
||||
* @return void
|
||||
*/
|
||||
public function process(): void
|
||||
public function process(object $event): void
|
||||
{
|
||||
// TODO: Implement process() method.
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"php": ">=8.5",
|
||||
"psr/event-dispatcher": "^1.0"
|
||||
},
|
||||
"autoload": {
|
||||
|
||||
Reference in New Issue
Block a user