|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: inhere |
| 5 | + * Date: 2017-03-27 |
| 6 | + * Time: 16:17 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Toolkit\Extlib\Concern; |
| 10 | + |
| 11 | +use function count; |
| 12 | +use function in_array; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class SimpleEventStaticTrait |
| 16 | + * |
| 17 | + * @package Inhere\Console\Concern |
| 18 | + */ |
| 19 | +trait SimpleEventAwareTrait |
| 20 | +{ |
| 21 | + /** |
| 22 | + * set the supported events, if you need. |
| 23 | + * if it is empty, will allow register any event. |
| 24 | + * |
| 25 | + * @var array |
| 26 | + */ |
| 27 | + protected static $supportedEvents = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * registered Events |
| 31 | + * |
| 32 | + * @var array |
| 33 | + * [ |
| 34 | + * 'event' => bool, // is once event |
| 35 | + * ] |
| 36 | + */ |
| 37 | + private static $events = []; |
| 38 | + |
| 39 | + /** |
| 40 | + * events and handlers |
| 41 | + * |
| 42 | + * @var array |
| 43 | + * [ |
| 44 | + * 'event' => callable, // event handler |
| 45 | + * ] |
| 46 | + */ |
| 47 | + private static $eventHandlers = []; |
| 48 | + |
| 49 | + /** |
| 50 | + * register a event handler |
| 51 | + * |
| 52 | + * @param string $event |
| 53 | + * @param callable $handler |
| 54 | + * @param bool $once |
| 55 | + */ |
| 56 | + public function on(string $event, callable $handler, bool $once = false): void |
| 57 | + { |
| 58 | + if (self::isSupportedEvent($event)) { |
| 59 | + self::$eventHandlers[$event][] = $handler; |
| 60 | + |
| 61 | + if (!isset(self::$events[$event])) { |
| 62 | + self::$events[$event] = $once; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * register a once event handler |
| 69 | + * |
| 70 | + * @param string $event |
| 71 | + * @param callable $handler |
| 72 | + */ |
| 73 | + public function once(string $event, callable $handler): void |
| 74 | + { |
| 75 | + $this->on($event, $handler, true); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * trigger event |
| 80 | + * |
| 81 | + * @param string $event |
| 82 | + * @param mixed ...$args |
| 83 | + * |
| 84 | + * @return bool |
| 85 | + */ |
| 86 | + public function fire(string $event, ...$args): bool |
| 87 | + { |
| 88 | + if (!isset(self::$events[$event])) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + // call event handlers of the event. |
| 93 | + /** @var mixed $return */ |
| 94 | + $return = true; |
| 95 | + foreach ((array)self::$eventHandlers[$event] as $cb) { |
| 96 | + $return = $cb(...$args); |
| 97 | + // return FALSE to stop go on handle. |
| 98 | + if (false === $return) { |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // is a once event, remove it |
| 104 | + if (self::$events[$event]) { |
| 105 | + return $this->off($event); |
| 106 | + } |
| 107 | + |
| 108 | + return (bool)$return; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * remove event and it's handlers |
| 113 | + * |
| 114 | + * @param $event |
| 115 | + * |
| 116 | + * @return bool |
| 117 | + */ |
| 118 | + public function off(string $event): bool |
| 119 | + { |
| 120 | + if ($this->hasEvent($event)) { |
| 121 | + unset(self::$events[$event], self::$eventHandlers[$event]); |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param string $event |
| 131 | + * |
| 132 | + * @return bool |
| 133 | + */ |
| 134 | + public function hasEvent(string $event): bool |
| 135 | + { |
| 136 | + return isset(self::$events[$event]); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @param string $event |
| 141 | + * |
| 142 | + * @return bool |
| 143 | + */ |
| 144 | + public function isOnce(string $event): bool |
| 145 | + { |
| 146 | + if ($this->hasEvent($event)) { |
| 147 | + return self::$events[$event]; |
| 148 | + } |
| 149 | + |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * check $name is a supported event name |
| 155 | + * |
| 156 | + * @param string $event |
| 157 | + * |
| 158 | + * @return bool |
| 159 | + */ |
| 160 | + public static function isSupportedEvent(string $event): bool |
| 161 | + { |
| 162 | + if (!$event || !preg_match('/[a-zA-z][\w-]+/', $event)) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + if ($ets = self::$supportedEvents) { |
| 167 | + return in_array($event, $ets, true); |
| 168 | + } |
| 169 | + |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @return array |
| 175 | + */ |
| 176 | + public static function getSupportEvents(): array |
| 177 | + { |
| 178 | + return self::$supportedEvents; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @param array $supportedEvents |
| 183 | + */ |
| 184 | + public static function setSupportEvents(array $supportedEvents): void |
| 185 | + { |
| 186 | + self::$supportedEvents = $supportedEvents; |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * @return array |
| 191 | + */ |
| 192 | + public static function getEvents(): array |
| 193 | + { |
| 194 | + return self::$events; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @return int |
| 199 | + */ |
| 200 | + public static function countEvents(): int |
| 201 | + { |
| 202 | + return count(self::$events); |
| 203 | + } |
| 204 | +} |
0 commit comments