Event.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace Webman\Event;
  3. use Psr\Log\LoggerInterface;
  4. use support\Log;
  5. class Event
  6. {
  7. /**
  8. * @var array
  9. */
  10. protected static $eventMap = [];
  11. /**
  12. * @var array
  13. */
  14. protected static $prefixEventMap = [];
  15. /**
  16. * @var int
  17. */
  18. protected static $id = 0;
  19. /**
  20. * @var LoggerInterface
  21. */
  22. protected static $logger;
  23. /**
  24. * @param mixed $event_name
  25. * @param callable $listener
  26. * @return int
  27. */
  28. public static function on($event_name, callable $listener): int
  29. {
  30. $is_prefix_name = $event_name[strlen($event_name) - 1] === '*';
  31. if ($is_prefix_name) {
  32. static::$prefixEventMap[substr($event_name, 0, -1)][++static::$id] = $listener;
  33. } else {
  34. static::$eventMap[$event_name][++static::$id] = $listener;
  35. }
  36. return static::$id;
  37. }
  38. /**
  39. * @param mixed $event_name
  40. * @param integer $id
  41. * @return int
  42. */
  43. public static function off($event_name, int $id): int
  44. {
  45. if (isset(static::$eventMap[$event_name][$id])) {
  46. unset(static::$eventMap[$event_name][$id]);
  47. return 1;
  48. }
  49. return 0;
  50. }
  51. /**
  52. * @param mixed $event_name
  53. * @param mixed $data
  54. * @param bool $halt
  55. * @return array|null|mixed
  56. */
  57. public static function emit($event_name, $data, bool $halt = false)
  58. {
  59. $listeners = static::getListeners($event_name);
  60. $responses = [];
  61. foreach ($listeners as $listener) {
  62. try {
  63. $response = $listener($data, $event_name);
  64. } catch (\Throwable $e) {
  65. $responses[] = $e;
  66. if (!static::$logger && is_callable('\support\Log::error')) {
  67. static::$logger = Log::channel();
  68. }
  69. if (static::$logger) {
  70. static::$logger->error($e);
  71. }
  72. continue;
  73. }
  74. $responses[] = $response;
  75. if ($halt && !is_null($response)) {
  76. return $response;
  77. }
  78. if ($response === false) {
  79. break;
  80. }
  81. }
  82. return $halt ? null : $responses;
  83. }
  84. /**
  85. * @param mixed $event_name
  86. * @param mixed $data
  87. * @param bool $halt
  88. * @return array|null|mixed
  89. */
  90. public static function dispatch($event_name, $data, bool $halt = false)
  91. {
  92. $listeners = static::getListeners($event_name);
  93. $responses = [];
  94. foreach ($listeners as $listener) {
  95. $response = $listener($data, $event_name);
  96. $responses[] = $response;
  97. if ($halt && !is_null($response)) {
  98. return $response;
  99. }
  100. if ($response === false) {
  101. break;
  102. }
  103. }
  104. return $halt ? null : $responses;
  105. }
  106. /**
  107. * @return array
  108. */
  109. public static function list(): array
  110. {
  111. $listeners = [];
  112. foreach (static::$eventMap as $event_name => $callback_items) {
  113. foreach ($callback_items as $id => $callback_item) {
  114. $listeners[$id] = [$event_name, $callback_item];
  115. }
  116. }
  117. foreach (static::$prefixEventMap as $event_name => $callback_items) {
  118. foreach ($callback_items as $id => $callback_item) {
  119. $listeners[$id] = [$event_name . '*', $callback_item];
  120. }
  121. }
  122. ksort($listeners);
  123. return $listeners;
  124. }
  125. /**
  126. * @param mixed $event_name
  127. * @return callable[]
  128. */
  129. public static function getListeners($event_name): array
  130. {
  131. $listeners = static::$eventMap[$event_name] ?? [];
  132. foreach (static::$prefixEventMap as $name => $callback_items) {
  133. if (strpos($event_name, $name) === 0) {
  134. $listeners = array_merge($listeners, $callback_items);
  135. }
  136. }
  137. ksort($listeners);
  138. return $listeners;
  139. }
  140. /**
  141. * @param mixed $event_name
  142. * @return bool
  143. */
  144. public static function hasListener($event_name): bool
  145. {
  146. return !empty(static::getListeners($event_name));
  147. }
  148. }