| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Traits; |
| 14 |
|
| 15 |
|
| 16 |
use Webklex\PHPIMAP\Events\Event; |
| 17 |
use Webklex\PHPIMAP\Exceptions\EventNotFoundException; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
trait HasEvents { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
protected array $events = []; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function setEvent(string $section, string $event, mixed $class): void { |
| 40 |
if (isset($this->events[$section])) { |
| 41 |
$this->events[$section][$event] = $class; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public function setEvents(array $events): void { |
| 50 |
$this->events = $events; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function getEvent(string $section, string $event): Event|string { |
| 62 |
if (isset($this->events[$section])) { |
| 63 |
return $this->events[$section][$event]; |
| 64 |
} |
| 65 |
throw new EventNotFoundException(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function getEvents(): array { |
| 74 |
return $this->events; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function dispatch(string $section, string $event, mixed ...$args): void { |
| 82 |
$event = $this->getEvent($section, $event); |
| 83 |
$event::dispatch(...$args); |
| 84 |
} |
| 85 |
|
| 86 |
} |